From d7e46a4e89e738e13070581e21294df3278f4403 Mon Sep 17 00:00:00 2001 From: oqyude Date: Tue, 13 Oct 2026 02:46:07 +0300 Subject: [PATCH] =?UTF-8?q?feat(pxe-infra):=20stage=20iPXE=20EFI=20+=20boo?= =?UTF-8?q?t.ipxe=20=E2=80=94=20completes=20the=20boot=20chain?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Wires the missing piece between the L3 services and the L4 VM: the firmware does DHCP+TFTP-fetch of an iPXE binary, then iPXE fetches a static boot.ipxe script over HTTP, which sanboots the alpine-worker raw image. Without this, the VM has a DHCP lease but no filename to TFTP-fetch, so the chain stops at the firmware. - modules/options.nix: new options.pxe-infra.bootloader - enable: master switch (default true) - ipxeBinary: filename under tftpboot/ (default "ipxe.efi", the universal UEFI binary from pkgs.ipxe; "snp.efi" is a smaller virtio-net-only alternative) - modules/pxe-infra/ipxe.nix: new file. - environment.etc drops pkgs.ipxe's binary under /etc/static/pxe-infra/tftpboot/, plus a static boot.ipxe script under /etc/static/pxe-infra/http/. - The script uses iPXE's own ${boot-url} variable substitution (escaped with Nix's ''${...}'' so it survives into the output verbatim) to sanboot http:///latest/ alpine-worker.raw, with a 5s retry on failure. - modules/pxe-infra/dhcp.nix: when both pxe-infra.tftp.enable and pxe-infra.bootloader.enable are on, dnsmasq settings now include `dhcp-boot=`. The MAC filter (tag:!known) still drops requests from unknown MACs before they can use the filename. - modules/pxe-infra/default.nix: imports ipxe.nix and adds two `L+` tmpfiles rules that symlink the iPXE binary and the boot.ipxe script from /etc/static/pxe-infra/... into the served TFTP/HTTP roots. `L+` (instead of plain `L`) means systemd-tmpfiles will update the symlink target on every boot, so pkgs.ipxe upgrades via nixos-rebuild take effect without redeploying anything. Refs: docs/ARCHITECTURE.md §L3, §L4. --- modules/options.nix | 28 ++++++++++++++++++++ modules/pxe-infra/default.nix | 9 +++++++ modules/pxe-infra/dhcp.nix | 10 ++++++- modules/pxe-infra/ipxe.nix | 49 +++++++++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 modules/pxe-infra/ipxe.nix diff --git a/modules/options.nix b/modules/options.nix index 416e6f6..5809895 100644 --- a/modules/options.nix +++ b/modules/options.nix @@ -334,5 +334,33 @@ ''; }; }; + + # iPXE bootloader staging. Bridges the UEFI firmware (which + # speaks TFTP only) to the L3 HTTP server (which serves the + # actual image). Without this, the L4 VM gets a DHCP lease + # but has no filename to TFTP-fetch, so the chain stops there. + bootloader = { + enable = lib.mkOption { + type = lib.types.bool; + default = true; + description = '' + Whether to stage the iPXE EFI binary into the TFTP root + and write the static boot.ipxe script into the HTTP + root. Off disables the entire L4 boot chain. + ''; + }; + ipxeBinary = lib.mkOption { + type = lib.types.str; + default = "ipxe.efi"; + description = '' + Filename of the iPXE EFI binary under the TFTP root. + The actual binary is taken from `pkgs.ipxe`; this only + controls the served filename and the `filename` DHCP + option handed to UEFI firmware. Defaults to the + universal UEFI binary; `snp.efi` is a smaller variant + that works on virtio-net only. + ''; + }; + }; }; } diff --git a/modules/pxe-infra/default.nix b/modules/pxe-infra/default.nix index 42dc7ff..89fbcfc 100644 --- a/modules/pxe-infra/default.nix +++ b/modules/pxe-infra/default.nix @@ -17,6 +17,7 @@ ./bridge.nix ./dhcp.nix ./http.nix + ./ipxe.nix ./vm.nix ]; @@ -25,10 +26,18 @@ # both serve from here; the actual artifacts land in this tree # by whatever means (manual copy, deploy step, mkosi output # directory — see docs/OPEN-QUESTIONS.md B.9/B.10). + # + # The `L+` rules symlink the iPXE binary and boot.ipxe script + # from /etc/static/pxe-infra/... (where the ipxe.nix module + # drops them via environment.etc) into the served roots, so + # `nixos-rebuild` upgrades of pkgs.ipxe are picked up at the + # next boot without redeploying anything. systemd.tmpfiles.rules = [ "d ${config.pxe-infra.artifacts.root} 0755 root root - -" "d ${config.pxe-infra.artifacts.root}/http 0755 root root - -" "d ${config.pxe-infra.artifacts.root}/tftpboot 0755 root root - -" + "L+ ${config.pxe-infra.artifacts.root}/tftpboot/${config.pxe-infra.bootloader.ipxeBinary} - - - - /etc/static/pxe-infra/tftpboot/${config.pxe-infra.bootloader.ipxeBinary}" + "L+ ${config.pxe-infra.artifacts.root}/http/boot.ipxe - - - - /etc/static/pxe-infra/http/boot.ipxe" ]; }; } diff --git a/modules/pxe-infra/dhcp.nix b/modules/pxe-infra/dhcp.nix index 4e4077f..6bd2c67 100644 --- a/modules/pxe-infra/dhcp.nix +++ b/modules/pxe-infra/dhcp.nix @@ -64,7 +64,15 @@ let tftp-root = "${config.pxe-infra.artifacts.root}/tftpboot"; }; - filtered = base // tftpSettings // lib.optionalAttrs (!cfg.authoritative) { + # Boot filename: when the iPXE binary is staged, hand it to + # UEFI firmware in the DHCP offer so the firmware knows what + # to TFTP-fetch. Sent to all clients; the `tag:!known` filter + # below still drops requests from unknown MACs. + bootSettings = lib.optionalAttrs (config.pxe-infra.tftp.enable && config.pxe-infra.bootloader.enable) { + dhcp-boot = config.pxe-infra.bootloader.ipxeBinary; + }; + + filtered = base // tftpSettings // bootSettings // lib.optionalAttrs (!cfg.authoritative) { # Drop any DHCP request not carrying the `known` tag we set on # static hosts. This is the MAC filter that keeps the rig # from accidentally serving strangers if a real-LAN packet diff --git a/modules/pxe-infra/ipxe.nix b/modules/pxe-infra/ipxe.nix new file mode 100644 index 0000000..5173328 --- /dev/null +++ b/modules/pxe-infra/ipxe.nix @@ -0,0 +1,49 @@ +# pxe-infra/ipxe — stage the iPXE EFI binary into the TFTP root +# and write a static boot.ipxe script into the HTTP root. +# +# Together with the `dhcp-boot` option configured in dhcp.nix, +# this completes the boot chain: +# UEFI firmware +# → TFTP: artifacts.root/tftpboot/ +# → runs iPXE +# → HTTP: http:///boot.ipxe +# → iPXE script sanboots the alpine-worker raw disk image +# +# The artifact itself is produced by build/mkosi/ in the +# pxe-infra-automation repo and rsynced into +# artifacts.root/http//. A `latest/` symlink points at the +# newest , which the static boot.ipxe references. +{ + config, + lib, + pkgs, + ... +}: +{ + config = lib.mkIf (config.pxe-infra.enable && config.pxe-infra.bootloader.enable) { + # iPXE binary from nixpkgs. We drop it under + # /etc/static/pxe-infra/tftpboot/ via environment.etc; the + # tmpfiles rule in default.nix symlinks from there into the + # actual served TFTP root. + environment.etc."pxe-infra/tftpboot/${config.pxe-infra.bootloader.ipxeBinary}".source = + "${pkgs.ipxe}/${config.pxe-infra.bootloader.ipxeBinary}"; + + # Static iPXE script, served by the L3 HTTP server. iPXE + # fetches it via HTTP after the initial TFTP-boot of the + # binary itself. + # + # `''${...}''` is Nix's escape for a literal `${...}` in a + # multi-line string; iPXE uses `${boot-url}` as its own + # variable substitution, which must survive into the output + # verbatim. + environment.etc."pxe-infra/http/boot.ipxe".source = pkgs.writeText "boot.ipxe" '' + #!ipxe + set boot-url http://${config.pxe-infra.bridge.address}/latest + sanboot ''${boot-url}/alpine-worker.raw || goto retry + :retry + echo sanboot failed, retrying in 5s + sleep 5 + sanboot ''${boot-url}/alpine-worker.raw + ''; + }; +}