User:Ajak/systemd-nspawn for Development and Update Automation

From Gentoo Wiki
Jump to:navigation Jump to:search


Unpack a stage3 into a directory which will become an nspawn container in /var/lib/machines, using machinectl pull-tar.

root #gpg --no-default-keyring --keyring /etc/systemd/import-pubring.gpg --import /usr/share/openpgp-keys/gentoo-release.asc
root #machinectl pull-tar$(curl -sL | tail -1 | awk '{print $1}') stable

Create an nspawn configuration for the machine in /etc/systemd/nspawn/stable.nspawn (the filename should be the same as the directory name created in the previous step).

Start the container manually once to disable the root password and the systemd-firstboot service.

root #systemd-nspawn -D /var/lib/machines/stable
Spawning container stable on /var/lib/machines/stable.
Press ^] three times within 1s to kill container.
stable ~ #systemctl disable systemd-firstboot
stable ~ #passwd -d root
passwd: password expiry information changed.
CODE /etc/systemd/nspawn/stable.nspawn

Start and enable the container to start at boot (but enabling doesn't seem to work for me, haven't investigated).

root #machinectl enable stable
root #machinectl start stable


To use the container, you can simply login via the machinectl facility:

root #machinectl shell stable
Connected to machine stable. Press ^] three times within 1s to exit session.
stable ~ #

Any changes made inside of the container will be persistent. When doing things like ebuild development, this is not necessarily desirable.

To use the container ephemerally (such that changes made while the container is in use are lost afterwards), manually using systemd-nspawn is necessary:

root #systemd-nspawn -xD /var/lib/machines/stable/ --bind=/var/cache/distfiles --bind=/var/cache/binpkgs --bind-ro=/var/db/repos
Spawning container stable-acb079b0961116f0 on /var/lib/machines/.#machine.stable61cd9f261a50cc47.
Press ^] three times within 1s to kill container.
stable-acb079b0961116f0 ~ #