diff --git a/tools/windows-vm/README.md b/tools/windows-vm/README.md new file mode 100644 index 0000000..556ad03 --- /dev/null +++ b/tools/windows-vm/README.md @@ -0,0 +1,87 @@ +# Throwaway Windows VMs on Proxmox + +Two ways to get a Windows guest for a test like "does this USB device work under +Windows", in the order they are worth using. + +## 1. Template + clone (the everyday path) + +One interactive install, then every later VM is a clone that boots in about a +minute. + +```sh +# after the guest is installed and updated +qm start # boot it, run Windows Update, then inside Windows: +# C:\Windows\System32\Sysprep\sysprep.exe /oobe /generalize /shutdown +qm template + +# every time after that +qm clone 9000 150 --name win-test --full +qm set 150 --memory 8192 --cores 4 +qm set 150 -hostpci0 0000:0c:00.3,pcie=1 # if the test needs USB hardware +qm start 150 +``` + +A clone keeps the template's SID until sysprep strips it, which is why the +template is generalised rather than merely shut down. + +## 2. Packer rebuild (when you want a clean, reproducible image) + +`win11.pkr.hcl` builds the template from the ISO with nobody watching. It exists +because the hand-driven install has one hostile step, described below. + +```sh +packer init . +packer build -var proxmox_token="$PROXMOX_PACKER_TOKEN" win11.pkr.hcl +``` + +Needs `local:iso/win11-eval.iso` and `local:iso/virtio-win.iso`, and an API token +(user `packer@pve`, role with `VM.Allocate`, `VM.Config.*`, `Datastore.AllocateSpace`). +Keep the token in `secretctl`, never in this file. + +## The step that makes Windows installs awkward + +Windows installation media prints + +``` +Press any key to boot from CD or DVD...... +``` + +for about five seconds. Miss it and the firmware falls through to the empty +disk and stops: `BdsDxe: No bootable option or device was found.` + +`qm sendkey` is not a reliable way to satisfy it. It injects PS/2 keyboard +events, while a q35 VM has only a USB keyboard, and the event has to land inside +a five-second window. On 2026-11-28 that cost several failed boots: the keys +reached the *firmware boot menu* (which redrew) but never the media's prompt. + +Packer's `boot_command` sends real keystrokes over VNC and lets you repeat them +across the whole window: + +```hcl +boot_command = [ + "", +] +``` + +That repetition is the fix — one press is a race, ten presses is not. + +## Files + +| File | Purpose | +|---|---| +| `win11.pkr.hcl` | Packer build: VM, UEFI + vTPM, virtio disk/NIC, answer file, sysprep, template | +| `answer/autounattend.xml` | Unattended install: disk layout, local admin, auto-logon, OOBE skipped | +| `answer/firstboot.ps1` | First-logon hook: enable WinRM so Packer can drive the rest | + +## Notes + +- Deliver the answer file on a **floppy** (`floppy_files`), not a second CD-ROM: + Windows then finds it at `A:\` and there is no drive-letter guessing. +- `virtio-win` gives paravirtualised disk and network. Without it the guest runs + on emulated SATA and `e1000`, which is fine for a driver test but slow. +- `qemu_agent = true` means you can `qm shutdown`/`qm guest exec` later instead + of power-cycling or mounting the disk. +- For USB hardware tests, pass the whole PCIe controller (`hostpci0`) rather than + a single device: a device passthrough is still enumerated by the host kernel + first, so it inherits any host-side fault. Check the controller's IOMMU group + is alone before relying on it. diff --git a/tools/windows-vm/answer/autounattend.xml b/tools/windows-vm/answer/autounattend.xml new file mode 100644 index 0000000..4344deb --- /dev/null +++ b/tools/windows-vm/answer/autounattend.xml @@ -0,0 +1,119 @@ + + + + + en-US + en-US + en-US + en-US + en-US + + + + + + 1 + reg add HKLM\SYSTEM\Setup\LabConfig /v BypassTPMCheck /t REG_DWORD /d 1 /f + + + 2 + reg add HKLM\SYSTEM\Setup\LabConfig /v BypassSecureBootCheck /t REG_DWORD /d 1 /f + + + 3 + reg add HKLM\SYSTEM\Setup\LabConfig /v BypassRAMCheck /t REG_DWORD /d 1 /f + + + 4 + reg add HKLM\SYSTEM\Setup\LabConfig /v BypassCPUCheck /t REG_DWORD /d 1 /f + + + + OnError + + 0 + true + + + 1EFI100 + + + 2MSR16 + + + 3Primarytrue + + + + + 11FAT32 + + + 22 + + + 33NTFSC + + + + + + + 03 + + + + true + kraken + homelab + + + + + + + WINTEST + UTC + + + + + + + true + true + true + true + 3 + true + true + + + + + kraken + Administrators + kraken123!true</PlainText></Password> + </LocalAccount> + </LocalAccounts> + </UserAccounts> + <AutoLogon> + <Enabled>true</Enabled> + <Username>kraken</Username> + <Password><Value>kraken123!</Value><PlainText>true</PlainText></Password> + <LogonCount>999</LogonCount> + </AutoLogon> + <FirstLogonCommands> + <SynchronousCommand wcm:action="add"> + <Order>1</Order> + <Description>collect USB report</Description> + <CommandLine>cmd /c if exist A:\firstboot.ps1 powershell.exe -ExecutionPolicy Bypass -NoProfile -File A:\firstboot.ps1</CommandLine> + </SynchronousCommand> + </FirstLogonCommands> + </component> + </settings> +</unattend> diff --git a/tools/windows-vm/answer/firstboot.ps1 b/tools/windows-vm/answer/firstboot.ps1 new file mode 100644 index 0000000..b33df8f --- /dev/null +++ b/tools/windows-vm/answer/firstboot.ps1 @@ -0,0 +1,16 @@ +# Runs once on first logon in a freshly installed guest, before Packer's +# provisioners take over over WinRM. Keep it short: everything heavier is a +# Packer provisioner so it is visible in the build log. +$ErrorActionPreference = 'SilentlyContinue' +Set-Content -Path 'C:\firstboot.txt' -Value "firstboot $([DateTime]::UtcNow.ToString('s'))" + +# WinRM is how Packer drives the rest of the build. +Enable-PSRemoting -Force -SkipNetworkProfileCheck +Set-Item WSMan:\localhost\Service\Auth\Basic -Value $true +Set-Service WinRM -StartupType Automatic +Start-Service WinRM +Set-NetFirewallRule -DisplayGroup 'Windows Remote Management' -Enabled True + +# A test guest that sleeps, never hibernates, and has no lock screen. +powercfg /change standby-timeout-ac 0 +powercfg /change monitor-timeout-ac 0 diff --git a/tools/windows-vm/win11.pkr.hcl b/tools/windows-vm/win11.pkr.hcl new file mode 100644 index 0000000..ffa2d61 --- /dev/null +++ b/tools/windows-vm/win11.pkr.hcl @@ -0,0 +1,153 @@ +# Builds a Windows 11 Proxmox template with no human in the loop. +# +# Why this exists: a hand-driven Windows install has one genuinely hostile +# step. The installation media shows "Press any key to boot from CD or DVD" +# for about five seconds, and if no key arrives the firmware falls through to +# the empty disk and stops with "No bootable option or device was found". +# `qm sendkey` cannot reliably satisfy it: it injects PS/2 events, while a +# modern q35 VM has only a USB keyboard, and the timing has to land inside +# that window. +# +# Packer solves it the way it solves it everywhere: `boot_command` sends real +# keystrokes over VNC, so the <spacebar> repeat below lands whenever the +# prompt appears, and every <wait> is a real pause rather than a guess. +# +# Build: packer init . && packer build -var-file=secrets.pkrvars.hcl win11.pkr.hcl +# Needs: win11 ISO + virtio-win.iso in local:iso, an API token with VM.* rights. + +packer { + required_plugins { + proxmox = { + version = ">= 1.1.8" + source = "github.com/hashicorp/proxmox" + } + } +} + +variable "proxmox_url" { + type = string + default = "https://10.0.0.23:8006/api2/json" +} + +variable "proxmox_token" { + type = string + sensitive = true +} + +variable "iso_file" { + type = string + default = "local:iso/win11-eval.iso" +} + +variable "virtio_iso" { + type = string + default = "local:iso/virtio-win.iso" +} + +variable "template_name" { + type = string + default = "win11-test-template" +} + +source "proxmox-iso" "win11" { + proxmox_url = var.proxmox_url + username = "packer@pve!packer" + token = var.proxmox_token + insecure_skip_tls_verify = true + node = "ziadpve" + vm_id = 9100 + vm_name = var.template_name + + # UEFI + vTPM: Windows 11 refuses to install without them, and a test guest + # that pretends to have them is worth less than one that does. + bios = "ovmf" + machine = "q35" + efi_config { + efi_storage_pool = "vmstore" + efi_type = "4m" + pre_enrolled_keys = true + } + tpm_config { + tpm_storage_pool = "vmstore" + tpm_version = "v2.0" + } + + cores = 4 + memory = 8192 + os = "win11" + scsi_controller = "virtio-scsi-single" + + disks { + disk_size = "64G" + storage_pool = "vmstore" + type = "virtio" + discard = true + } + + network_adapters { + model = "virtio" + bridge = "vmbr0" + } + + iso_file = var.iso_file + unmount_iso = true + additional_iso_files { + device = "ide2" + iso_file = var.virtio_iso + unmount = true + iso_storage_pool = "local" + } + + # The answer file rides on a floppy, so Windows Setup finds it at A:\ and it + # never collides with a CD-ROM drive letter. + floppy_files = [ + "answer/autounattend.xml", + "answer/firstboot.ps1", + ] + + # This is the whole point of the exercise. Repeat the spacebar rather than + # pressing once: the prompt renders a variable beat after the DVD is selected. + boot = "order=ide0;virtio0" + boot_wait = "4s" + boot_command = [ + "<wait><spacebar><wait><spacebar><wait><spacebar><wait><spacebar><wait>", + "<spacebar><wait><spacebar><wait><spacebar><wait><spacebar><wait>", + "<spacebar><wait><spacebar><wait><spacebar><wait><spacebar><wait>", + ] + + # Wait for Setup to finish, reboot, and land on the desktop; then let + # sysprep/generalise (invoked by firstboot.ps1) shut the machine down. + communicator = "winrm" + winrm_username = "kraken" + winrm_password = "kraken123!" + winrm_timeout = "2h" + + qemu_agent = true + + # Convert the finished build into a template: it can only be cloned, never + # started by accident. + template_name = var.template_name + additional_arguments = "-device usb-kbd" +} + +build { + sources = ["source.proxmox-iso.win11"] + + # Guest-agent + virtio drivers come off the second ISO, so disks and network + # are paravirtualised instead of the slow emulated fallbacks. + provisioner "powershell" { + inline = [ + "Get-ChildItem D:\\ -Filter virtio-win-guest-tools.exe -Recurse | Select-Object -First 1 | ForEach-Object { Start-Process $_.FullName -ArgumentList '/S' -Wait }", + "Set-Service -Name WinRM -StartupType Automatic", + ] + } + + # Generalise so every clone gets its own SID, then power off for templating. + provisioner "powershell" { + inline = [ + "& $env:SystemRoot\\System32\\Sysprep\\Sysprep.exe /oobe /generalize /quiet /quit", + "while ($true) { $s = (Get-Service Sysprep).Status; if ($s -eq 'Stopped') { break }; Start-Sleep -Seconds 10 }", + "Stop-Computer -Force", + ] + } +}