LoginSignup
2
2

More than 5 years have passed since last update.

systemd-nspawn+overlayfsについて

Last updated at Posted at 2017-12-07

systemd-nspawn Advent Calendar 2017 5日目の記事です。

systemd-nspawnはoverlayfsを使って簡単にCoW(Copy-On-Write)な環境を作れます

準備

overlayモジュールが必要なので読み込みます

modprobe overlay
echo overlay >> /etc/modules-load.d/modules.conf

使ってみる

overlayfsを使う場合は--overlay=オプションを指定します
overlayオプションは3つのファイルパスを取り(2つでもOKですが今回は3つの場合を説明します)

--overlay=一番最初のレイヤー:overlayfsの反映先:コンテナ側のマウントパス

となります

例えばこのようにした場合、ホスト側の/mntがコンテナ側の/mntに繋がるのですが、コンテナ側で行った変更はホスト側から見ると反映されません。逆にホスト側からの変更はコンテナ側には見れるようになります。

systemd-nspawn -D ubuntu --overlay=/mnt:/var/tmp:/mnt

実験してみます

  • root@container: コンテナ内
  • root@host: ホスト側

で打つコマンドを示しています

root@container:/# touch /mnt/container.txt
root@container:/# ls -l /mnt/container.txt # 存在する

root@host:/# ls -l /mnt/container.txt # 存在しない
root@host:/# ls -l /var/tmp/container.txt # 存在する
root@host:/# touch /mnt/host.txt

root@container:/# ls -l /mnt/host.txt # 存在する
root@container:/# rm /mnt/host.txt

root@host:/# ls -l /mnt/host.txt # 存在する
root@host:/# ls -l /var/tmp/host.txt # 削除を現す特殊ファイル

要するにコンテナ内の/mntで行った変更は差分情報を/var/tmpに書き込まれて、ホスト側の/mntには影響がないという挙動です

こんな感じで、簡単に使えるので便利です
その他詳しいことはman systemd-nspawnを参照ください
ちなみに新しいバージョンだと、+をファイルパス前につけるとコンテナからの相対パスを指定出来たり、2番目を省略すると自動的に/var/tmpを使ったりと色々便利になってます!

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2