巷ではコンテナが大人気です。一方で、
本連載でもこれらの技術のいくつかは何度か取り上げられています。
ところで、
そこで今回は、systemd-nspawnというコマンドを使い、
強化版chroot
UNIX オペレーティングシステムには本来のルートディレクトリと論理的に分離し、chrootというコマンドがあります。
chrootのよくある利用方法としては次のようなものがあります。
- システムで動作しているサービスを隔離し、
万が一、 これが侵入者に乗っ取られた場合でも、 システム全体が脅威に晒されることを防ぐ - FTPサーバーなどのリモートユーザーがアクセスできる領域を制限する
- Linuxディストリビューションのインストールで利用する (※1)
chrootの歴史は古いですが、
今回紹介するsystemd-nspawnはこのchrootの強化版です
systemdのコンテナ機能はLinuxカーネルの機能である名前空間を主に利用しています。この機能を使えば、
systemd-nspawnは複数の種類がある名前空間をまとめて取り扱い、
そのため、systemd-nspawnはchrootと異なり、
Ubuntuのコンテナをとりあえず立ち上げてみる
systemd-nspawnはデフォルトではインストールされていないため、systemd-containerパッケージが必要です。
また、debootstrapパッケージも必要となります。
host$ sudo apt install -y systemd-container debootstrap
任意のディレクトリにdebootstrapコマンドを使い、artfulという名前で作成します。
host$ sudo debootstrap artful ~/artful
コマンドを実行すると、
I: Retrieving InRelease I: Checking Release signature I: Valid Release signature (key id 790BC7277767219C42C86F933B4FE6ACC0B21F32) I: Retrieving Packages I: Validating Packages I: Resolving dependencies of required packages... I: Resolving dependencies of base packages... (中略) I: Base system installed successfully.
このままでは、
host$ sudo systemd-nspawn -D ~/artful Spawning container artful on /home/popo/artful. Press ^] three times within 1s to kill container. host's /etc/localtime is not a symlink, not updating container timezone. -bash: cannot set terminal process group (-1): Inappropriate ioctl for device -bash: no job control in this shell mesg: ttyname failed: No such device
コンテナ起動時のメッセージにもありますが、Ctrlを押しながら]を3回、
続いて、
artful# useradd admin -G sudo -m # 管理者ユーザーの作成 artful# passwd admin # パスワードの設定 Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully
コンテナから抜けます。ブートなしの場合は以下で抜けることができます。
artful# exit logout Container artful exited successfully.
続いて、-bオプションを付けてコマンドを実行し、
host$ sudo systemd-nspawn -b -D ~/artful
様々なサービスがコンテナ内のsystemdにより起動させられ、
Ubuntu Artful Aardvark (development branch) ubuntu console ubuntu login:
先ほど作成したユーザーでログインしましょう。ログインできない場合はユーザー・Ctrlを押しながら]を1秒以内に3連打してコンテナを強制終了し、
DNSサーバーの設定
ここで、
artful$ ip addr show
ホストで利用できるすべてのIPアドレスがコンテナでも利用できることを確認できるはずです。systemd-nspawnはオプションを指定しない場合、
ただし、
17.systemd-resolvedがDNSリゾルバーに採用されており、/etc/は自動で管理されています。今回、
そのため、/etc/にDNSサーバーの情報を記入します。
まずはホスト側で利用しているDNSサーバーの情報を確認します。GUIの接続情報画面を開いて確認もできますが、
host$ systemd-resolve --status|grep "DNS Servers: " # Ubuntu 17.04以降 host$ nmcli con show (接続名) |grep "IP4.DNS" # Ubuntu 16.04 LTS Desktop版の場合 host$ grep nameserver /etc/resolv.conf # Ubuntu 16.04 LTS Server版のように、/etc/resolv.confでDNSサーバーを指定している場合
そして、/etc/の一部をコメントアウトを外し、
[Resolve] DNS=(ホストで利用しているDNSサーバーのIPアドレス)
その後、systemd-resolvedを再起動します。
artful$ sudo systemctl restart systemd-resolved
これで、
artful$ sudo apt install -y vim bash-completion
ブートありの場合のコンテナを終了・
artful$ sudo poweroff # 終了 artful$ sudo reboot # 再起動
Fedoraのコンテナを立ち上げる
Fedoraのコンテナも作ってみましょう。
別の端末を起動します。 Fedoraのルートファイルシステムを作るのに必要なyumをインストールします
host$ sudo apt install yum
Fedora 26のレポジトリのパッケージをダウンロードして展開し、
host$ wget http://ftp.riken.jp/Linux/fedora/releases/26/Everything/x86_64/os/Packages/f/fedora-repos-26-1.noarch.rpm host$ rpm2cpio fedora-repos-26-1.noarch.rpm | cpio -id host$ sudo cp etc/yum.repos.d/* /etc/yum/repos.d/ host$ sudo cp -R etc/pki/ /etc/
Fedoraのルートファイルシステムを、fedora-26というディレクトリに作成します。
host$ sudo yum -y --releasever=26 --installroot=$HOME/fedora-26 install systemd passwd dnf vim iproute sudo
あとのユーザー作成とDNS設定の手順はほぼ同じですが、
fedora-26# useradd admin -G wheel -m # sudoグループではなくwheelグループを指定する fedora-26# passwd admin
気をつけなければならない点としては、
fedora-26$ sudo dnf -y --releasever=26 install systemd passwd dnf vim iproute sudo fedora-release
fedora-releaseもインストールしているので、--releaseverオプションなしでもdnfが使えます
コンテナの中のsystemdを操作する
ここまでで、
さらに別の端末を立ち上げます。 machinectlコマンドを利用すると、
host$ machinectl MACHINE CLASS SERVICE OS VERSION ADDRESSES artful container systemd-nspawn ubuntu 17.10 - fedora-26 container systemd-nspawn fedora 26 - 2 machines listed.
systemd関連コマンドで末尾がctlで終わるコマンドの中には-Mオプションを利用できるものがあります。-Mオプションを使うと、systemd-nspawnで起動したコンテナを操作対象にできます。
次の例では、
host$ sudo journalctl -M artful host$ sudo systemctl status -M artful
-Mで指定しているコンテナの名前は、systemd-nspawnコマンドも-Mオプションを取ることができ、
このあたりがsystemd-nspawnであえてコンテナを動かしていておもしろい部分です
systemd採用のディストリビューションなら(たぶん)何でも動かせる
今回はUbuntuとFedoraのコンテナ動かす例を取り上げましたが、systemd-nspawnではルートファイルシステムが用意できれば、
Ubuntuと同じやり方でDebianを動かしてみたり、