RHEL 10 Podman 卷与 SELinux :Z:rootless 数据持久化
This content is not available in your language yet.
容器写数据有两个选择:volume(Podman 管)和 bind mount(你管目录)。在 RHEL 上 SELinux 会额外检查标签:volume 自动打 container_file_t,bind 目录需要 :Z 或 :z。本页用 rootless Podman 实测两者。
在 PVE 390 rhel10-lab(RHEL 10.2)以 cloud-user 执行:
podman volume create demo-volpodman run --rm -v demo-vol:/data \ registry.access.redhat.com/ubi10/ubi-minimal \ sh -c 'echo hello > /data/x; cat /data/x; ls -Z /data'podman volume inspect demo-vol --format '{{.Mountpoint}}'
mkdir -p /tmp/podman-zecho host > /tmp/podman-z/host.txtpodman run --rm -v /tmp/podman-z:/data:Z \ registry.access.redhat.com/ubi10/ubi-minimal \ sh -c 'cat /data/host.txt; ls -Z /data'ls -Z /tmp/podman-z/host.txtpodman volume rm demo-volE3 记录:plans/content-expansion/evidence/rhel/rhel-podman-volume/run.json(7 步,含卷/标签输出 sha256)。
先给结论:volume 自动带标签
Section titled “先给结论:volume 自动带标签”podman volume create创建的卷由 Podman 管理,挂进容器时自动是container_file_t;- bind mount 的宿主目录保留原标签(如
user_tmp_t),容器进程通常读不了; :Z给目录打私有 MCS 类别(如c414,c956),只允许这个容器访问;:z是共享类别,多容器共享同一目录时用。
创建卷并写数据
Section titled “创建卷并写数据”$ podman run --rm -v demo-vol:/data ubi-minimal sh -c 'echo hello > /data/x; ls -Z /data'system_u:object_r:container_file_t:s0 x卷内文件标签是 container_file_t,没有 MCS 类别(卷是 Podman 专有的)。
$ podman volume inspect demo-vol --format '{{.Mountpoint}}'/home/cloud-user/.local/share/containers/storage/volumes/demo-vol/_datarootless 卷在用户家目录;rootful 才在 /var/lib/containers/storage/volumes。备份/迁移时别找错地方。
bind + :Z
Section titled “bind + :Z”# 初始标签(示例)$ ls -Z /tmp/podman-z/host.txtunconfined_u:object_r:user_tmp_t:s0 /tmp/podman-z/host.txt
$ podman run --rm -v /tmp/podman-z:/data:Z ubi-minimal \ sh -c 'cat /data/host.txt; ls -Z /data'hostsystem_u:object_r:container_file_t:s0:c414,c956 host.txt
$ ls -Z /tmp/podman-z/host.txtsystem_u:object_r:container_file_t:s0:c414,c956 /tmp/podman-z/host.txt:Z 把宿主目录重标为 container_file_t + 私有 MCS 类别。这会改宿主机文件标签,所以:
- 只对专用于容器的目录用
:Z; - 多容器共享同一目录用
:z(共享类别); - 不要对
/etc、家目录根、系统目录用:Z。
podman volume rm demo-volrm -rf /tmp/podman-z如果 volume rm 报 “volume is in use”,先 podman ps -a 找到容器并删除。
| 现象 | 原因 | 处置 |
|---|---|---|
Permission denied 读 bind 文件 | 标签不匹配 | 加 :Z/:z,或 chcon |
volume is in use | 容器仍引用 | 删容器再删卷 |
| 家目录满 | rootless 卷在 ~/.local/share | podman system df、迁移 graphroot |
| 多容器抢同一目录 | :Z 私有类别互斥 | 改 :z 或各自卷 |
- 数据放 volume 还是 bind;
- bind 目录是否只给容器用(能否接受
:Z改标签); - 多容器共享用
:z; - 卷路径在家目录(rootless);
- 删除前确认无容器引用;
- 备份覆盖卷目录而非容器层。
未实测与边界
Section titled “未实测与边界”- 未测试
:z共享类别的多容器并发; - 未测试 NFS/CSI 卷;
- 未测试 rootful 卷与 rootless 卷互迁;
- 未测试
podman unshare chown修 UID 归属。
- RHEL 10 Podman rootless — subuid/UID 映射
- RHEL 10 Quadlet — systemd 用户服务
- RHEL 10 SELinux:文件上下文与布尔值 — 标签基础
- 官方:Building, running, and managing containers