-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optional systemd-nspawn support #8823
base: master
Are you sure you want to change the base?
Conversation
Can you give a link to some very simple quick tutorial with using this functionality? |
I'm not aware of a single good howto for using systemd-nspawn. It's mostly blogs about how the author solved a specific issue. My usage is from reading the manpage (https://www.freedesktop.org/software/systemd/man/latest/systemd-nspawn.html) and Arch's wiki for troubleshooting using as a service (https://wiki.archlinux.org/title/systemd-nspawn). The overview for getting an OS container to start is: Have a directory with the OS's filesystem in it (systemd-nspawn can also work with raw disk images and check if they have a correct cryptographic signature but I haven't tried either.) Here's an example of how to get Alpine Linux up and running: Place to hold the container:
Download the mini root filesystem from alpinelinux.org (this is aarch64):
Prepare the container for booting:
Set root's passwd Update the package repo: Install alpine's meta package for minimal install: Add nano (unless you like vi): Configure timezone (optional)
Configure login consoles to disable tty and add console (the commented tty and console line below):
Permit root logins (the pts/# lines below):
Enable boot services:
Enable shutdown services
Exit and boot the container
If everything went to plan, you should have a booted Alpine Linux install to add more packages / services / etc. It'll stay booted until you shutdown ( To have it keep running, you'll want it to be a service, which is where the symlinks to /var/lib/machines come in. (Symlinks need to be full paths if you're not using the helper service.) Something like I'm using Gentoo with systemd on LE which does have machinectl working as expected to interact with it. Installing it was less work than the above (or I'm just more used to it) - unpack the stage3 tarball and do the system configuration steps form the Gentoo handbook (no kernel config, no hard drive partitioning, etc). The Arch wiki linked above has instructions on creating the filesystem for Arch, Debian/Ubuntu and Fedora/Alma (these assume the use of Arch / full distro). There are scripts here https://gist.github.com/sfan5/52aa53f5dca06ac3af30455b203d3404 that are meant to be run on another system to prepare an alpine, arch or ubuntu filesystem directory. I've referred to it for image setup steps before, but haven't actually run the scripts before.
|
Gentoo's wiki on systemd-nspawn is pointing to https://nspawn.org/images/ as premade images suitable for using with systemd-nspawn. |
Sounds useful to me. Not sure yet how to place it along docker. |
Additional notes/observations: Alpine wasn't a great base as it's not using systemd, which means machinectl couldn't control it from LE. It was useful for building a debian container, however. (Fun fact: the RPi5 needs about 4 hrs to build an RPi5 image on an sd card; measured before recent speedups.) The containers can have certain configuration settings that would be used on the CLI be instead stored in an *.nspawn file for use by machinectl, where * is the name of the container. *.nspawn files are stored alongside the container ("untrusted") or in /etc/systemd/nspawn ("trusted"). Certain settings are only available from trusted *.nspawn files. This affects using bind mounts, getting shared networking, and running privileged containers. (pretty much every option in https://www.freedesktop.org/software/systemd/man/latest/systemd.nspawn.html that mentions "privilege".) By default, when launching with machinectl, containers have private networking. In our use, this means no networking. With private networking disabled, containers share the network interface with the host. If a privileged port is desired (rsync in my case), private users must be disabled too (this results in a privileged container).
|
Signed-off-by: Ian Leonard <[email protected]>
Signed-off-by: Ian Leonard <[email protected]>
Signed-off-by: Ian Leonard <[email protected]>
fb11ab7
to
2b659f1
Compare
Another note is being able to run programs within containers with |
This adds systemd-nspawn and machined to the image, locked behind the NSPAWN_SUPPORT distribution option. It's not enabled by default.
Systemd-nspawn is systemd's chroot, container, and virtual machine manager. This has enough for the first two; VM support is unknown and not my interest. It can run whole OSes, or single applications. I've only tested running an OS. My understanding is that it can run images made by docker (via --oci-bundle=), but I haven't tried it.
/var/lib/machines is the recommended location containers are expected. This has a helper service to get that populated based on the contents of /storage/containers.
systemd-nspawn, machinectl, and systemd-machined combined are somewhere around 600KB uncompressed.
If you're going to try it, and want containers to start on boot,
machines.target
needs to be enabled first (the machine too).If using a full OS, machinectl's login defaults the terminal to vt220, so no color on the console. Add an environment override in the container to set TERM to xterm to get color back. Alternatively, use machinectl shell or systemd-nspawn directly to avoid this.