12.2. 使用临时创建动态挂载点


作为 root 用户,您可以使用 bootc root.transient-ro 功能在 root 文件系统上 以只读方式 构建临时覆盖,创建动态顶级挂载点,并在需要时 以读写形式重新挂载 文件系统。

使用 root.transient-ro 功能时,要确保应用程序可以正确访问主机目录,并在 只读文件系统 上创建必要的挂载点,需要考虑以下事项:

  • 应用程序需要绑定挂载主机目录,以匹配主机的绝对路径。
  • 需要特定于平台的挂载点,如 macOS 上的 /Users
  • 部署后创建动态挂载点,但在应用程序启动之前。
  • 对于常规进程,文件系统仍为只读状态

先决条件

  • container-tools 元数据软件包已安装。

流程

  1. 创建一个 ephemeral-root-example.service 服务,以创建 具有读写权限 的目录。在以下示例中,/etc/afs 存在。

    [Unit]
    Description=Transient Root Example - Dynamic Mount Point Creation
    After=local-fs.target
    ConditionPathExists=/etc/afs
    DefaultDependencies=no
    
    [Service]
    Type=oneshot
    RemainAfterExit=yes
    MountFlags=slave
    # LIBMOUNT_FORCE_MOUNT2=always is required for mount command compatibility
    # See: https://github.com/util-linux/util-linux/issues/3171
    Environment=LIBMOUNT_FORCE_MOUNT2=always
    ExecStart=/usr/bin/bash -c "echo 'Transient root example: Detected /etc/afs, remounting root as read-write'; mount -o remount,rw /; mkdir -p /afs; echo 'Created /afs directory for AFS mount point'"
    
    [Install]
    WantedBy=multi-user.target
  2. 在 ContainerFile中添加以下配置:

    # Copy the systemd service unit that demonstrates dynamic mount point creation
    # The service will:
    # 1. Check for existence of /etc/afs (ConditionPathExists)
    # 2. Use MountFlags=slave for proper mount propagation
    # 3. Set LIBMOUNT_FORCE_MOUNT2=always for mount command compatibility
    # 4. Remount root as read-write and create /afs directory when triggered
    COPY transient-root-example.service /usr/lib/systemd/system/
    
    # Enable the service to start automatically on boot
    RUN systemctl enable transient-root-example.service
  3. 要在容器构建过程中启用 ephemeral-ro 选项,请在镜像中的 /usr/lib/ostree/prepare-root.conf 文件中创建以下配置:

    RUN echo -e '[root]\ntransient=true' > /usr/lib/ostree/prepare-root.conf && \
        set -x; kver=$(cd /usr/lib/modules && echo *); dracut -vf /usr/lib/modules/$kver/initramfs.img $kver
    注意

    当您更改文件系统配置时,您必须重新生成 initramfs 镜像。

  4. 使用临时根文件系统构建 bootc 镜像:

    $ podman build --cap-add SYS_ADMIN -t transient-root-ro:test .

    这会添加 SYS_ADMIN 功能,其在构建期间用于 ostree 操作。

验证

  1. 验证 构建镜像中正确设置了 ephemeral-ro = true 配置文件:

    $ sudo podman run --rm transient-root-ro:test cat /usr/lib/ostree/prepare-root.conf

    您可以看到类似如下的输出:

    [root]
    transient-ro = true
  2. 在运行的系统中,验证运行时功能:

    1. 引导镜像。
    2. 创建 /etc/afs 测试目录。

      $ sudo mkdir -p /etc/afs
    3. 重启服务以触发动态挂载点创建:

      $ systemctl restart transient-root-example.service

      服务 以读写形式重新挂载 root,并创建 /afs 目录。

  3. 要在没有 systemd 服务的情况下测试功能,请在运行的系统上以 root 用户身份创建一个测试目录:

    $ sudo export LIBMOUNT_FORCE_MOUNT2=always
    $ sudo unshare -m -- /bin/sh -c 'mount -o remount,rw / && mkdir /transient-test'
  4. 验证是否已创建该目录:

    $ sudo ls -d /transient-test /transient-test
  5. 通过尝试写入根文件系统来验证其他进程的 只读状态

    $ touch /another-test-file
    touch: cannot touch '/another-test-file': Read-only file system
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

关于红帽文档

Legal Notice

Theme

© 2026 Red Hat
返回顶部