Table of Contents

systemd-nspawn CLI Commands Cheet Sheat



machinectl commands

Commands Explanation
machinectl -h help
machinectl list
machinectl status GUEST
machinectl poweroff GUEST


How to use systemd-nspawn

disable audit

adding audit=0

# vi /etc/default/grub

GRUB_CMDLINE_LINUX="rd.lvm.lv=centos/swap vconsole.font=latarcyrheb-sun16 vconsole.keymap=jp106 rd.lvm.lv=centos/root crashkernel=auto audit=0 rhgb quiet"

# grub2-mkconfig -o /boot/grub2/grub.cfg 
# reboot


How to create repo

# mkdir ~/yum; cd ~/yum
# mount -o loop OracleLinux-R7-U1-Server-x86_64-dvd.iso /mnt
# cp -Rp /mnt /share/OS/Linux/OEL/x64/7.1 .
# umount /mnt
# vi /etc/yum.repos.d/pkg.repo
[OL7.1]
name=OL7.1
gpgcheck=0
enabled=1
baseurl=file:///root/yum/7.1/
# yum clean all


How to create container

Sample
# yum -y --nogpgcheck --installroot=/var/lib/machines/container1 --disablerepo='*' --enablerepo='dvdmedia' install systemd yum vim bash-completion passwd
# yum -y --nogpg --installroot=/var/lib/container/mycontainer --disablerepo='*' --enablerepo=OL7.1 install systemd passwd yum oraclelinux-release vim-minimal
# yum -y --releasever=7 --nogpg --installroot=/srv/mycontainer --disablerepo='*' --enablere po=base install systemd passwd yum vim-minimal


How to start container

# systemd-nspawn -D /guest/container1 --boot
# systemd-nspawn -D /var/lib/container/mycontainer
# systemd-nspawn -D /var/lib/machine/container --boot --bind=/mnt
# systemd-nspawn -D /var/lib/machines/container1 --bind=/mnt:/mnt

How to create service

# vi /etc/systemd/system/mycontainer.service
[Service]
ExecStart=/usr/bin/systemd-nspawn -bD /var/lib/container/mycontainer
KillMode=process

# systemctl daemon-reload
# systemctl start mycontainer
# systemctl status mycontainer
# machinectl list


Networking

Bridge

Network Configuration

# cat /etc/sysconfig/network-scripts/ifcfg-br0

# cat /var/lib/machines/container/etc/sysconfig/network-scripts/ifcfg-virbrhost0
HWADDR=CE:6A:41:08:FA:80
TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME=virbrhost0
UUID=514a767e-cf78-495b-93fc-8a568f378689
ONBOOT=yes

Start Container

systemd-nspawn  --network-veth --network-bridge=br0 --bind /var/cache/yum -D /var/lib/machines/container -b


Import & Export

Commands Explanation
machinectl clone GUEST NEW-GUEST
# machinectl export-tar CONTAINER-NAME  EXPORT-NAME
# machinectl import-tar EXPORT-NAME NEW-CONTAINER