From b083c03d5e3e3b7c44f4ba90243c5f12a03952b8 Mon Sep 17 00:00:00 2001 From: autumn Date: Mon, 14 Sep 2026 01:47:43 +0000 Subject: [PATCH] bug: fix kvm device not being properly forwarded Co-authored-by: Autumn Liu Reviewed-on: https://git.floweytf.com/server/server-config/pulls/9 Co-authored-by: autumn --- dsl.nix | 22 ++++++++++++++-------- tests/server-n0/integration.py | 19 +++++++++++++++++++ 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/dsl.nix b/dsl.nix index 4745527..2af94fe 100644 --- a/dsl.nix +++ b/dsl.nix @@ -222,13 +222,15 @@ rec { : Bind mounts, keyed by mount point. `resources.devices` - : Host device nodes to expose, as paths, added to the unit's - `DeviceAllow=`. nspawn populates a handful of privileged devices - (`/dev/kvm`, `/dev/net/tun`, ...) into the container's own `/dev` once - the cgroup allows it, and silently skips ones the host doesn't have - - no `resources.fs` bind mount needed. Nodes keep their host ownership, - so a consumer inside needs `isolation.privateUsers = "identity"` for - the owning gid to mean anything. + : Host device nodes to expose, as paths. Bound into the container at the + same path and added to the unit's `DeviceAllow=` - the cgroup + permission alone doesn't get you a working node (nothing can `mknod` a + device inside a user namespace), and the bind alone leaves the cgroup + denying `open()`, so both are needed. The bind is unconditional, so + the device must actually exist on whatever host runs the container. + Nodes keep their host ownership, so a consumer inside needs + `isolation.privateUsers = "identity"` for the owning gid to mean + anything. `isolation.nixDaemon` : Whether the Nix daemon and CLI are available inside the container. @@ -295,6 +297,10 @@ rec { + lib.optionalString useIdmap ":idmap" ) allBindMounts; bindMountHostPaths = map (bm: bm.hostPath) (lib.attrValues allBindMounts); + # DeviceAllow= alone only grants cgroup permission - nspawn can't + # mknod a device node inside a user namespace, so the node still + # needs an explicit bind from the host. + deviceFlags = map (d: "--bind=${d}") devices; inherit (deriveIdentity ifPrefix.nspawn name) iface mac @@ -381,7 +387,7 @@ rec { nixosContainer = { inherit autoStart privateUsers; bindMounts = { }; - extraFlags = bindMountExtraFlags; + extraFlags = bindMountExtraFlags ++ deviceFlags; privateNetwork = true; extraVeths = linkVeths; config = diff --git a/tests/server-n0/integration.py b/tests/server-n0/integration.py index b5f95c8..1c8c41e 100644 --- a/tests/server-n0/integration.py +++ b/tests/server-n0/integration.py @@ -333,9 +333,28 @@ with subtest("(functional): published ports follow the container's lifetime"): with subtest("(functional): declared devices are usable in-container"): for name, devices in DEVICES.items(): for dev in devices: + # opened, not stat'd: without DeviceAllow= the node is still visible server.succeed(ct_cmd(name, f"exec 3<> {dev}")) +with subtest("(functional): /dev/kvm actually initializes, not just opens"): + # a wrong-gid or unmapped node still opens but fails the ioctls qemu + # needs, so boot with no disk (never completes) and kill it - the + # failure mode this catches is qemu refusing to start at all. + for name, devices in DEVICES.items(): + if "/dev/kvm" not in devices: + continue + out = server.succeed( + ct_cmd( + name, + f"{TIMEOUT} 3 qemu-system-x86_64 -enable-kvm -m 256 -nographic -no-reboot " + ">/tmp/qemu.out 2>&1; cat /tmp/qemu.out", + ) + ) + assert "Permission denied" not in out, f"{name}: qemu couldn't use /dev/kvm:\n{out}" + assert "Could not access KVM" not in out, f"{name}: qemu couldn't use /dev/kvm:\n{out}" + + with subtest("(isolation): devices reach only the container that declared them"): for name in enterable: for dev in {d for ds in DEVICES.values() for d in ds} - set(DEVICES.get(name, [])):