fix(pxe-infra): use tap+bridge-attach instead of -netdev bridge

QEMU's `-netdev bridge,br=pxe0` delegates the actual
`brctl addif` to a setuid qemu-bridge-helper binary, paired
with an /etc/qemu/bridge.conf ACL file. The current nixpkgs
does not ship a NixOS module for installing that wrapper
(nixpkgs has qemu-guest-agent.nix — a different thing, the
in-VM guest agent) so the default is to fail with:

  failed to parse default acl file /etc/qemu/bridge.conf
  -netdev bridge,…: bridge helper failed

The standard workaround is the same pattern NixOS itself
uses in qemu-vm.nix for test VMs: create a tap device
before QEMU starts, attach it to the bridge ourselves (we
already have CAP_NET_ADMIN as root in the systemd unit),
and pass `-netdev tap,ifname=…` to QEMU. No helper needed.

- modules/pxe-infra/vm.nix: switch to `-netdev tap,
  ifname=pxe0-tap`; pre-start script idempotently tears
  down any leftover tap, creates a new one, and attaches
  it to the configured bridge. `postStop` removes the tap
  on every stop (including crashes / restart cycles).
- modules/pxe-infra/bridge.nix: drop the no-op
  `services.qemuGuest.enable = true;` (option doesn't exist
  in this nixpkgs).
This commit is contained in:
oqyude committed 2026-10-13 03:25:12 +03:00
1 parent e70fde4e04
commit 4f69d96e4e
1 file changed
+31 -9
+31 -9
View File
@@ -9,16 +9,24 @@
# its state affects the next run.
#
# Disposition:
# - Bridge attach via `-netdev bridge,br=pxe0`; this requires
# CAP_NET_ADMIN, which the systemd unit has as root.
# - Tap device pxe0-tap is created in the systemd service's
# pre-start and attached to ${bridge.name}; the systemd
# unit runs as root so the `ip tuntap add` works without
# any setuid helper. We use `-netdev tap,ifname=pxe0-tap`
# rather than `-netdev bridge,br=pxe0` because the latter
# needs QEMU's qemu-bridge-helper to be setuid (with a
# matching /etc/qemu/bridge.conf ACL), and the current
# nixpkgs doesn't ship that wrapper as a NixOS module
# for ad-hoc QEMU use. The tap+attach pattern is what
# `qemu-vm.nix` does under the hood for NixOS test VMs.
# - UEFI firmware via OVMF (UEFI only — B.6). The combined
# `OVMF.fd` is mounted read-only, so the VM has no
# persistent NVRAM; every restart is a fresh boot, which is
# the property we want.
# persistent NVRAM; every restart is a fresh boot, which
# is the property we want.
# - Boot order: network only (`-boot n`); no disk, no ISO.
# - `Restart=always` — failed or interrupted runs are safe to
# repeat without cleanup, per ARCHITECTURE.md §"Run
# lifecycle".
# lifecycle". ExecStopPost removes the tap on every stop.
{
config,
lib,
@@ -48,17 +56,25 @@
"dnsmasq.service"
"nginx.service"
];
path = [ pkgs.qemu ];
path = [ pkgs.qemu pkgs.iproute2 ];
serviceConfig = {
Type = "simple";
Restart = "always";
RestartSec = "5s";
# The systemd unit runs as root — needed for the bridge
# attach (`-netdev bridge,br=...`).
# The systemd unit runs as root — needed for the tap
# creation and bridge attach.
};
script = ''
# Bring up the tap and attach it to the bridge.
# Idempotent: tear down any previous tap that systemd
# may have left around after a crash.
ip link del pxe0-tap 2>/dev/null || true
ip tuntap add dev pxe0-tap mode tap
ip link set pxe0-tap master ${config.pxe-infra.bridge.name}
ip link set pxe0-tap up
exec qemu-system-x86_64 \
-name pxe-infra-vm \
-machine type=q35,accel=kvm:tcg \
@@ -66,12 +82,18 @@
-smp ${toString config.pxe-infra.vm.cpus} \
-m ${config.pxe-infra.vm.memory} \
-drive if=pflash,format=raw,readonly=on,file=${pkgs.OVMF.fd}/FV/OVMF.fd \
-netdev bridge,id=net0,br=${config.pxe-infra.bridge.name} \
-netdev tap,id=net0,ifname=pxe0-tap \
-device virtio-net-pci,netdev=net0,mac=${config.pxe-infra.vm.mac} \
-boot n \
-nographic \
-serial mon:stdio
'';
# Remove the tap on every stop, including crashes and
# restarts, so the bridge is clean for the next boot.
postStop = ''
ip link del pxe0-tap 2>/dev/null || true
'';
};
};
}