systemd-nspawn command examples

systemd-nspawn command examples

systemd-nspawn – Spawn a namespace container for debugging, testing and building

Example 1. Download a Fedora image and start a shell in it

# machinectl pull-raw --verify=no http://ftp.halifax.rwth-aachen.de/fedora/linux/releases/21/Cloud/Images/x86_64/Fedora-Cloud-Base-20141203-21.x86_64.raw.xz
# systemd-nspawn -M Fedora-Cloud-Base-20141203-21

This downloads an image using machinectl(1) and opens a shell in it.

Example 2. Build and boot a minimal Fedora distribution in a container

# yum -y --releasever=21 --nogpg --installroot=/srv/mycontainer --disablerepo='*' --enablerepo=fedora install systemd passwd yum fedora-release vim-minimal
# systemd-nspawn -bD /srv/mycontainer

This installs a minimal Fedora distribution into the directory /srv/mycontainer/ and then boots an OS in a namespace container in it.

Example 3. Spawn a shell in a container of a minimal Debian unstable distribution

# debootstrap --arch=amd64 unstable ~/debian-tree/
# systemd-nspawn -D ~/debian-tree/

This installs a minimal Debian unstable distribution into the directory ~/debian-tree/ and then spawns a shell in a namespace container in it.

Example 4. Boot a minimal Arch Linux distribution in a container

# pacstrap -c -d ~/arch-tree/ base
# systemd-nspawn -bD ~/arch-tree/

This installs a mimimal Arch Linux distribution into the directory ~/arch-tree/ and then boots an OS in a namespace container in it.

Example 5. Boot into an ephemeral “btrfs” snapshot of the host system

# systemd-nspawn -D / -xb

This runs a copy of the host system in a “btrfs” snapshot which is removed immediately when the container exits. All file system changes made during runtime will be lost on shutdown, hence.

Example 6. Run a container with SELinux sandbox security contexts

# chcon system_u:object_r:svirt_sandbox_file_t:s0:c0,c1 -R /srv/container
# systemd-nspawn -L system_u:object_r:svirt_sandbox_file_t:s0:c0,c1 -Z system_u:system_r:svirt_lxc_net_t:s0:c0,c1 -D /srv/container /bin/sh


Leave a Reply

Your email address will not be published. Required fields are marked *