tags: - systemd - nspawn categories: - informational comments: true

date: 2022-07-01 00:00:00

Create a Debian systemd container in Ubuntu using nspawn

Backgrpund

Filesystem

Process IDs (PIDs)

User IDs (UIDs)

Network interfaces

Controlling the ‘namespaces’

Issue with single process containers

Create a Debian systemd nspawn container

apt install debootstrap systemd-container bridge-utils
cd /var/lib/machines
mkdir -p /var/lib/machines/helloworld
chown root:root /var/lib/machines
chmod 700 /var/lib/machines

debootstrap stable /var/lib/machines/helloworld http://deb.debian.org/debian/

Login and accessing

systemd-nspawn -UM helloworld
password
useradd -s /bin/bash -m hello1
password hello1

systemd-nspawn -UMb helloworld

echo 'auto host0' >> /etc/network/interfaces
echo 'iface host0 inet dhcp' >> /etc/network/interfaces
apt update

An overview of systemd’s container commands

systemd-nspawn

machinectl

[Service]
ExecStart=/usr/bin/systemd-nspawn --quiet --keep-unit --boot --link-journal=try-guest --network-veth -U --settings=override --machine=%i

/etc/systemd/nspawn

Enable/manage container via machinectl

To start a container in the background: machinectl start helloworld

To get status information for a running container: machinectl status helloworld

To obtain a login shell on a running container (requires dbus on both host and container): machinectl login helloworld

To leave the session: press Ctrl + ] 3 times within one second

To shut down a running container: machinectl stop helloworld

To configure a container to boot every time the system boots: machinectl enable helloworld machinectl disable helloworld

Error due to Debian keyring

    W: Cannot check Release signature; keyring file not available /usr/share/keyrings/debian-archive-keyring.gpg
    I: Retrieving InRelease
    I: Retrieving Release
    E: Failed getting release file http://deb.debian.org/debian/dists/stable/Release

Installing the debian keyring did not help

apt install debian-keyring debian-archive-keyring

Fixing the Debian keyring issue for debootstrap

wget https://ftp-master.debian.org/keys/archive-key-10.asc
wget https://ftp-master.debian.org/keys/archive-key-10-security.asc
mkdir /usr/share/keyrings/
gpg --no-default-keyring --keyring=/usr/share/keyrings/debian-archive-keyring.gpg     --import archive-key-10.asc
gpg --no-default-keyring --keyring=/usr/share/keyrings/debian-archive-keyring.gpg     --import archive-key-10-security.asc
ls -lF /usr/share/keyrings/debian-archive-keyring.gpg

debootstrap stable /var/lib/machines/helloworld http://deb.debian.org/debian/

Reference

https://medium.com/@huljar/setting-up-containers-with-systemd-nspawn-b719cff0fb8d

https://pub.nethence.com/xen/debootstrap

https://wiki.archlinux.org/title/systemd-nspawn