跳转到内容
搜索 RHEL 文档

    浏览当前文档目录

    RHEL 10 Podman 卷与 SELinux :Z:rootless 数据持久化

    容器写数据有两个选择:volume(Podman 管)和 bind mount(你管目录)。在 RHEL 上 SELinux 会额外检查标签:volume 自动打 container_file_t,bind 目录需要 :Z 或 :z。本页用 rootless Podman 实测两者。

    在 PVE 390 rhel10-lab(RHEL 10.2)以 cloud-user 执行:

    Terminal window
    podman volume create demo-vol
    podman 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-z
    echo host > /tmp/podman-z/host.txt
    podman 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.txt
    podman volume rm demo-vol

    E3 记录:plans/content-expansion/evidence/rhel/rhel-podman-volume/run.json(7 步,含卷/标签输出 sha256)。

    • podman volume create 创建的卷由 Podman 管理,挂进容器时自动是 container_file_t;
    • bind mount 的宿主目录保留原标签(如 user_tmp_t),容器进程通常读不了;
    • :Z 给目录打私有 MCS 类别(如 c414,c956),只允许这个容器访问;
    • :z 是共享类别,多容器共享同一目录时用。
    $ 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/_data

    rootless 卷在用户家目录;rootful 才在 /var/lib/containers/storage/volumes。备份/迁移时别找错地方。

    # 初始标签(示例)
    $ ls -Z /tmp/podman-z/host.txt
    unconfined_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'
    host
    system_u:object_r:container_file_t:s0:c414,c956 host.txt
    $ ls -Z /tmp/podman-z/host.txt
    system_u:object_r:container_file_t:s0:c414,c956 /tmp/podman-z/host.txt

    :Z 把宿主目录重标为 container_file_t + 私有 MCS 类别。这会改宿主机文件标签,所以:

    • 只对专用于容器的目录用 :Z;
    • 多容器共享同一目录用 :z(共享类别);
    • 不要对 /etc、家目录根、系统目录用 :Z。
    Terminal window
    podman volume rm demo-vol
    rm -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/sharepodman system df、迁移 graphroot
    多容器抢同一目录:Z 私有类别互斥改 :z 或各自卷
    • 数据放 volume 还是 bind;
    • bind 目录是否只给容器用(能否接受 :Z 改标签);
    • 多容器共享用 :z;
    • 卷路径在家目录(rootless);
    • 删除前确认无容器引用;
    • 备份覆盖卷目录而非容器层。
    • 未测试 :z 共享类别的多容器并发;
    • 未测试 NFS/CSI 卷;
    • 未测试 rootful 卷与 rootless 卷互迁;
    • 未测试 podman unshare chown 修 UID 归属。