diff --git a/e2e/platforms/qemu_windows/build-base.sh b/e2e/platforms/qemu_windows/build-base.sh new file mode 100755 index 00000000..90133ed6 --- /dev/null +++ b/e2e/platforms/qemu_windows/build-base.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash +# Builds the BASE of the golden Windows image: the unattended install of +# w0-install.sh up to a desktop with auto-login, the Visual C++ runtime and the +# guest agent in the Run key, and nothing else. That is almost all of the time +# of a build (about 18 minutes of 20), and it rarely changes, so it is built +# once; build-golden.sh puts the rest on top of it in about 2.5 minutes. +# +# Rebuild it for a new ISO, when the agent needs something more in the image, +# or when the 90-day evaluation runs out. +# +# usage: build-base.sh ISO AGENT_EXE OUTDIR +# needs on PATH: xorriso qemu-system-x86_64 qemu-img swtpm socat +# needs in env: OVMF_FD (an OVMF build with tpmSupport, without SMM) +set -euo pipefail + +iso=$1 agent=$2 out=$3 +here=$(cd "$(dirname "$0")" && pwd) + +mkdir -p "$out" +stage=$(mktemp -d) +trap 'rm -rf "$stage"' EXIT + +# autounattend.xml runs `setup.ps1` from the first drive that has one +cp "$here/autounattend.xml" "$agent" "$stage/" +cp "$here/setup-base.ps1" "$stage/setup.ps1" +xorriso -as mkisofs -J -r -V ANSWER -o "$out/answer.iso" "$stage" + +"$here/w0-install.sh" "$iso" "$out/answer.iso" "$out" + +# the installed disk is the base of the golden image: leave it alone +chmod a-w "$out/disk.qcow2" +echo "base image: $out/disk.qcow2 ($(date -Is))" diff --git a/e2e/platforms/qemu_windows/build-golden.sh b/e2e/platforms/qemu_windows/build-golden.sh new file mode 100755 index 00000000..6a81ff9b --- /dev/null +++ b/e2e/platforms/qemu_windows/build-golden.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +# Builds the golden Windows image on top of a base from build-base.sh: boots +# an overlay of the base, provisions it through the guest agent (provision.ps1: +# policies, Defender, the display driver) and shuts it down, in about 2.5 +# minutes. OUTDIR gets disk.qcow2, which is backed by the base through a +# relative path (so the two can move together), vars.fd and tpm/; the per-run +# overlays are backed by that. +# +# usage: build-golden.sh BASE_DIR OUTDIR QXLDOD_DIR +# QXLDOD_DIR is virtio-win's qxldod/w10/amd64, the display driver that gives a +# second QXL device a monitor. +# needs on PATH: qemu-system-x86_64 qemu-img swtpm +# needs in env: OVMF_FD (an OVMF build with tpmSupport, without SMM) +# run from e2e/ with PYTHONPATH=lib:. (the nix app does) +set -euo pipefail + +exec python -m platforms.qemu_windows.build_golden "$@" diff --git a/e2e/platforms/qemu_windows/build-ref.sh b/e2e/platforms/qemu_windows/build-ref.sh new file mode 100755 index 00000000..03ffe79f --- /dev/null +++ b/e2e/platforms/qemu_windows/build-ref.sh @@ -0,0 +1,41 @@ +#!/usr/bin/env bash +# Builds the binaries of another komorebi commit, to run the e2e scenarios +# against them: `KOMOREBI_E2E_BINS=OUTDIR nix run .#e2e-windows -- ...`. +# +# usage: build-ref.sh REF OUTDIR +# REF is anything git knows (a commit, a branch, a tag); it is fetched from +# origin if it is not here yet. The checkout of REF is built with this +# tree's flake.nix and flake.lock, because the nix build of an older commit +# may not work any more; the test window is this tree's. OUTDIR gets +# komorebi.exe, komorebic.exe, komorebi-testwin.exe and `commit`, the full +# hash, which the runner writes into every result.json. +# needs on PATH: git nix +set -euo pipefail + +ref=$1 out=$2 +here=$(cd "$(dirname "$0")" && pwd) +root=$(git -C "$here" rev-parse --show-toplevel) + +if ! git -C "$root" cat-file -e "$ref^{commit}" 2>/dev/null; then + git -C "$root" fetch origin "+refs/heads/*:refs/remotes/origin/*" +fi +commit=$(git -C "$root" rev-parse "$ref^{commit}") + +tree=$(mktemp -d) +cleanup() { + git -C "$root" worktree remove --force "$tree" 2>/dev/null || rm -rf "$tree" +} +trap cleanup EXIT +git -C "$root" worktree add --detach "$tree" "$commit" +cp "$root/flake.nix" "$root/flake.lock" "$tree/" + +mkdir -p "$out" +for package in komorebi komorebic; do + path=$(nix build "$tree#$package" --no-link --print-out-paths) + cp "$path/bin/$package.exe" "$out/" +done +path=$(nix build "$root#komorebi-testwin" --no-link --print-out-paths) +cp "$path/bin/komorebi-testwin.exe" "$out/" +chmod u+w "$out"/*.exe +echo "$commit" > "$out/commit" +echo "built $commit into $out" diff --git a/e2e/platforms/qemu_windows/build_golden.py b/e2e/platforms/qemu_windows/build_golden.py new file mode 100644 index 00000000..3576149a --- /dev/null +++ b/e2e/platforms/qemu_windows/build_golden.py @@ -0,0 +1,61 @@ +"""Builds the golden layer on top of a base image. + +usage: python -m platforms.qemu_windows.build_golden BASE OUT QXLDOD_DIR + +Boots an overlay of BASE with the host's clock (the Visual C++ download needs +TLS), provisions it through the agent, has the guest shut itself down, and +leaves OUT/disk.qcow2 backed by BASE through a relative path, with the +firmware variables and the TPM state as they are after that shutdown. +""" + +import os +import shutil +import subprocess +import sys +from pathlib import Path + +from .guest import Guest + +HERE = Path(__file__).parent +DRIVER_FILES = ("qxldod.inf", "qxldod.cat", "qxldod.sys") + + +def main(argv: list[str]) -> int: + base, out, driver = (Path(a).resolve() for a in argv[:3]) + shutil.rmtree(out, ignore_errors=True) + # the overlay, firmware variables and TPM state of the run are the + # artifacts, so the run directory stays + with Guest(base, out, rtc_base=None, keep=True) as guest: + agent = guest.agent + agent.exec("cmd", "/c", "mkdir C:\\e2e\\drv 2>nul") + for name in DRIVER_FILES: + agent.put_file(driver / name, f"C:\\e2e\\drv\\{name}") + agent.put_file(HERE / "provision.ps1", "C:\\e2e\\provision.ps1") + result = agent.exec( + "powershell", "-NoProfile", "-ExecutionPolicy", "Bypass", + "-File", "C:\\e2e\\provision.ps1", + ) + print(agent.get_file("C:\\e2e-provision.log").decode()) + if result["status"] != 0: + raise RuntimeError(f"provisioning failed: {result}") + agent.exec("cmd", "/c", "shutdown /s /t 0") + agent.close() + if not guest.wait_exit(300): + raise RuntimeError("the guest did not shut down") + # the swtpm of the run has been stopped, so its state is flushed + for sock in out.glob("*.sock"): + sock.unlink() + disk = out / "disk.qcow2" + (out / "overlay.qcow2").rename(disk) + relative = os.path.relpath(base / "disk.qcow2", out) + subprocess.run( + ["qemu-img", "rebase", "-u", "-b", relative, "-F", "qcow2", str(disk)], + check=True, + ) + disk.chmod(0o444) + print(f"golden image: {disk} on {relative}") + return 0 + + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) diff --git a/e2e/platforms/qemu_windows/provision.ps1 b/e2e/platforms/qemu_windows/provision.ps1 new file mode 100644 index 00000000..e691f85a --- /dev/null +++ b/e2e/platforms/qemu_windows/provision.ps1 @@ -0,0 +1,45 @@ +# GOLDEN layer provisioning, run in the guest through the agent. The steps of +# the former single-step setup (policies, Defender, VC++ runtime, widgets, QXL driver, firewall); +# the agent copy / Run key / shutdown are not here: the base has the agent and +# the host shuts the guest down. Expects the qxldod files in C:\e2e\drv. +$ErrorActionPreference = 'Continue' +$log = 'C:\e2e-provision.log' +function Log($m) { "$(Get-Date -Format o) $m" | Add-Content $log } +Log "provision start; elevated=$(([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) user=$env:USERNAME" + +$policies = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies' +Set-ItemProperty "$policies\System" EnableLUA 0 +New-Item -Force 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\Personalization' | + New-ItemProperty -Name NoLockScreen -Value 1 -Force | Out-Null +New-Item -Force 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\System' | + New-ItemProperty -Name DisableLockWorkstation -Value 1 -Force | Out-Null +Set-ItemProperty 'HKCU:\Control Panel\Desktop' ScreenSaveActive 0 +powercfg /x -monitor-timeout-ac 0 +powercfg /x -standby-timeout-ac 0 +powercfg /h off +Log 'policies done' + +Set-MpPreference -DisableRealtimeMonitoring $true +New-Item -Force 'HKLM:\SOFTWARE\Policies\Microsoft\Windows Defender' | + New-ItemProperty -Name DisableAntiSpyware -Value 1 -Force | Out-Null +foreach ($s in 'wuauserv', 'UsoSvc', 'WaaSMedicSvc') { + Stop-Service $s -Force -ErrorAction SilentlyContinue + Set-Service $s -StartupType Disabled -ErrorAction SilentlyContinue +} +Log 'defender/update done' + +$ProgressPreference = 'SilentlyContinue' +Invoke-WebRequest https://aka.ms/vs/17/release/vc_redist.x64.exe ` + -OutFile C:\vc_redist.x64.exe +Log 'vc redist downloaded' +Start-Process C:\vc_redist.x64.exe -ArgumentList /quiet,/norestart -Wait +Remove-Item C:\vc_redist.x64.exe -ErrorAction SilentlyContinue +New-Item -Force 'HKLM:\SOFTWARE\Policies\Microsoft\Dsh' | + New-ItemProperty -Name AllowNewsAndInterests -Value 0 -Force | Out-Null +Log "vc redist: $(Test-Path C:\Windows\System32\vcruntime140.dll)" + +pnputil /add-driver C:\e2e\drv\qxldod.inf /install | Out-Null +Log "qxl driver stored: $(pnputil /enum-drivers | Select-String -Pattern 'qxldod' -Quiet)" + +Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled False +Log 'provision done' diff --git a/e2e/platforms/qemu_windows/setup-base.ps1 b/e2e/platforms/qemu_windows/setup-base.ps1 new file mode 100644 index 00000000..4defa4e8 --- /dev/null +++ b/e2e/platforms/qemu_windows/setup-base.ps1 @@ -0,0 +1,25 @@ +# BASE layer first-logon: only what is needed to reach a desktop with an +# elevated agent running at every logon. Provisioning is the golden layer's job. +$log = 'C:\e2e-setup.log' +function Log($m) { "$(Get-Date -Format o) $m" | Add-Content $log } +Log 'base setup start' +# the agent is the transport of the golden layer and must run elevated +# (UAC off takes effect at the next boot, which is the layer build's) +Set-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System' EnableLUA 0 +$drive = Get-PSDrive -PSProvider FileSystem | + Where-Object { Test-Path (Join-Path $_.Root 'komorebi-e2e-agent.exe') } | + Select-Object -First 1 +New-Item -Force -ItemType Directory C:\e2e\bin | Out-Null +Copy-Item (Join-Path $drive.Root 'komorebi-e2e-agent.exe') C:\e2e\bin +Set-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run' ` + KomorebiE2eAgent 'C:\e2e\bin\komorebi-e2e-agent.exe' +# the agent links the VC++ runtime dynamically, so it cannot start without it +$ProgressPreference = 'SilentlyContinue' +Invoke-WebRequest https://aka.ms/vs/17/release/vc_redist.x64.exe -OutFile C:\vc_redist.x64.exe +Start-Process C:\vc_redist.x64.exe -ArgumentList /quiet,/norestart -Wait +Remove-Item C:\vc_redist.x64.exe -ErrorAction SilentlyContinue +Log "vc redist: $(Test-Path C:\Windows\System32\vcruntime140.dll)" +# the host reaches the agent through a QEMU port forward +Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled False +Log 'base setup done' +shutdown /s /t 5 diff --git a/e2e/platforms/qemu_windows/setup.ps1 b/e2e/platforms/qemu_windows/setup.ps1 deleted file mode 100644 index aaaafeb8..00000000 --- a/e2e/platforms/qemu_windows/setup.ps1 +++ /dev/null @@ -1,29 +0,0 @@ -# First-logon provisioning for the e2e golden image. Runs from the answer -# file ISO; finishes by shutting the guest down so the host sees it exit. -$ErrorActionPreference = 'Continue' -$log = 'C:\e2e-setup.log' -function Log($m) { "$(Get-Date -Format o) $m" | Add-Content $log } -Log 'setup start' - -# no UAC prompts, no lock screen, no screensaver, no display or sleep timeout -Set-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System' EnableLUA 0 -New-Item -Force 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\Personalization' | - New-ItemProperty -Name NoLockScreen -Value 1 -Force | Out-Null -New-Item -Force 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\System' | - New-ItemProperty -Name DisableLockWorkstation -Value 1 -Force | Out-Null -Set-ItemProperty 'HKCU:\Control Panel\Desktop' ScreenSaveActive 0 -powercfg /x -monitor-timeout-ac 0 -powercfg /x -standby-timeout-ac 0 -powercfg /h off - -# no Defender real-time scanning, no Windows Update -Set-MpPreference -DisableRealtimeMonitoring $true -New-Item -Force 'HKLM:\SOFTWARE\Policies\Microsoft\Windows Defender' | - New-ItemProperty -Name DisableAntiSpyware -Value 1 -Force | Out-Null -foreach ($s in 'wuauserv', 'UsoSvc', 'WaaSMedicSvc') { - Stop-Service $s -Force -ErrorAction SilentlyContinue - Set-Service $s -StartupType Disabled -ErrorAction SilentlyContinue -} - -Log 'setup done' -shutdown /s /t 5 diff --git a/e2e/platforms/qemu_windows/w0-boot.sh b/e2e/platforms/qemu_windows/w0-boot.sh index 37d18a78..b0220b1a 100755 --- a/e2e/platforms/qemu_windows/w0-boot.sh +++ b/e2e/platforms/qemu_windows/w0-boot.sh @@ -11,6 +11,8 @@ set -euo pipefail work=$1 limit=${2:-300} : "${OVMF_FD:?set OVMF_FD to an OVMF fd store path with TPM support}" : "${VCPUS:=4}" "${MEM:=6G}" +# an evaluation build reads as expired when the guest clock is the host's +: "${RTC_BASE:=2024-10-01T00:00:00}" rm -rf "$work/boot" "$work/overlay.qcow2" "$work/qmp.sock" "$work/tpm.sock" mkdir -p "$work/boot" @@ -26,6 +28,7 @@ start=$(date +%s) echo "boot start: $(date -Is)" qemu-system-x86_64 -name w0-boot -accel kvm -machine q35,smm=off \ -cpu host -smp "$VCPUS" -m "$MEM" \ + -rtc "base=$RTC_BASE" \ -drive "if=pflash,format=raw,readonly=on,unit=0,file=$OVMF_FD/FV/OVMF_CODE.fd" \ -drive "if=pflash,format=raw,unit=1,file=$work/vars-boot.fd" \ -chardev "socket,id=chrtpm,path=$work/tpm.sock" \ diff --git a/e2e/platforms/qemu_windows/w0-install.sh b/e2e/platforms/qemu_windows/w0-install.sh index 7af1370d..33228697 100755 --- a/e2e/platforms/qemu_windows/w0-install.sh +++ b/e2e/platforms/qemu_windows/w0-install.sh @@ -54,9 +54,9 @@ qemu-system-x86_64 -name w0-install -accel kvm -machine q35,smm=off \ & qpid=$! -# "Press any key to boot from CD or DVD": tap space for the first minute. -# Later reboots must reach the disk, so stop after that. -for _ in $(seq 1 60); do +# "Press any key to boot from CD or DVD": tap space for the first 20 s only; a +# later press would land on the Cancel button of the install progress page. +for _ in $(seq 1 20); do sleep 1 kill -0 "$qpid" 2>/dev/null || break [ -S "$work/qmp.sock" ] && press spc || true diff --git a/flake.nix b/flake.nix index 980df7b5..a26241f3 100644 --- a/flake.nix +++ b/flake.nix @@ -303,13 +303,22 @@ : "''${E2E_GOLDEN:?set E2E_GOLDEN to the golden image directory}" out=''${1:?usage: e2e-windows OUT SCENARIO...} shift - bins=$(mktemp -d) - trap 'rm -rf "$bins"' EXIT - cp ${build.komorebi}/bin/komorebi.exe "$bins" - cp ${build.komorebic}/bin/komorebic.exe "$bins" - cp ${build.komorebi-testwin}/bin/komorebi-testwin.exe "$bins" + # the binaries under test: this checkout's, or those of another + # commit, made by e2e/platforms/qemu_windows/build-ref.sh + if [ -n "''${KOMOREBI_E2E_BINS:-}" ]; then + bins=$KOMOREBI_E2E_BINS + if [ -f "$bins/commit" ]; then + KOMOREBI_E2E_COMMIT=$(cat "$bins/commit") + fi + else + bins=$(mktemp -d) + trap 'rm -rf "$bins"' EXIT + cp ${build.komorebi}/bin/komorebi.exe "$bins" + cp ${build.komorebic}/bin/komorebic.exe "$bins" + cp ${build.komorebi-testwin}/bin/komorebi-testwin.exe "$bins" + fi export OVMF_FD=${ovmfWithTpm} - export KOMOREBI_E2E_COMMIT=${self.rev or "dirty"} + export KOMOREBI_E2E_COMMIT=''${KOMOREBI_E2E_COMMIT:-${self.rev or "dirty"}} export PYTHONPATH=${./e2e/lib}:${./e2e} exec python -m platforms.qemu_windows.run \ "$E2E_GOLDEN" "$bins" "$out" "$@" @@ -424,6 +433,7 @@ # the neutral scenarios against a throwaway Windows guest: # E2E_GOLDEN= nix run .#e2e-windows -- \ # OUT SCENARIO... + # KOMOREBI_E2E_BINS= tests another commit # the golden image is built once with # e2e/platforms/qemu_windows/build-golden.sh e2e-windows = {