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