From 949bfca6bdedc4d3ad7403df1b2b3dff5c359fe0 Mon Sep 17 00:00:00 2001 From: Autumn Liu Date: Sun, 13 Sep 2026 20:18:24 -0500 Subject: [PATCH] actually bind /dev/kvm into the container, and test it initializes DeviceAllow= only grants cgroup permission to open a device node - it doesn't create one, and nspawn can't mknod a device inside a user namespace. The node needs an explicit bind from the host as well, so both are required together. Also add a test that actually boots qemu-system-x86_64 -enable-kvm (no disk, killed by timeout) and checks for "Permission denied" / "Could not access KVM" in its output, rather than just checking the node opens - a wrong-gid or unmapped node still opens but fails the ioctls qemu needs. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01Dzy7tEKLCFFXD96LmR8we3 --- dsl.nix | 22 ++++++++++++++-------- tests/server-n0/integration.py | 23 ++++++++++++++++++----- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/dsl.nix b/dsl.nix index c3ac837..c887446 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 edd7d41..1c8c41e 100644 --- a/tests/server-n0/integration.py +++ b/tests/server-n0/integration.py @@ -333,15 +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: - if server.execute(f"test -e {dev}")[0] != 0: - # host lacks the device (e.g. /dev/kvm without nested - # virtualization, as in this test's own VM) - nspawn skips - # populating it in-container, so there's nothing to check. - continue # 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, [])):