diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e59ab4c9..ee36c81e 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -553,7 +553,8 @@ jobs: # What each bucket's scenarios read, so it downloads only that: # `image` the image-set parts for dist/diskless (dogfood-diskless.sh # and dogfood-install.sh boot netboot, dogfood-installer.sh the - # installer); outside releases `next` the image-next parts for + # installer, or netboot with DOGFOOD_INSTALL=diskless-console); + # outside releases `next` the image-next parts for # dist/diskless-next, `broken` dist/diskless-broken and `sigs` # dist/ignition-sigs. include: @@ -568,7 +569,7 @@ jobs: broken: true sigs: false - scenario: installer - image: image-set-{core,installer} + image: image-set-{core,netboot,installer} next: image-next-* broken: false sigs: false diff --git a/docs/skills/ci-tooling.md b/docs/skills/ci-tooling.md index 8637dc2b..6e0067c3 100644 --- a/docs/skills/ci-tooling.md +++ b/docs/skills/ci-tooling.md @@ -300,27 +300,24 @@ those of the org config `projectbluefin/.project/prow.yaml`; the hive Fedora's `edk2-ovmf` is pinned by Koji URL + SHA-256 in `scripts/ci/install-qemu-ovmf.sh`: Ubuntu 26.04's OVMF 2025.11 rejects PK enrollment and boots with Secure Boot off ([details](ddi-installer-build.md)). Bump by hand: Renovate cannot update the SHA-256. - - diskless netboot, no failed units; `tests/fixtures/nfs/netdb.probe` - must resolve `tcp` and `sunrpc`, reach the local portmapper (`rpcinfo`) - and get an NFSv3 mount past the protocol lookup (`DOGFOOD_EXPECT`); - - `DOGFOOD_TAMPER=raw` and `=sums`: the pull must refuse a corrupted DDI - (checksum) and that DDI with `SHA256SUMS` re-hashed to match it - (signature), and nothing may boot; - - Ignition from the `ignition.config` credential and from - `bluefin-node.ign` over UEFI HTTP boot, both with - `tests/fixtures/ignition/apply-marker.ign` (`DOGFOOD_EXPECT`); outside - releases also signed with the Ignition config key (applied), with the - image signing key or over other bytes (refused), and unsigned on the - `1..1` set, built with `ignition_allow_unsigned` off (refused; - see booty-integration.md); + - diskless netboot, no failed units; `tests/fixtures/nfs/netdb.probe` must resolve `tcp` and + `sunrpc`, reach the local portmapper (`rpcinfo`) and get an NFSv3 mount past the protocol + lookup (`DOGFOOD_EXPECT`); + - `DOGFOOD_TAMPER=raw` and `=sums`: the pull must refuse a corrupted DDI (checksum) and that + DDI with `SHA256SUMS` re-hashed to match it (signature), and nothing may boot; + - `DOGFOOD_FIRMWARE=foreign-keys`: firmware with vendor keys, not the Bluefin db + (`OVMF_VARS_FOREIGN`), refuses the netboot ESP's loader before systemd-boot runs (#450); + - Ignition from the `ignition.config` credential and from `bluefin-node.ign` over UEFI HTTP + boot, both with `tests/fixtures/ignition/apply-marker.ign` (`DOGFOOD_EXPECT`); outside + releases also signed with the Ignition config key (applied), with the image signing key or + over other bytes (refused), and unsigned on the `1..1` set, built with + `ignition_allow_unsigned` off (refused; see booty-integration.md); - releases: diskless boot, `systemd-sysinstall` to disk, boot the disk; - - every other build (the nightly build of `main`, `full-build` and FSDK - pull requests, dispatches): the same, then `systemd-sysupdate` A->B to - `1..1` and a boot-counted rollback from a corrupted `1..2`, - with the ZFS and NVIDIA sysexts merged (`DOGFOOD_SYSEXT=zfs,nvidia`; - versions: [ddi-installer-build.md](ddi-installer-build.md)). Releases - skip it: their extra sets would be release-signed versions nobody - publishes. + - every other build (the nightly build of `main`, `full-build` and FSDK pull requests, + dispatches): the same, then `systemd-sysupdate` A->B to `1..1` and a boot-counted + rollback from a corrupted `1..2`, with the ZFS and NVIDIA sysexts merged + (`DOGFOOD_SYSEXT=zfs,nvidia`; versions: [ddi-installer-build.md](ddi-installer-build.md)). + Releases skip it: their extra sets would be release-signed versions nobody publishes. - `scripts/dogfood-installer.sh`: offline unattended install onto a blank disk, then boot with and without the stick. Outside releases, update to `1..1`: unreachable and foreign-signed sources must fail `systemd-sysupdate.service` and appear on the login banner; @@ -335,6 +332,9 @@ those of the org config `projectbluefin/.project/prow.yaml`; the hive `boot-test-logs-installer` retains serial journals, screen snapshots and console proof, plus `installer-artifact-identity.txt`: the source commit and pre-boot SHA-256 of the installer, manifest and signature. Partial logs survive regressions; disks and credentials stay excluded. + `DOGFOOD_INSTALL=diskless-console` (#450) logs in at a diskless boot's tty1 with a per-run root + password (hashed, masked), types the "From a diskless node" commands of [ddi-installer.md](ddi-installer.md), + answers `systemd-sysinstall`'s disk, keep/erase, firmware and `yes` questions, boots the disk. 5. **Version Derivation:** The release version is set per build with `just set-version`: `YY.MM.` for releases, `0.` for every other build so it can never sort above a release. diff --git a/docs/skills/ddi-installer.md b/docs/skills/ddi-installer.md index 0fb3e6af..b8f14024 100644 --- a/docs/skills/ddi-installer.md +++ b/docs/skills/ddi-installer.md @@ -103,7 +103,9 @@ path: `BOOTX64.EFI` alone. - **Microsoft-only firmware.** Until shim is Microsoft-signed it boots only where the Bluefin db is enrolled, and the USB installer's Secure Boot - check still refuses firmware that lacks the Bluefin db. + check still refuses firmware that lacks the Bluefin db. The boot test + shows firmware that holds only other keys refusing the netboot ESP's + loader before anything of Bluefin runs (`DOGFOOD_FIRMWARE=foreign-keys`). - **UEFI HTTP boot** loads the netboot UKI directly, without shim. shim fetches `DEFAULT_LOADER` relative to its own URL when HTTP-booted; serving a shim chain that way is Booty-side work. @@ -193,9 +195,17 @@ in-RAM DDI partitions (`/dev/disk/by-loop-ref/rootdisk.raw-part1` and `part2`): systemctl start run-bluefin-boot.mount kernel="$(ls /run/bluefin/boot/EFI/Linux/bluefin-server-[0-9]*.efi)" systemd-sysinstall --kernel="${kernel}" \ - --definitions=/run/bluefin/boot/bluefin/repart.d /dev/sdX + --definitions=/run/bluefin/boot/bluefin/repart.d ``` +`systemd-sysinstall` lists the disks and asks for one (its number or name), +whether to keep what is on it or erase it, whether to register the install in +the firmware's boot entries, and, after a summary, for `yes`. Name the disk at +the end of the command (e.g. `/dev/sdX`) to skip the first question. The boot +test logs in at a diskless node's console, types this block as written and +answers the questions (`DOGFOOD_INSTALL=diskless-console` of +`scripts/dogfood-installer.sh`). + On older image builds whose `repart.d` used `CopyBlocks=auto`, active system extensions (`systemd-sysext`, e.g. ZFS or k0s) turn `/usr` into an overlayfs, causing `systemd-repart` to fail with "Failed to resolve automatic CopyBlocks= diff --git a/scripts/ci/boot-test.sh b/scripts/ci/boot-test.sh index aef22f45..dd27bdf0 100755 --- a/scripts/ci/boot-test.sh +++ b/scripts/ci/boot-test.sh @@ -56,6 +56,10 @@ bucket_diskless() { -- scripts/dogfood-diskless.sh dist/diskless --check scenario "Refuse an unsigned manifest" DOGFOOD_TAMPER=sums \ -- scripts/dogfood-diskless.sh dist/diskless --check + # Firmware holding only someone else's Secure Boot keys refuses the + # boot loader before Bluefin runs (#450). + scenario "Refuse to boot on firmware with foreign Secure Boot keys" DOGFOOD_FIRMWARE=foreign-keys \ + -- scripts/dogfood-diskless.sh dist/diskless --check scenario "Ignition config from a credential" \ DOGFOOD_IGNITION=tests/fixtures/ignition/apply-marker.ign "${ignition_probe[@]}" \ -- scripts/dogfood-diskless.sh dist/diskless --check @@ -100,7 +104,8 @@ bucket_install() { # The offline USB installer onto a blank disk: outside releases also the # out-of-the-box update contract of a stick install (#310); the supervised # console path under concurrent noise, including a real prewrite failure -# (#454); and the refusal without Secure Boot (#309). +# (#454); the refusal without Secure Boot (#309); and the documented +# install from a diskless node, typed at its console (#450). bucket_installer() { if [ "${release}" = true ]; then scenario "Install from the offline USB installer and boot it" \ @@ -119,6 +124,9 @@ bucket_installer() { scenario "Refuse to install from the USB installer without Secure Boot" \ DOGFOOD_SECURE_BOOT=off DOGFOOD_STATE=dist/dogfood-installer-secure-boot-off \ -- scripts/dogfood-installer.sh dist/diskless + scenario "Install from a diskless boot at the console" \ + DOGFOOD_INSTALL=diskless-console DOGFOOD_STATE=dist/dogfood-installer-diskless-console \ + -- scripts/dogfood-installer.sh dist/diskless } # The offline USB installer onto disks that are not empty (#359): over a diff --git a/scripts/ci/dogfood-preflight.sh b/scripts/ci/dogfood-preflight.sh index cde2ef24..a59c021c 100755 --- a/scripts/ci/dogfood-preflight.sh +++ b/scripts/ci/dogfood-preflight.sh @@ -24,6 +24,7 @@ host_tools=( sfdisk blkid mkswap # (fdisk, util-linux) the foreign-gpt target disk mkfs.vfat mkfs.ext4 # (dosfstools, e2fsprogs) the same gpg # (gnupg) the foreign-signed update source + openssl # (openssl) hashes the diskless console's root password sha256sum base64 dd od truncate realpath mktemp awk ) # Matched, but never run on the runner. @@ -68,7 +69,7 @@ for tool in "${host_tools[@]}"; do command -v "${tool}" >/dev/null || error "${tool}: command not found (add its package to scripts/ci/install-qemu-ovmf.sh)" done -for var in OVMF_CODE OVMF_CODE_INSECURE OVMF_VARS; do +for var in OVMF_CODE OVMF_CODE_INSECURE OVMF_VARS OVMF_VARS_FOREIGN; do [ -s "${!var:-}" ] || error "${var} is not set to a firmware file (scripts/ci/install-qemu-ovmf.sh sets it)" done diff --git a/scripts/ci/install-qemu-ovmf.sh b/scripts/ci/install-qemu-ovmf.sh index 5283dbe0..5f0941f1 100755 --- a/scripts/ci/install-qemu-ovmf.sh +++ b/scripts/ci/install-qemu-ovmf.sh @@ -1,7 +1,8 @@ #!/usr/bin/env bash # Install what the boot test runs on an Ubuntu runner: QEMU, the disk tools # the dogfood scripts call, and Fedora's Secure Boot OVMF, then export -# OVMF_CODE, OVMF_CODE_INSECURE and OVMF_VARS to the job's later steps. +# OVMF_CODE, OVMF_CODE_INSECURE, OVMF_VARS and OVMF_VARS_FOREIGN (Fedora's +# store with its vendor keys enrolled) to the job's later steps. # boot-test and boot-test-preflight both run this, so they cannot drift. # # bash scripts/ci/install-qemu-ovmf.sh @@ -18,7 +19,7 @@ OVMF_RPM_SHA256=8b157ab2aa0cccccf021f43011308349147c76ae59550f52239ea50678dd0a5f sudo apt-get update sudo apt-get install -y --no-install-recommends qemu-system-x86 libarchive-tools \ - fdisk dosfstools e2fsprogs mtools + fdisk dosfstools e2fsprogs mtools openssl sudo chmod 666 /dev/kvm rpm="${RUNNER_TEMP:?}/edk2-ovmf.rpm" @@ -28,9 +29,10 @@ echo "${OVMF_RPM_SHA256} ${rpm}" | sha256sum -c - mkdir -p "${RUNNER_TEMP}/ovmf" bsdtar -xf "${rpm}" -C "${RUNNER_TEMP}/ovmf" \ ./usr/share/edk2/ovmf/OVMF_CODE.secboot.fd ./usr/share/edk2/ovmf/OVMF_VARS.fd \ - ./usr/share/edk2/ovmf/OVMF_CODE.fd + ./usr/share/edk2/ovmf/OVMF_CODE.fd ./usr/share/edk2/ovmf/OVMF_VARS.secboot.fd { echo "OVMF_CODE=${ovmf}/OVMF_CODE.secboot.fd" echo "OVMF_CODE_INSECURE=${ovmf}/OVMF_CODE.fd" echo "OVMF_VARS=${ovmf}/OVMF_VARS.fd" + echo "OVMF_VARS_FOREIGN=${ovmf}/OVMF_VARS.secboot.fd" } >> "${GITHUB_ENV:?}" diff --git a/scripts/dogfood-diskless.sh b/scripts/dogfood-diskless.sh index 9524ee48..2960d9ce 100644 --- a/scripts/dogfood-diskless.sh +++ b/scripts/dogfood-diskless.sh @@ -45,6 +45,13 @@ # SHA256SUMS re-hashed to match it but no longer matching # SHA256SUMS.gpg (sums); --check then passes only if the # initrd's pull refuses it for that reason +# DOGFOOD_FIRMWARE=foreign-keys (--check, netboot, no DOGFOOD_VARS) start from a +# variable store already in user mode with someone else's +# Secure Boot keys (the distribution's pre-enrolled vendor +# keys, OVMF_VARS_FOREIGN) instead of a blank one: --check +# passes only if the firmware itself refuses the netboot +# ESP's boot loader, so neither systemd-boot nor the kernel +# runs set -euo pipefail dir="$(realpath "${1:?usage: $0 [--check]}")" @@ -63,6 +70,18 @@ image="bluefin-server_${ver}.raw" . "$(dirname "${BASH_SOURCE[0]}")/dogfood-firmware.sh" code="$(ovmf_code)" || exit 1 vars_tmpl="$(ovmf_vars)" || exit 1 +firmware="${DOGFOOD_FIRMWARE:-}" +case "${firmware}" in + '') ;; + foreign-keys) + if [ "${mode}" != --check ] || [ "${DOGFOOD_BOOT:-netboot}" != netboot ] || [ -n "${DOGFOOD_VARS:-}" ] \ + || [ -n "${DOGFOOD_REFUSAL:-}${DOGFOOD_TAMPER:-}" ]; then + echo "ERROR: DOGFOOD_FIRMWARE=foreign-keys is a --check of a netboot boot, without DOGFOOD_VARS, DOGFOOD_REFUSAL or DOGFOOD_TAMPER" >&2 + exit 1 + fi + vars_tmpl="$(ovmf_vars_foreign)" || exit 1 ;; + *) echo "ERROR: DOGFOOD_FIRMWARE must be foreign-keys" >&2; exit 1 ;; +esac work="$(mktemp -d /tmp/bluefin-dogfood.XXXXXX)" probe_log="${DOGFOOD_PROBE_LOG:-${work}/probe.log}" @@ -89,6 +108,11 @@ for f in "${dir}"/*; do ln -s "${f}" "${srv}/"; done if [ -n "${DOGFOOD_SERVE_EXTRA:-}" ]; then for f in "${DOGFOOD_SERVE_EXTRA}"/*; do ln -sf "$(realpath "${f}")" "${srv}/"; done; fi image_re="${image//./\\.}" refusal_re="${DOGFOOD_REFUSAL:-}" +# OVMF reports each boot option it cannot load on the firmware console; an +# image Secure Boot rejects fails to load with Access Denied (or, under a +# deferring policy, Security Violation). +[ "${firmware}" != foreign-keys ] \ + || refusal_re='BdsDxe: failed to load Boot[0-9A-F]{4} .*: (Access Denied|Security Violation)' case "${DOGFOOD_TAMPER:-}" in raw|sums) rm "${srv}/${image}"; cp "${dir}/${image}" "${srv}/${image}" @@ -180,6 +204,7 @@ if [ -n "${DOGFOOD_IGNITION:-}" ]; then fi tamper="${DOGFOOD_TAMPER:-}" refusal="${tamper:+tamper=${tamper}}" +[ -z "${firmware}" ] || refusal="firmware=${firmware}" [ -n "${refusal_re}" ] && refusal="${refusal:-refusal}" if [ -n "${tamper}" ]; then # The initrd's console shows only that the download unit failed; copy the @@ -317,7 +342,11 @@ if [ -n "${refusal}" ]; then elif [ -n "${DOGFOOD_NODE_IGN:-}" ] && { ! served bluefin-node.ign || ! served bluefin-node.ign.gpg '[0-9]+'; }; then echo "FAIL: ${refusal}: refused before fetching bluefin-node.ign and requesting its .gpg" >&2 status=1 - elif ! grep -aq 'Secure boot enabled' "${dir}/dogfood-serial.log"; then + elif [ -n "${firmware}" ] && grep -aqE 'successfully enrolled|Secure boot enabled' "${dir}/dogfood-serial.log"; then + # systemd-boot enrolls the Bluefin keys only into a store in setup mode. + echo "FAIL: ${refusal}: refused, but systemd-boot or the kernel ran first" >&2 + status=1 + elif [ -z "${firmware}" ] && ! grep -aq 'Secure boot enabled' "${dir}/dogfood-serial.log"; then echo "FAIL: ${refusal}: refused, but the kernel did not run with Secure Boot enabled" >&2 status=1 else diff --git a/scripts/dogfood-firmware.sh b/scripts/dogfood-firmware.sh index 9ad2df66..c114ff0a 100644 --- a/scripts/dogfood-firmware.sh +++ b/scripts/dogfood-firmware.sh @@ -8,6 +8,8 @@ # ovmf_code Secure Boot firmware code (OVMF_CODE) # ovmf_code_insecure firmware without Secure Boot (OVMF_CODE_INSECURE) # ovmf_vars blank variable store (OVMF_VARS) +# ovmf_vars_foreign variable store in user mode with the distribution's +# vendor Secure Boot keys, not ours (OVMF_VARS_FOREIGN) first_existing() { for f in "$@"; do [ -f "$f" ] && { echo "$f"; return 0; }; done; return 1; } @@ -40,3 +42,12 @@ ovmf_vars() { /usr/share/edk2/x64/OVMF_VARS.4m.fd \ || { echo "ERROR: no blank OVMF_VARS found (set OVMF_VARS)" >&2; return 1; } } + +ovmf_vars_foreign() { + [ -n "${OVMF_VARS_FOREIGN:-}" ] && { echo "${OVMF_VARS_FOREIGN}"; return 0; } + first_existing \ + /usr/share/edk2/ovmf/OVMF_VARS.secboot.fd \ + /usr/share/OVMF/OVMF_VARS_4M.ms.fd \ + /usr/share/OVMF/OVMF_VARS.ms.fd \ + || { echo "ERROR: no OVMF_VARS with enrolled vendor keys found (set OVMF_VARS_FOREIGN)" >&2; return 1; } +} diff --git a/scripts/dogfood-installer.sh b/scripts/dogfood-installer.sh index 27ce3afd..2791c023 100644 --- a/scripts/dogfood-installer.sh +++ b/scripts/dogfood-installer.sh @@ -51,6 +51,21 @@ # target without trying to boot it. # Every install also checks the "installed" screen, and every first boot # the serial console's login banner (hostname and IP address). +# DOGFOOD_INSTALL=diskless-console (no , blank target, Secure Boot on, +# minimal profile) installs from a diskless boot instead of the stick, the +# way a person at the monitor of a netbooted node does (#450). then +# needs the netboot ESP and the OS DDI, which a local HTTP server serves: +# 1. enroll the Secure Boot keys from the netboot ESP +# 2. boot it diskless with the target and a second disk; root's password, +# random for this run, reaches the node only hashed (the +# passwd.hashed-password.root credential; masked in the CI log), and +# the image keeps root locked otherwise. Log in as root at tty1, type +# the commands "From a diskless node" in docs/skills/ddi-installer.md, +# and answer systemd-sysinstall's questions: the target by its number +# in the disk list, erase, register in the firmware, and "yes" after +# the summary; then power off. The target must hold exactly the ESP and +# usr slot A (+ verity), and the other disk stay blank +# 3. boot the target on its own, as step 3 above # is an image set with a higher version signed by the same key (dev # keys locally, as in CI), or "release": the transfers stay on the image's # own source (GitHub Releases) and the newest release must be found, staged @@ -72,8 +87,11 @@ # hanging. # # Usage: dogfood-installer.sh .raw> [|release] +# (DOGFOOD_INSTALL=diskless-console: with bluefin-server-netboot_.esp.raw +# and bluefin-server_.raw) # Environment: -# DOGFOOD_PORT= HTTP port serving (default 8765) +# DOGFOOD_PORT= HTTP port serving , or to the diskless +# boot of DOGFOOD_INSTALL=diskless-console (default 8765) # DOGFOOD_STATE= logs, target disk and UEFI vars (default dist/dogfood-installer) # DOGFOOD_TARGET_DISK= target disk image, recreated (default /target.raw) # DOGFOOD_TARGET= what the target holds before the install: @@ -90,8 +108,9 @@ # DOGFOOD_SYSINSTALL_CRED=0 do not pass the unattended drop-in credential # DOGFOOD_SECURE_BOOT= enforcing (default: the keys from step 1) or # off (firmware without Secure Boot) -# DOGFOOD_INSTALL= unattended (default: the drop-in credential) or -# console (typed at the monitor, see above) +# DOGFOOD_INSTALL= unattended (default: the drop-in credential), +# console (typed at the monitor, see above) or +# diskless-console (from a diskless boot, see above) # DOGFOOD_CONSOLE_NOISE=0|1 concurrent error noise (default 0; console only) # DOGFOOD_CONSOLE_FAILURE=.. none (default) or kernel (console only): a wrong # guest kernel payload path, before any disk write @@ -134,7 +153,9 @@ case "${install_mode}" in unattended) ;; console) [ -z "${next}" ] && [ "${target_kind}" != prior-install ] && [ "${secure_boot}" = enforcing ] \ || { echo "ERROR: DOGFOOD_INSTALL=console does not combine with , prior-install or DOGFOOD_SECURE_BOOT=off" >&2; exit 1; } ;; - *) echo "ERROR: DOGFOOD_INSTALL must be unattended or console, not '${install_mode}'" >&2; exit 1 ;; + diskless-console) [ -z "${next}" ] && [ "${target_kind}" = blank ] && [ "${secure_boot}" = enforcing ] \ + || { echo "ERROR: DOGFOOD_INSTALL=diskless-console needs a blank DOGFOOD_TARGET, Secure Boot and no " >&2; exit 1; } ;; + *) echo "ERROR: DOGFOOD_INSTALL must be unattended, console or diskless-console, not '${install_mode}'" >&2; exit 1 ;; esac console_noise="${DOGFOOD_CONSOLE_NOISE:-0}" console_failure="${DOGFOOD_CONSOLE_FAILURE:-none}" @@ -158,10 +179,13 @@ case "${profile}" in minimal|home-server) ;; *) echo "ERROR: DOGFOOD_PROFILE must be minimal or home-server" >&2; exit 1 ;; esac +[ "${install_mode}" != diskless-console ] || [ "${profile}" = minimal ] \ + || { echo "ERROR: DOGFOOD_INSTALL=diskless-console installs the minimal profile only" >&2; exit 1; } steps=4 [ -z "${next}" ] || steps=6 [ "${target_kind}" != prior-install ] || steps=6 [ "${console_failure}" = none ] || steps=2 +[ "${install_mode}" != diskless-console ] || steps=3 state="$(realpath -m "${DOGFOOD_STATE:-dist/dogfood-installer}")" # Never recreate proof state inside, or above, the exported source image set. [[ "${dir}/" != "${state}/"* ]] \ @@ -173,10 +197,18 @@ target_dev="${DOGFOOD_TARGET_DEV:-/dev/disk/by-id/virtio-${target_serial}}" install_args="${DOGFOOD_SYSINSTALL_ARGS:---confirm=no}" dropin_src="${here}/../files/os/systemd/system/systemd-sysinstall.service.d/10-bluefin-installer.conf" -# shellcheck disable=SC2012 # release file names have no odd characters -installer="$(ls "${dir}"/bluefin-server-installer_*.raw 2>/dev/null | tail -n1)" \ - || { echo "ERROR: no bluefin-server-installer_.raw in ${dir}" >&2; exit 1; } -ver="${installer##*/bluefin-server-installer_}"; ver="${ver%.raw}" +if [ "${install_mode}" = diskless-console ]; then + # shellcheck disable=SC2012 # release file names have no odd characters + esp="$(ls "${dir}"/bluefin-server-netboot_*.esp.raw 2>/dev/null | tail -n1)" \ + || { echo "ERROR: no bluefin-server-netboot_.esp.raw in ${dir}" >&2; exit 1; } + ver="${esp##*/bluefin-server-netboot_}"; ver="${ver%.esp.raw}" + [ -f "${dir}/bluefin-server_${ver}.raw" ] || { echo "ERROR: no bluefin-server_${ver}.raw in ${dir}" >&2; exit 1; } +else + # shellcheck disable=SC2012 # release file names have no odd characters + installer="$(ls "${dir}"/bluefin-server-installer_*.raw 2>/dev/null | tail -n1)" \ + || { echo "ERROR: no bluefin-server-installer_.raw in ${dir}" >&2; exit 1; } + ver="${installer##*/bluefin-server-installer_}"; ver="${ver%.raw}" +fi # shellcheck source=scripts/dogfood-firmware.sh . "${here}/dogfood-firmware.sh" @@ -203,11 +235,13 @@ cp "${vars_tmpl}" "${vars}" # Reuse the Homelab harness's stock systemd-boot profile selection. Only the # scratch stick changes; the signed UKI and release artifact remain intact. -cp --reflink=auto "${installer}" "${state}/installer.raw" -installer="${state}/installer.raw" -esp_offset="$(sfdisk --json "${installer}" | python3 -c 'import json,sys; print(next(p["start"] for p in json.load(sys.stdin)["partitiontable"]["partitions"] if p.get("name") == "bluefin-installer") * 512)')" -printf 'timeout 3\nconsole-mode keep\nsecure-boot-enroll if-safe\ndefault bluefin-server-installer_*@%s\n' "${profile}" > "${state}/loader.conf" -mcopy -o -i "${installer}@@${esp_offset}" "${state}/loader.conf" ::/loader/loader.conf +if [ "${install_mode}" != diskless-console ]; then + cp --reflink=auto "${installer}" "${state}/installer.raw" + installer="${state}/installer.raw" + esp_offset="$(sfdisk --json "${installer}" | python3 -c 'import json,sys; print(next(p["start"] for p in json.load(sys.stdin)["partitiontable"]["partitions"] if p.get("name") == "bluefin-installer") * 512)')" + printf 'timeout 3\nconsole-mode keep\nsecure-boot-enroll if-safe\ndefault bluefin-server-installer_*@%s\n' "${profile}" > "${state}/loader.conf" + mcopy -o -i "${installer}@@${esp_offset}" "${state}/loader.conf" ::/loader/loader.conf +fi fail() { echo "FAIL: $* (logs: ${state})" >&2 @@ -268,12 +302,24 @@ qemu=(qemu-system-x86_64 -drive if=pflash,format=raw,unit=1,file="${vars}" -display none -no-reboot ) -# snapshot=on keeps the release artifact untouched. # shellcheck disable=SC2054 # commas belong to the QEMU options -stick=(-drive "if=none,id=stick,format=raw,snapshot=on,file=${installer}" - -device virtio-blk-pci,drive=stick,serial=bluefin-installer) disk=(-drive "if=none,id=target,format=raw,file=${target}" -device "virtio-blk-pci,drive=target,serial=${target_serial}") +# The medium step 1 enrolls the Secure Boot keys from: the stick, or the +# netboot ESP that diskless-console then boots, as the disk the firmware +# boots first. snapshot=on keeps the release artifact untouched. +if [ "${install_mode}" = diskless-console ]; then + medium_name="netboot ESP" + # shellcheck disable=SC2054 # commas belong to the QEMU options + medium=(-drive "if=none,id=esp,format=raw,snapshot=on,file=${esp}" + -device virtio-blk-pci,drive=esp,serial=bluefin-netboot,bootindex=1) +else + medium_name="installer" + # shellcheck disable=SC2054 # commas belong to the QEMU options + stick=(-drive "if=none,id=stick,format=raw,snapshot=on,file=${installer}" + -device virtio-blk-pci,drive=stick,serial=bluefin-installer) + medium=("${stick[@]}") +fi # boot_start : start QEMU in the background. Serial logs # land in ${state}/.*.log; the monitor (what a person sees on tty1) @@ -385,6 +431,21 @@ wait_prompt() { # last_screen : the newest snapshot of the monitor. last_screen() { awk '/^=== screen /{buf=""; next} /^=== console .* noise=[0-9]+$/{last=buf; complete=1; next} {buf=buf $0 "\n"} END{printf "%s", complete ? last : buf}' "${state}/$1.screen.log"; } +# wait_question []: wait until the newest snapshot of +# the monitor shows , however the console wrapped it: systemd-sysinstall +# asks questions wider than the screen. Whitespace is ignored on both sides, +# since a wrapped line loses its trailing blanks. +wait_question() { + local want="${2//[[:space:]]/}" deadline=$(( $(date +%s) + ${3:-300} )) + while kill -0 "${qemu_pid}" 2>/dev/null && [ "$(date +%s)" -lt "${deadline}" ]; do + [[ "$(last_screen "$1" 2>/dev/null | tr -d '[:space:]')" == *"${want}"* ]] && return 0 + sleep 1 + done + kill "${qemu_pid}" 2>/dev/null || true + tail -n 30 "${state}/$1.screen.log" >&2 || true + fail "$1: the monitor never showed '$2'" +} + # Commit text and metadata together only when a full console trailer arrives. last_console_frame() { awk '/^=== screen /{buf=""; next} /^=== console .* noise=[0-9]+$/{last=$0 "\n" buf; complete=1; next} {buf=buf $0 "\n"} END{if (complete) printf "%s", last}' \ @@ -439,16 +500,37 @@ hold_console() { "${phase}" "${first_tick}" "${last_tick}" "${first_noise}" "${noise}" "${proof#=== console }" | tee -a "${state}/${name}.console-proof.log" } -# type_keys : type on the guest's keyboard through the -# QEMU monitor (sendkey); a newline is Enter. +# type_keys : type on the guest's keyboard (US layout) +# through the QEMU monitor (sendkey); a newline is Enter, \x15 is Ctrl-U +# (systemd's prompts erase their preselected answer with it). type_keys() { python3 - "${state}/$1.mon" "$2" <<'PY' import socket, sys, time -keys = {" ": "spc", "\n": "ret", "-": "minus", ".": "dot", "/": "slash", "=": "equal", ",": "comma"} +keys = {" ": "spc", "\n": "ret", "\x15": "ctrl-u", "-": "minus", ".": "dot", "/": "slash", "=": "equal", + ",": "comma", ";": "semicolon", "'": "apostrophe", "`": "grave_accent", "\\": "backslash", + "[": "bracket_left", "]": "bracket_right"} +shifted = {"!": "1", "@": "2", "#": "3", "$": "4", "%": "5", "^": "6", "&": "7", "*": "8", "(": "9", ")": "0", + "_": "minus", "+": "equal", ":": "semicolon", '"': "apostrophe", "~": "grave_accent", "|": "backslash", + "{": "bracket_left", "}": "bracket_right", "<": "comma", ">": "dot", "?": "slash"} + + +def key(ch): + if ch in keys: + return keys[ch] + if ch in shifted: + return "shift-" + shifted[ch] + if ch.isascii() and ch.isupper(): + return "shift-" + ch.lower() + if ch.isascii() and ch.isalnum(): + return ch + sys.exit(f"type_keys: no key for {ch!r}") + + +typed = [key(ch) for ch in sys.argv[2]] with socket.socket(socket.AF_UNIX) as s: s.connect(sys.argv[1]) - for ch in sys.argv[2]: - s.sendall(f"sendkey {keys.get(ch, ch)}\n".encode()) + for k in typed: + s.sendall(f"sendkey {k}\n".encode()) time.sleep(0.15) time.sleep(0.5) PY @@ -519,12 +601,12 @@ screen_creds=( if [ "${secure_boot}" = off ]; then echo "==> 1/${steps} firmware without Secure Boot (${code##*/}): nothing to enroll" else - echo "==> 1/${steps} enroll Secure Boot keys from the installer (${ver})" + echo "==> 1/${steps} enroll Secure Boot keys from the ${medium_name} (${ver})" # systemd-boot says "successfully enrolled" on the firmware console once # db, KEK and PK are written, then resets, which -no-reboot turns into an # exit; OVMF sometimes hangs in that reset instead, so the message ends # the boot. Enrolling takes seconds, not the per-boot timeout. - timeout_s=120 boot_start 1-enroll "${stick[@]}" -nic none + timeout_s=120 boot_start 1-enroll "${medium[@]}" -nic none timeout_s=120 boot_wait 1-enroll 'successfully enrolled' ttyS0 grep -aq 'successfully enrolled' "${state}/1-enroll.ttyS0.log" || fail "key enrollment" fi @@ -562,8 +644,9 @@ TTYPath=/dev/ttyS2 EOF # Wanted by sysinit.target too: the console install has no sysinstall drop-in. printf '[Unit]\nWants=dogfood-journal.service\n' > "${state}/journal-wants.conf" -install_creds=(-smbios "$(cred systemd.extra-unit.dogfood-journal.service "${state}/journal.service")" +journal_creds=(-smbios "$(cred systemd.extra-unit.dogfood-journal.service "${state}/journal.service")" -smbios "$(cred systemd.unit-dropin.sysinit.target~dogfood-journal "${state}/journal-wants.conf")") +install_creds=("${journal_creds[@]}") # systemd-firstboot prompts on the installer console first; answer it the # unattended way. sysinstall copies locale, keymap and timezone to the target. for kv in firstboot.locale=C.UTF-8 firstboot.keymap=us firstboot.timezone=UTC 'passwd.hashed-password.root=!*'; do @@ -752,6 +835,93 @@ FAILURE check_done_screen "${name}" } +# diskless_install_command: the commands "From a diskless node" in +# docs/skills/ddi-installer.md, as a person at the console types them. +diskless_install_command() { + awk '/^### From a diskless node$/ {s = 1; next} + b && /^```$/ {exit} + b {print; next} + s && /^#/ {exit} + s && /^```bash$/ {b = 1}' "${here}/../docs/skills/ddi-installer.md" +} + +# diskless_console_install : boot the netboot ESP diskless with the +# target and a second disk, log in as root at tty1 with this run's password, +# type the documented commands, answer systemd-sysinstall's questions (the +# target by its number, erase, register in the firmware, yes), power off, +# and check both disks from the host. +diskless_console_install() { + local name="$1" decoy="${state}/decoy.raw" commands list n pw summary want parts c + commands="$(diskless_install_command)" + [[ "${commands}" == *systemd-sysinstall* ]] \ + || fail "${name}: no systemd-sysinstall command under 'From a diskless node' in docs/skills/ddi-installer.md" + rm -f "${decoy}" + truncate -s 8G "${decoy}" + # Root's password for this run only. The node gets just its hash, the + # plain text stays in this shell (and is masked in the CI log), and the + # image's own root account stays locked. + pw="$(od -An -N12 -tx1 /dev/urandom | tr -d ' \n')" + [ "${GITHUB_ACTIONS:-}" != true ] || echo "::add-mask::${pw}" + (umask 077; printf '%s\n' "${pw}" | openssl passwd -6 -stdin | tr -d '\n' > "${state}/diskless-root.hash") + [ -s "${state}/diskless-root.hash" ] || fail "${name}: could not hash the root password" + printf 'raw,machine,verify=signature,blockdev:rootdisk:http://10.0.2.2:%s/bluefin-server_%s.raw\n' \ + "${port}" "${ver}" > "${state}/import.pull" + (cd "${dir}" && exec python3 -m http.server --bind 127.0.0.1 "${port}" >"${state}/http.log" 2>&1) & + http_pid=$! + local boot_creds=(-smbios "$(cred import.pull "${state}/import.pull")" + -smbios "$(cred passwd.hashed-password.root "${state}/diskless-root.hash")" + "${journal_creds[@]}" "${screen_creds[@]}") + for c in firstboot.locale firstboot.keymap firstboot.timezone; do + boot_creds+=(-smbios "$(cred "${c}" "${state}/${c}")") + done + # The pull and the install share this boot's deadline. + timeout_s=$(( timeout_s * 2 )) boot_start "${name}" "${medium[@]}" "${disk[@]}" \ + -drive "if=none,id=decoy,format=raw,file=${decoy}" -device virtio-blk-pci,drive=decoy,serial=bluefin-decoy \ + -nic user,model=virtio-net-pci "${boot_creds[@]}" + # agetty reprints the banner when DHCP adds the address. + wait_screen "${name}" 'ssh root@[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' "${timeout_s}" + wait_prompt "${name}" ' login:$' + type_keys "${name}" $'root\n' + wait_prompt "${name}" 'Password:$' 60 + type_keys "${name}" "${pw}"$'\n' + wait_prompt "${name}" '[#$]$' 60 + printf '%s\n' "${commands}" | sed 's/^/ typed| /' + type_keys "${name}" "${commands}"$'\n' + # Each question preselects an answer; Ctrl-U clears it before typing. + wait_prompt "${name}" 'Please enter target disk device:( .*)?$' "${timeout_s}" + list="$(last_screen "${name}" | grep -E '^ *[0-9]+\) ')" || true + printf '%s\n' "${list}" | sed 's/^/ screen| /' + n="$(printf '%s\n' "${list}" | sed -n "s|^ *\([0-9][0-9]*\)) ${target_dev}\$|\1|p")" + [ -n "${n}" ] || fail "${name}: ${target_dev} is not in systemd-sysinstall's disk list" + printf '%s\n' "${list}" | grep -qE '\) /dev/disk/by-id/virtio-bluefin-decoy$' \ + || fail "${name}: systemd-sysinstall's disk list does not show the other disk" + type_keys "${name}" $'\x15'"${n}"$'\n' + wait_question "${name}" "Please type 'keep' to install the OS in addition to what the disk already contains, or 'erase' to erase all data on the disk" + type_keys "${name}" $'\x15erase\n' + wait_question "${name}" "Type 'yes' to register OS installation in firmware variables of the local system, 'no' otherwise" + type_keys "${name}" $'\x15yes\n' + wait_question "${name}" "Please type 'yes' to confirm the choices above and begin the installation" + summary="$(last_screen "${name}" | tr -d '[:space:]')" + for want in "SelectedDisk:${target_dev}" EraseDisk:yes RegisterinFirmware:yes; do + [[ "${summary}" == *"${want}"* ]] || fail "${name}: systemd-sysinstall's summary lacks '${want}'" + done + last_screen "${name}" | sed '/^$/d' | sed 's/^/ screen| /' + type_keys "${name}" $'\x15yes\n' + wait_question "${name}" 'Installation succeeded.' "${timeout_s}" + wait_prompt "${name}" '[#$]$' 120 + last_screen "${name}" | sed '/^$/d' | tail -n 8 | sed 's/^/ screen| /' + type_keys "${name}" $'poweroff\n' + boot_wait "${name}" '' + parts="$(sfdisk --json "${target}" 2>/dev/null | python3 -c 'import json, sys; p = json.load(sys.stdin)["partitiontable"]["partitions"]; print(len(p), sum(x.get("name") == "_empty" for x in p))')" \ + || parts=none + [ "${parts}" = "3 0" ] \ + || fail "${name}: expected the ESP and usr slot A (+ verity) on the target and no slot B yet, got partitions/empty: ${parts}" + sfdisk -l "${target}" | sed -n '/^Device/,$p' | sed 's/^/ target| /' + if sfdisk -d "${decoy}" >/dev/null 2>&1 || [ -n "$(blkid -p "${decoy}" 2>/dev/null)" ]; then + fail "${name}: systemd-sysinstall wrote to the other disk" + fi +} + if [ "${secure_boot}" = off ]; then # bluefin-installer-secure-boot.service, which sysinstall Requires=, # reports its result on ttyS1. @@ -783,6 +953,9 @@ elif [ "${install_mode}" = console ]; then echo "PASS: attended sysinstall failed before disk writes; error visible, service journal retained, no reboot/halt or done unit (artifacts: ${state})" exit 0 fi +elif [ "${install_mode}" = diskless-console ]; then + echo "==> 2/${steps} diskless boot of the netboot ESP; systemd-sysinstall typed at tty1 onto a ${target_kind} disk" + diskless_console_install 2-install else echo "==> 2/${steps} offline install onto a ${target_kind} disk: ExecStart=${exec_start} ${install_args} ${target_dev}" run_install 2-install @@ -1067,6 +1240,11 @@ check_disk_boot 3-first-boot 1 "${ver}" 2 check_issue 3-first-boot banner_has 3-first-boot boot 'Last update check: never' +if [ "${install_mode}" = diskless-console ]; then + echo "PASS: a diskless boot installed ${ver} at the console: root logged in at tty1 with this run's password, the documented systemd-sysinstall command listed the disks, took the target by its number, erase, firmware registration and 'yes', and left the other disk blank; the target booted from its own bluefin_usr_${ver} slot with slot B and the xfs root created on first boot and no failed units" + exit 0 +fi + echo "==> 4/${steps} boot the target with the installer still attached" boot 4-with-installer 'PROBE failed=' "${disk[@]}" "${stick[@]}" -nic user,model=virtio-net-pci "${creds[@]}" check_disk_boot 4-with-installer 2 "${ver}" 2 diff --git a/tests/unit/test_ci_boot_test.py b/tests/unit/test_ci_boot_test.py index ab6b4c33..ba701ea8 100644 --- a/tests/unit/test_ci_boot_test.py +++ b/tests/unit/test_ci_boot_test.py @@ -85,7 +85,7 @@ def test_each_scenario_runs_once_and_its_fixtures_exist(release: bool) -> None: for path in re.findall(r"\btests/fixtures/\S+", word): assert (ROOT / path).is_file(), f"{name}: {path}" assert len(names) == len(set(names)) - assert len(names) == (12 if release else 16) + assert len(names) == (14 if release else 18) @pytest.mark.parametrize("release", [False, True], ids=["pull-request", "release"]) @@ -114,9 +114,16 @@ def test_console_evidence_is_retained_on_success_and_failure(tmp_path: Path) -> )) evidence.add("dist/dogfood-installer-console-failure/2-install.failure.screen.txt") evidence.add("dist/dogfood-installer-console/3-disk.login-screen.log") + evidence.update(f"dist/dogfood-installer-diskless-console/{name}" for name in ( + "2-install.screen.log", "2-install.ttyS2.log", "3-first-boot.ttyS1.log", + )) private = {f"dist/dogfood-installer-console-failure/{name}" for name in ( "target.raw", "decoy.raw", "OVMF_VARS.fd", "password", "screen.sh", )} + # The diskless console run's root password hash and the credentials it boots with. + private |= {f"dist/dogfood-installer-diskless-console/{name}" for name in ( + "diskless-root.hash", "passwd.hashed-password.root", "import.pull", "target.raw", "decoy.raw", + )} for path in evidence | private: target = tmp_path / path target.parent.mkdir(parents=True, exist_ok=True) diff --git a/tests/unit/test_ci_dogfood_preflight.py b/tests/unit/test_ci_dogfood_preflight.py index e15a3505..59b641b4 100644 --- a/tests/unit/test_ci_dogfood_preflight.py +++ b/tests/unit/test_ci_dogfood_preflight.py @@ -56,7 +56,7 @@ def runner(tmp_path: Path, missing: str = "") -> dict[str, str]: kvm = tmp_path / "kvm" kvm.touch() return {"PATH": str(bindir), "OVMF_CODE": str(firmware), "OVMF_CODE_INSECURE": str(firmware), - "OVMF_VARS": str(firmware), "PREFLIGHT_KVM": str(kvm)} + "OVMF_VARS": str(firmware), "OVMF_VARS_FOREIGN": str(firmware), "PREFLIGHT_KVM": str(kvm)} def preflight(env: dict[str, str], script: Path = SCRIPT) -> subprocess.CompletedProcess[str]: @@ -77,7 +77,7 @@ def test_a_missing_host_tool_fails(tmp_path: Path, tool: str) -> None: assert f"::error title=boot-test preflight::{tool}: command not found" in out.stdout -@pytest.mark.parametrize("unset", ["OVMF_CODE", "OVMF_CODE_INSECURE", "OVMF_VARS", "PREFLIGHT_KVM"]) +@pytest.mark.parametrize("unset", ["OVMF_CODE", "OVMF_CODE_INSECURE", "OVMF_VARS", "OVMF_VARS_FOREIGN", "PREFLIGHT_KVM"]) def test_missing_firmware_or_kvm_fails(tmp_path: Path, unset: str) -> None: env = runner(tmp_path) env[unset] = str(tmp_path / "absent") diff --git a/tests/unit/test_ci_image_artifacts.py b/tests/unit/test_ci_image_artifacts.py index f1e286c7..3f5d80a0 100644 --- a/tests/unit/test_ci_image_artifacts.py +++ b/tests/unit/test_ci_image_artifacts.py @@ -154,7 +154,8 @@ def test_full_set_consumers_download_every_image_set_part(job: str) -> None: # The part of the set a dogfood script boots or installs (its first argument) # that it reads besides the catch-all; a later set argument is an update -# source, read whole. +# source, read whole. DOGFOOD_INSTALL=diskless-console makes the installer +# check boot the netboot ESP and pull the OS DDI instead of the stick. BOOTS = { "scripts/dogfood-diskless.sh": "netboot", "scripts/dogfood-install.sh": "netboot", @@ -163,6 +164,7 @@ def test_full_set_consumers_download_every_image_set_part(job: str) -> None: def set_args(bucket: str) -> list[tuple[str, list[str]]]: + """(part of the set the call boots, its set arguments) for each call.""" calls = [] for release in (False, True): out = subprocess.run( @@ -173,7 +175,8 @@ def set_args(bucket: str) -> list[tuple[str, list[str]]]: for line in out.stdout.splitlines(): words = line.split(": ", 1)[1].split() script = next(w for w in words if w.startswith("scripts/dogfood-")) - calls.append((script, [w for w in words if w.startswith("dist/diskless")])) + boots = "netboot" if "DOGFOOD_INSTALL=diskless-console" in words else BOOTS[script] + calls.append((boots, [w for w in words if w.startswith("dist/diskless")])) return calls @@ -186,8 +189,8 @@ def test_a_bucket_downloads_the_parts_its_scripts_read(bucket: str) -> None: every = {"core", "netboot", "installer", "sysupdate", "sysexts"} need: dict[str, set[str]] = {"dist/diskless": set(), "dist/diskless-next": set(), "dist/diskless-broken": set()} - for script, sets in set_args(bucket): - need[sets[0]] |= {"core", BOOTS[script]} + for boots, sets in set_args(bucket): + need[sets[0]] |= {"core", boots} for later in sets[1:]: need[later] |= every assert selected(INCLUDE[bucket]["image"], list(UPLOADS)) == {f"image-set-{p}" for p in need["dist/diskless"]} diff --git a/tests/unit/test_dogfood_firmware.py b/tests/unit/test_dogfood_firmware.py index 8e9e20c2..39f9ffdf 100644 --- a/tests/unit/test_dogfood_firmware.py +++ b/tests/unit/test_dogfood_firmware.py @@ -9,7 +9,8 @@ import pytest FIRMWARE = Path(__file__).resolve().parents[2] / "scripts" / "dogfood-firmware.sh" -LOOKUPS = {"ovmf_code": "OVMF_CODE", "ovmf_code_insecure": "OVMF_CODE_INSECURE", "ovmf_vars": "OVMF_VARS"} +LOOKUPS = {"ovmf_code": "OVMF_CODE", "ovmf_code_insecure": "OVMF_CODE_INSECURE", "ovmf_vars": "OVMF_VARS", + "ovmf_vars_foreign": "OVMF_VARS_FOREIGN"} def lookup(function: str, env: dict[str, str], root: Path) -> subprocess.CompletedProcess[str]: @@ -38,6 +39,8 @@ def test_no_firmware_fails_and_names_the_variable(function: str, variable: str, ("ovmf_code", "OVMF/OVMF_CODE_4M.secboot.fd", "edk2/x64/OVMF_CODE.secboot.4m.fd"), ("ovmf_code_insecure", "OVMF/OVMF_CODE_4M.fd", "edk2/x64/OVMF_CODE.4m.fd"), ("ovmf_vars", "OVMF/OVMF_VARS_4M.fd", "edk2/x64/OVMF_VARS.4m.fd"), + # Never a blank store: the foreign-keys check needs one in user mode. + ("ovmf_vars_foreign", "OVMF/OVMF_VARS_4M.ms.fd", "OVMF/OVMF_VARS_4M.fd"), ]) def test_the_first_existing_candidate_is_chosen(function: str, first: str, second: str, tmp_path: Path) -> None: for name in (first, second): diff --git a/tests/unit/test_dogfood_installer.py b/tests/unit/test_dogfood_installer.py index 891e2fd3..61c9280e 100644 --- a/tests/unit/test_dogfood_installer.py +++ b/tests/unit/test_dogfood_installer.py @@ -14,7 +14,9 @@ import re import shlex +import socket import subprocess +import threading from pathlib import Path import pytest @@ -103,3 +105,126 @@ def test_the_journal_mirror_opens_after_the_flush(script: str) -> None: lines = unit[1].splitlines() assert "ExecStart=journalctl -b -f --no-pager -o short-monotonic" in lines assert "After=systemd-journal-flush.service" in lines + + +# DOGFOOD_INSTALL=diskless-console (#450) types the documented install at a +# diskless node's console and answers systemd-sysinstall's questions. + +def diskless_commands() -> str: + script = ( + f'here={shlex.quote(str(SCRIPT.parent))}\n' + + function("diskless_install_command") + + "diskless_install_command\n" + ) + out = subprocess.run(["bash", "-c", script], capture_output=True, text=True, check=True) + return out.stdout + + +def test_the_documented_diskless_install_leaves_the_disk_question_to_sysinstall() -> None: + commands = diskless_commands() + joined = commands.replace("\\\n", " ") + install = next((line for line in joined.splitlines() if line.startswith("systemd-sysinstall ")), None) + assert install, commands + # A disk named on the command line skips the question the harness answers. + args = shlex.split(install)[1:] + assert args and all(arg.startswith("--") for arg in args), install + + +# QEMU key names for a US layout, read back into the characters they type. +UNSHIFTED = {"spc": " ", "ret": "\n", "ctrl-u": "\x15", "minus": "-", "dot": ".", "slash": "/", "equal": "=", + "comma": ",", "semicolon": ";", "apostrophe": "'", "grave_accent": "`", "backslash": "\\", + "bracket_left": "[", "bracket_right": "]"} +SHIFTED = {"1": "!", "2": "@", "3": "#", "4": "$", "5": "%", "6": "^", "7": "&", "8": "*", "9": "(", "0": ")", + "minus": "_", "equal": "+", "semicolon": ":", "apostrophe": '"', "grave_accent": "~", + "backslash": "|", "bracket_left": "{", "bracket_right": "}", "comma": "<", "dot": ">", "slash": "?"} + + +def typed(key: str) -> str: + if key.startswith("shift-"): + base = key.removeprefix("shift-") + return SHIFTED.get(base, base.upper() if len(base) == 1 and base.isalpha() else f"<{key}>") + return UNSHIFTED.get(key, key if len(key) == 1 and key.isalnum() and not key.isupper() else f"<{key}>") + + +def type_keys(tmp_path: Path, text: str) -> tuple[subprocess.CompletedProcess, list[str]]: + """type_keys on a stand-in QEMU monitor; the sendkey commands it received.""" + received: list[bytes] = [] + with socket.socket(socket.AF_UNIX) as server: + server.bind(str(tmp_path / "t.mon")) + server.listen(1) + server.settimeout(60) + + def serve() -> None: + try: + conn, _ = server.accept() + except OSError: + return + with conn: + while data := conn.recv(4096): + received.append(data) + + thread = threading.Thread(target=serve) + thread.start() + script = 'state="$1"\n' + function("type_keys") + 'type_keys t "$2"\n' + result = subprocess.run(["bash", "-c", script, "type_keys", str(tmp_path), text], + capture_output=True, text=True, timeout=120) + server.close() + thread.join() + lines = b"".join(received).decode().splitlines() + assert all(line.startswith("sendkey ") for line in lines), lines + return result, [line.removeprefix("sendkey ") for line in lines] + + +def test_every_character_of_the_documented_install_is_typed_as_itself(tmp_path: Path) -> None: + # Each distinct character once (typing is paced like a person), plus the + # Ctrl-U that clears a question's preselected answer. + text = "".join(sorted(set(diskless_commands()))) + "\x15yes\n" + result, keys = type_keys(tmp_path, text) + assert result.returncode == 0, result.stderr + assert "".join(typed(k) for k in keys) == text + + +def test_a_character_without_a_key_types_nothing(tmp_path: Path) -> None: + result, keys = type_keys(tmp_path, "ls\u00e9\n") + assert result.returncode != 0 + assert "no key for" in result.stderr + assert keys == [] + + +def wait_question(tmp_path: Path, screen: str, question: str) -> subprocess.CompletedProcess: + (tmp_path / "q.screen.log").write_text(screen, encoding="utf-8") + last_screen = re.search(r"^last_screen\(\) \{.*\}$", TEXT, re.M) + assert last_screen + script = ( + 'set -euo pipefail; state="$1"\n' + 'fail() { echo "FAIL: $*" >&2; exit 1; }\n' + + last_screen[0] + "\n" + function("wait_question") + + "sleep 300 /dev/null 2>&1 &\nqemu_pid=$!\n" + "trap 'kill \"${qemu_pid}\" 2>/dev/null || true' EXIT\n" + 'wait_question q "$2" 2\n' + ) + return subprocess.run(["bash", "-c", script, "wait_question", str(tmp_path), question], + capture_output=True, text=True, timeout=60) + + +ERASE = ("Please type 'keep' to install the OS in addition to what the disk already contains, " + "or 'erase' to erase all data on the disk") + + +def snapshot(text: str, width: int = 40) -> str: + """A screen snapshot as the mirror writes it: folded at the screen width, trailing blanks cut.""" + rows = [text[i:i + width].rstrip() for i in range(0, len(text), width)] + return f"=== screen 1.0 cursor {len(rows) - 1}\n" + "\n".join(rows) + "\n" + + +def test_a_question_wider_than_the_screen_is_found(tmp_path: Path) -> None: + screen = snapshot("Required minimal installation disk size is 2.1G.") + snapshot(f"? {ERASE}: ") + result = wait_question(tmp_path, screen, ERASE) + assert result.returncode == 0, result.stderr + + +def test_a_question_no_longer_on_the_screen_is_not_found(tmp_path: Path) -> None: + screen = snapshot(f"? {ERASE}: erase") + snapshot("Summary: Selected Disk: /dev/vdb") + result = wait_question(tmp_path, screen, ERASE) + assert result.returncode != 0 + assert "the monitor never showed" in result.stderr