12.2. transient-ro による動的マウントポイントの作成
root ユーザーは、bootc の root.transient-ro 機能を使用することで、デフォルトでは read-only であるルートファイルシステムの上に一時的なオーバーレイを構築し、動的なトップレベルのマウントポイントを作成して、必要に応じてファイルシステムを read-write として再マウントすることができます。
root.transient-ro 機能を使用する場合は、アプリケーションがホストディレクトリーに適切にアクセスし、read-only ファイルシステム上に必要なマウントポイントを作成できるように、以下のように考慮すべき点がいくつかあります。
- アプリケーションは、ホストの絶対パスに一致するホストディレクトリーをバインドマウントする必要があります。
-
macOS の
/Usersなど、プラットフォーム固有のマウントポイントが必要です。 - デプロイ後、アプリケーションの起動前に動的マウントポイントを作成します。
-
ファイルシステムは、通常のプロセスに対しては
read-onlyのまま維持されます。
前提条件
-
container-toolsメタパッケージがインストールされている。
手順
transient-root-example.serviceサービスを作成し、readおよびwrite権限を持つディレクトリーを作成します。次の例では、/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.targetContainerFile に次の設定を追加します。
# 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コンテナービルドプロセス中に
transient-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 .これにより、ビルド中の
ostree操作においてbootcが必要とするSYS_ADMINケーパビリティーが追加されます。
検証
ビルドしたイメージで
transient-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サービスにより、ルートが
read-writeとして再マウントされ、/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ルートファイルシステムへの書き込みを試行して、他のプロセスにとっては
read-onlyステータスであることを検証します。$ touch /another-test-file touch: cannot touch '/another-test-file': Read-only file system