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 + ''; + }; + }; +}