From Mageia wiki
Revision as of 16:33, 20 May 2019 by Papoteur (talk | contribs) (Add Packaging category)
Jump to: navigation, search

The systemd-nspawn utility is a way to run containers on Linux. It's not a method you want in a production environment, or something that you want for daily usage.

But it's a great way to learn more about container technology, and can be used to test and develop software, or you can use it to create packages. You can think of systemd-nspawn as a sort of chroot on steriods.

This document is meant for packagers, developers and everyone that wants to learn more about container technology.

Note: all the commands in this document should be executed as root:

su -

Container filesystem

The first step is to create a directory for the root filesystems of the containers:

mkdir /container/

Let's make a Mageia container directory:

mkdir /container/mageia6

Initialize the rpm database for Mageia:

rpm --rebuilddb --root=/container/mageia6

Install some base rpm's. Installing these ensures that you can install the packages for the Mageia 6 release. Officially you can skip this step, but it makes the configuration somewhat easier. You can ignore warnings.

rpm --root=/container/mageia6/ --nodeps -ivh \
http://free.nchc.org.tw/mageia/distrib/6/x86_64/media/core/release/mageia-release-Default-6-4.mga6.x86_64.rpm

rpm --root=/container/mageia6/ --nodeps -ivh \
http://free.nchc.org.tw/mageia/distrib/6/x86_64/media/core/release/mageia-release-common-6-4.mga6.x86_64.rpm

rpm --root=/container/mageia6/ --nodeps -ivh \
http://free.nchc.org.tw/mageia/distrib/6/x86_64/media/core/release/lsb-release-2.0-52.mga6.noarch.rpm

Configure the Mageia repositories:

urpmi.addmedia --distrib http://free.nchc.org.tw/mageia/distrib/6/x86_64/ --urpmi-root /container/mageia6

Of course you can use other mirrors!

Now you can start a minimal installation:

urpmi basesystem-minimal urpmi locales locales-en systemd --auto --no-recommends --urpmi-root /container/mageia6/ --root /container/mageia6/

Root password

Before you can use the root filesystem, you have set a root password. Therefore you need to change your root filesystem into the container.

chroot /container/mageia6

Now you can set the root password:

passwd

To enable the root user to login, add to the file /etc/securetty the following line:

console

Exit the chroot environment:

exit

First container

We are ready to launch the first container, based on the created root filesystem:

systemd-nspawn --boot --directory=/container/mageia6

Disable the network daemon:

chkconfig network off

Logout, and exit the container by pressing ctrl+] 3 times

machinectl

To make the management of the container easier, create a systemd-unit file: /etc/systemd/system/var-lib-machines.mount with the following content:

[Unit]
Description = systemd containers filesystem

[Mount]
Where = /var/lib/machines
What = /container
Options = bind

[Install]
WantedBy = local-fs.target

Start and enable this unit at boot time:

systemctl start var-lib-machines.mount
systemctl enable var-lib-machines.mount

To show the Mageia6 'image', execute:

machinectl list-images

Start the image:

machinectl start mageia6

To login:

machinectl shell mageia6

Exit the shell by pressing ctrl+] 3 times, and view the status:

machinectl status mageia6

Now you can shutdown the container until next time you need it:

machinectl poweroff mageia6