You are not logged in.

#1 Today 18:49:04

LumieLynn
Member
Registered: Today
Posts: 1

Run commands and shells in non-systemd nspawn guests without setns()

Hi,

I was running Void Linux under systemd-nspawn on my Arch machine and wanted to open a shell from the host as my usual user in Void. Since machinectl shell needs systemd running inside the guest, I started a small guest runtime so I could do that with Void's own init.

The runtime is a static binary that runs inside the guest and listens on a Unix socket. The project also includes a test client that connects to it from the host. The client looks up the container through systemd-machined and reaches the socket through the container leader's entry in /proc, so it can stay in the host's namespaces. There's no need to run D-Bus inside the guest, either.

The test client has an exec subcommand that works much like docker exec, and a shell subcommand that opens a login shell for the guest account you specify. Here's a test I ran from Arch to check that stdout, stderr, and the exit status all made it back from Void.

$ sudo ./lasper-guest-test-client exec --machine voidlinux --user root -- \
    /bin/sh -c 'printf "stdout\n"; printf "stderr\n" >&2; exit 7'
stdout
stderr
$ printf 'client exit=%s\n' "$?"
client exit=7

I also opened a login shell as my regular Void user. Neovim and btop both redrew correctly when I resized the host terminal.

For Boot=yes, I bind-mount the runtime at /lib/systemd/systemd in the guest because nspawn checks that path before trying /sbin/init. It's a bit of a hack, but once the broker is listening, the runtime replaces itself with the guest's own /sbin/init, which then runs as PID 1. Both /lib/systemd/systemd and /usr/lib/systemd/systemd must be absent from the rootfs before you add the mount. The guide shows how to check those paths and how to start the runtime directly with Boot=no.

Source on GitHub
Build and setup guide

If you're also running a non-systemd guest under nspawn on Arch, I'd be interested to hear whether this setup works for you.

Offline

Board footer

Powered by FluxBB