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

From Gentoo Wiki
Jump to:navigation Jump to:search

WIP

Unpack a stage3 into a directory which will become an nspawn container in /var/lib/machines. As bug #821568 is now fixed, machinectl pull-tar can be used.

root #gpg --no-default-keyring --keyring /etc/systemd/import-pubring.gpg --import /usr/share/openpgp-keys/gentoo-release.asc
root #machinectl pull-tar https://bouncer.gentoo.org/fetch/root/all/releases/amd64/autobuilds/$(curl -sL https://bouncer.gentoo.org/fetch/root/all/releases/amd64/autobuilds/latest-stage3-amd64-systemd.txt | 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
[Files]
Bind=/var/cache/distfiles
Bind=/var/cache/binpkgs
BindReadOnly=/var/db/repos

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

Usage

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 ~ #