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 (this can probably be done by machinectl in the future: bug #821568).

root #curl -sLO$(curl -sL | tail -1 | awk '{print $1}')
root #mkdir -p /var/lib/machines/stable
root #tar xf stage3-amd64-systemd*.tar.xz -C /var/lib/machines/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 ~ #