CentOS7 に debootstrap をインストール
sudo yum install debootstrap.noarch
debootstrap コマンドで /var/lib/machines/ubuntu1804 に必要最低限の ubuntu1804 をインストールする
sudo debootstrap --variant=minbase --include=systemd,dbus,locales,language-pack-ja,sudo,vim --arch amd64 bionic /var/lib/machines/ubuntu1804 http://jp.archive.ubuntu.com/ubuntu
いったん ubuntu1804 コンテナを起動してみる
sudo systemd-nspawn -D /var/lib/machines/ubuntu1804
ubuntu1804 コンテナ内で /etc/hostname を修正
echo 'ubuntu1804' > /etc/hostname
ubuntu1804 コンテナ内で /etc/hosts を修正
(echo '127.0.0.1 localhost'
echo '127.0.1.1 ubuntu1804'
) > /etc/hosts
ubuntu1804 コンテナ内で locale を修正
update-locale LANG=ja_JP.UTF-8
resolv.confは静的に変更する
ls -la /etc/resolv.conf
lrwxrwxrwx 1 root root 39 Jan 31 04:37 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf
unlink /etc/resolv.conf
echo 'nameserver 8.8.8.8' > /etc/resolv.conf
ubuntu1804 コンテナ内でログイン用のユーザを作成しパスワードを設定して終了する
useradd -m -r user -s /bin/bash
passwd user
gpasswd -a user sudo
exit
正しくブートするかコマンドラインから ubuntu1804 コンテナ起動させる
sudo systemd-nspawn -b -D /var/lib/machines/ubuntu1804
正しく起動すれば、「Ctrl+]」 x 3 で終了する
Ctrl+]
Ctrl+]
Ctrl+]
CentOS7側のホスト側で ubuntu1804 コンテナの起動設定を行う
sudo systemctl enable systemd-nspawn@ubuntu1804.service
Created symlink from /etc/systemd/system/machines.target.wants/systemd-nspawn@ubuntu1804.service to /usr/lib/systemd/system/systemd-nspawn@.service.
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
[Unit]
Description=Container %I
Documentation=man:systemd-nspawn(1)
PartOf=machines.target
Before=machines.target
[Service]
ExecStart=/usr/bin/systemd-nspawn --quiet --keep-unit --boot --link-journal=try-guest --network-veth --machine=%I
KillMode=mixed
Type=notify
RestartForceExitStatus=133
SuccessExitStatus=133
Slice=machine.slice
Delegate=yes
[Install]
WantedBy=machines.target
そのままだと仮想ネットワークで外にでられないため、ホスト機を同じネットワークで動作するように変更
(echo '[Service]'
echo 'ExecStart='
echo 'ExecStart=/usr/bin/systemd-nspawn --quiet --keep-unit --boot --link-journal=try-guest --machine=%I'
) | sudo SYSTEMD_EDITOR=tee systemctl edit systemd-nspawn@"ubuntu1804".service
systemctl daemon-reload
[Service]
ExecStart=
ExecStart=/usr/bin/systemd-nspawn --quiet --keep-unit --boot --link-journal=try-guest --machine=%I
macinectl コマンドで起動させ、ログインしてみる。
sudo machinectl start ubuntu1804
sudo machinectl login ubuntu1804
ログインできたら成功!
$ pstree -ap
systemd,1
├─agetty,25 -o -p -- \\u --noclear --keep-baud console 115200,38400,9600 vt220
├─dbus-daemon,23 --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
├─login,103 -p --
│ └─bash,116
│ └─pstree,121 -ap
├─systemd-journal,13
├─systemd-logind,22
└─systemd-resolve,19