systemd-nspawnのGentoo Linuxコンテナ作成方法です。
ホスト環境
項目 | 詳細 |
---|---|
OS | Gentoo Linux |
Kernel | 4.9.76-gentoo-r1 |
Systemd | systemd-236-r5 |
コンテナ作成
Stage3のインストール
cd /tmp
mkdir gentoo
wget http://distfiles.gentoo.org/releases/amd64/autobuilds/current-stage3-amd64-nomultilib/stage3-amd64-nomultilib-<日時>.tar.xz
tar xpf stage3-*.tar.{bz2,xz} -C gentoo --xattrs-include='*.*' --numeric-owner
ベースシステムのインストール
chroot
cp /etc/portage/make.conf gentoo/etc/portage/make.conf
cp --dereference /run/systemd/resolve/resolv.conf gentoo/etc/
cp /etc/locale.gen gentoo/etc/
cp /etc/timezone gentoo/etc/
mkdir gentoo/etc/portage/repos.conf
mkdir gentoo/usr/portage
mount --types proc /proc gentoo/proc
mount --rbind /sys gentoo/sys
mount --make-rslave gentoo/sys
mount --rbind /dev gentoo/dev
mount --make-rslave gentoo/dev
mount --bind /etc/portage/repos.conf gentoo/etc/portage/repos.conf
mount --bind /usr/portage gentoo/usr/portage
chroot gentoo /bin/bash
source /etc/profile
export PS1="(chroot) ${PS1}"
プロファイルの選択
eselect profile list
で表示されたprofileの中から使いたいプロファイルを下記のコマンドで選択
eselect profile set <プロファイル番号>
プロファイルの選択でsystemdを選ばなかった場合は、/etc/portage/make.confのUSEフラグにsystemdを追加すること。
アップデート
emerge -auvDN @world
システム設定
locale-gen
emerge --config sys-libs/timezone-data
env-update && source /etc/profile && export PS1="(chroot) $PS1"
systemctl enable systemd-networkd
systemctl enable systemd-resolved
passwd
ネットワーク設定
コンテナ側を固定IPにして、ホストのブリッジ経由でネットワークに接続する設定です。(DHCPを使用する場合は必要ないかも)
ln -s /dev/null /etc/systemd/network/80-container-host0.network
cat << 'EOF' > /etc/systemd/network/host0.network
[Match]
Name=host0
[Network]
DNS=192.168.1.1
Address=192.168.1.101/24
Gateway=192.168.1.1
EOF
終了処理
exit
umount -l gentoo/dev{/shm,/pts,}
umount -R gentoo/sys
umount gentoo{/proc,/etc/portage/repos.conf,/usr/portage}
起動
systemd-nspawn --boot --link-journal=try-guest --network-veth --network-bridge=br0 --directory=gentoo --machine=gentoo
machinectl
設定
machinectlでコンテナを操作するための設定です。
mv gentoo /var/lib/machine/
cat << 'EOF' > /etc/systemd/nspawn/gentoo.nspawn
[Exec]
Boot=on
[Files]
Bind=/usr/portage
BindReadOnly=/etc/portage/repos.conf
BindReadOnly=/usr/src/linux
TemporaryFileSystem=/tmp
[Network]
VirtualEthernet=yes
Bridge=br0
EOF
コンテナを起動
machinectl start gentoo
コンテナへログイン
machinectl login gentoo
コンテナをシャットダウン
machinectl poweroff gentoo