From 5973aa74fd307e4d6bbb69420bc8a7f6e8783a2d Mon Sep 17 00:00:00 2001 From: oqyude Date: Tue, 13 Oct 2026 02:42:49 +0300 Subject: [PATCH] =?UTF-8?q?feat(pxe-infra):=20add=20QEMU/KVM=20guest=20mod?= =?UTF-8?q?ule=20=E2=80=94=20L4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Wires the L4 guest VM from docs/ARCHITECTURE.md into the homelab flake. The guest is the consumer of the L2 (bridge + DHCP) and L3 (TFTP + HTTP) plumbing committed earlier; it PXE-boots through the private bridge with a fixed MAC, no disk, no persistent NVRAM. - modules/options.nix: new options.pxe-infra.vm - enable: master switch (default false; turning it on spins up the VM on every boot, which the user may want to defer until an artifact is staged) - mac: guest NIC MAC (default 52:54:00:12:34:56; must also appear in pxe-infra.dhcp.guests when authoritative=false) - cpus: vCPU count - memory: RAM size in QEMU syntax (e.g. "2G") - modules/pxe-infra/vm.nix: new file. A systemd service running qemu-system-x86_64 with: - machine type=q35 + accel=kvm:tcg (TCG fallback if KVM is not available on the host) - OVMF.fd as a read-only pflash drive (UEFI only — B.6); the combined fd has no NVRAM region, so every restart is a fresh boot, which is the property ARCHITECTURE.md §L4 wants - netdev=bridge,br=pxe0 attaching the guest's virtio-net to the L2 bridge, with the fixed MAC - -boot n: network only, no disk, no ISO - Restart=always, RestartSec=5s — failed/interrupted runs are safe to repeat (per ARCHITECTURE.md "Run lifecycle") - modules/pxe-infra/default.nix: imports the new vm.nix. Refs: docs/ARCHITECTURE.md §L4, docs/OPEN-QUESTIONS.md "Resolved (2026-10-13)" B.6 (UEFI only). --- modules/options.nix | 48 +++++++++++++++++++++++ modules/pxe-infra/default.nix | 1 + modules/pxe-infra/vm.nix | 72 +++++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 modules/pxe-infra/vm.nix diff --git a/modules/options.nix b/modules/options.nix index 01ea3d6..416e6f6 100644 --- a/modules/options.nix +++ b/modules/options.nix @@ -286,5 +286,53 @@ ''; }; }; + + # VM: a single QEMU/KVM guest that PXE-boots through the + # bridge. Layer L4 of docs/ARCHITECTURE.md. The guest is + # intentionally disposable — no persistent disk, no NVRAM + # persistence, restart on crash — so every run starts from + # the same state, which is the rig's whole reason to exist. + vm = { + enable = lib.mkOption { + type = lib.types.bool; + default = false; + description = '' + Whether to run a QEMU/KVM guest that PXE-boots through + the bridge. Disabled by default: enabling it spins up + the VM on boot, which the user may want to defer until + the artifact is staged. + ''; + }; + mac = lib.mkOption { + type = lib.types.str; + default = "52:54:00:12:34:56"; + example = "52:54:00:12:34:56"; + description = '' + MAC address of the guest's NIC. Choose from the + 52:54:00:XX:XX:XX QEMU/OUI range. The same MAC must + appear in `dhcp.guests` when `dhcp.authoritative` is + false, otherwise the VM gets no lease. + ''; + }; + cpus = lib.mkOption { + type = lib.types.int; + default = 2; + example = 4; + description = '' + Number of virtual CPUs. Memory and CPU together cap + which tests can run on the rig (Phase 5 in + RUNNER-DESIGN.md). + ''; + }; + memory = lib.mkOption { + type = lib.types.str; + default = "2G"; + example = "4G"; + description = '' + RAM size in QEMU syntax (e.g. `2G`, `512M`). Caps the + testable memory in Phase 3 (RAM burnin). + ''; + }; + }; }; } diff --git a/modules/pxe-infra/default.nix b/modules/pxe-infra/default.nix index 50a10ef..42dc7ff 100644 --- a/modules/pxe-infra/default.nix +++ b/modules/pxe-infra/default.nix @@ -17,6 +17,7 @@ ./bridge.nix ./dhcp.nix ./http.nix + ./vm.nix ]; config = lib.mkIf config.pxe-infra.enable { diff --git a/modules/pxe-infra/vm.nix b/modules/pxe-infra/vm.nix new file mode 100644 index 0000000..f5a5a93 --- /dev/null +++ b/modules/pxe-infra/vm.nix @@ -0,0 +1,72 @@ +# pxe-infra/vm — QEMU/KVM guest that PXE-boots through the +# private bridge. +# +# Per docs/ARCHITECTURE.md §L4: the guest is intentionally +# disposable; every run starts from the same state, with no +# persistent disk. The boot source is the PXE services on L2/L3 +# (dnsmasq for DHCP+TFTP, nginx for HTTP), so the VM is +# essentially a fancier ipxe-compatible client: nothing about +# 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. +# - 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. +# - 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". +{ + config, + lib, + pkgs, + ... +}: +{ + config = lib.mkIf (config.pxe-infra.enable && config.pxe-infra.vm.enable) { + environment.systemPackages = [ pkgs.qemu ]; + + systemd.services.pxe-infra-vm = { + description = "pxe-infra: PXE-boot QEMU/KVM guest (L4)"; + wantedBy = [ "multi-user.target" ]; + after = [ + "pxe-infra-bridge.service" + "dnsmasq.service" + "nginx.service" + "network-online.target" + ]; + wants = [ + "pxe-infra-bridge.service" + "dnsmasq.service" + "nginx.service" + ]; + path = [ pkgs.qemu ]; + + serviceConfig = { + Type = "simple"; + Restart = "always"; + RestartSec = "5s"; + # The systemd unit runs as root — needed for the bridge + # attach (`-netdev bridge,br=...`). + }; + + script = '' + exec qemu-system-x86_64 \ + -name pxe-infra-vm \ + -machine type=q35,accel=kvm:tcg \ + -cpu host \ + -smp ${toString config.pxe-infra.vm.cpus} \ + -m ${config.pxe-infra.vm.memory} \ + -drive if=pflash,format=raw,readonly=on,file=${pkgs.OVMF.fd} \ + -netdev bridge,id=net0,br=${config.pxe-infra.bridge.name} \ + -device virtio-net-pci,netdev=net0,mac=${config.pxe-infra.vm.mac} \ + -boot n \ + -nographic \ + -serial mon:stdio + ''; + }; + }; +}