12.2. 使用临时创建动态挂载点
作为 root 用户,您可以使用 bootc root.transient-ro 功能在 root 文件系统上 以只读方式 构建临时覆盖,创建动态顶级挂载点,并在需要时 以读写形式重新挂载 文件系统。
使用 root.transient-ro 功能时,要确保应用程序可以正确访问主机目录,并在 只读文件系统 上创建必要的挂载点,需要考虑以下事项:
- 应用程序需要绑定挂载主机目录,以匹配主机的绝对路径。
-
需要特定于平台的挂载点,如 macOS 上的
/Users。 - 部署后创建动态挂载点,但在应用程序启动之前。
-
对于常规进程,
文件系统仍为只读状态。
先决条件
-
container-tools元数据软件包已安装。
流程
创建一个
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在 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要在容器构建过程中启用
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 镜像。
使用临时根文件系统构建 bootc 镜像:
$ podman build --cap-add SYS_ADMIN -t transient-root-ro:test .这会添加
SYS_ADMIN功能,其在构建期间用于ostree操作。
验证
验证
构建镜像中正确设置了 ephemeral-ro = true配置文件:$ sudo podman run --rm transient-root-ro:test cat /usr/lib/ostree/prepare-root.conf您可以看到类似如下的输出:
[root] transient-ro = true在运行的系统中,验证运行时功能:
- 引导镜像。
创建
/etc/afs测试目录。$ sudo mkdir -p /etc/afs重启服务以触发动态挂载点创建:
$ systemctl restart transient-root-example.service服务
以读写形式重新挂载root,并创建/afs目录。
要在没有
systemd服务的情况下测试功能,请在运行的系统上以 root 用户身份创建一个测试目录:$ sudo export LIBMOUNT_FORCE_MOUNT2=always $ sudo unshare -m -- /bin/sh -c 'mount -o remount,rw / && mkdir /transient-test'验证是否已创建该目录:
$ sudo ls -d /transient-test /transient-test通过尝试写入根文件系统来验证其他进程的
只读状态:$ touch /another-test-file touch: cannot touch '/another-test-file': Read-only file system