windows-vm: template-first, Packer for rebuilds

Testing a USB device under Windows meant driving a Windows install by hand, and
that turned out to have one genuinely hostile step. The installation media shows
'Press any key to boot from CD or DVD......' for about five seconds; miss it and
the firmware falls through to the empty disk with 'BdsDxe: No bootable option'.
qm sendkey cannot be relied on for it: it injects PS/2 events while a q35 VM has
only a USB keyboard, and the event must land inside that window. In practice the
keys reached the firmware boot menu but never the media prompt.

Two ways out, in the order they are worth the effort:

  template + clone   install once, sysprep /generalize, qm template; every later
                     VM is a clone that boots in about a minute
  Packer             win11.pkr.hcl builds the template unattended. boot_command
                     sends real keystrokes over VNC and repeats the spacebar
                     across the whole prompt window, which is the fix: one press
                     is a race, ten presses is not

The answer file rides on a floppy so Windows finds it at A:\ instead of guessing
which CD-ROM letter it got, and virtio-win is installed so disks and network are
paravirtualised rather than emulated.
This commit is contained in:
ziadarafat committed 2026-11-28 18:11:22 -07:00
1 parent cda5bd83a9
commit 79125a841b
4 files changed
+375

No files matched your search

+87
View File
@@ -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 <id> # boot it, run Windows Update, then inside Windows:
# C:\Windows\System32\Sysprep\sysprep.exe /oobe /generalize /shutdown
qm template <id>
# 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 = [
"<wait><spacebar><wait><spacebar><wait><spacebar><wait>",
]
```
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.
+119
View File
@@ -0,0 +1,119 @@
<?xml version="1.0" encoding="utf-8"?>
<unattend xmlns="urn:schemas-microsoft-com:unattend">
<settings pass="windowsPE">
<component name="Microsoft-Windows-International-Core-WinPE" processorArchitecture="amd64"
publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS">
<SetupUILanguage><UILanguage>en-US</UILanguage></SetupUILanguage>
<InputLocale>en-US</InputLocale>
<SystemLocale>en-US</SystemLocale>
<UILanguage>en-US</UILanguage>
<UserLocale>en-US</UserLocale>
</component>
<component name="Microsoft-Windows-Setup" processorArchitecture="amd64"
publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS">
<RunSynchronous>
<!-- Belt and braces: this VM has TPM + UEFI, but never let a check cost us the run. -->
<RunSynchronousCommand wcm:action="add">
<Order>1</Order>
<Path>reg add HKLM\SYSTEM\Setup\LabConfig /v BypassTPMCheck /t REG_DWORD /d 1 /f</Path>
</RunSynchronousCommand>
<RunSynchronousCommand wcm:action="add">
<Order>2</Order>
<Path>reg add HKLM\SYSTEM\Setup\LabConfig /v BypassSecureBootCheck /t REG_DWORD /d 1 /f</Path>
</RunSynchronousCommand>
<RunSynchronousCommand wcm:action="add">
<Order>3</Order>
<Path>reg add HKLM\SYSTEM\Setup\LabConfig /v BypassRAMCheck /t REG_DWORD /d 1 /f</Path>
</RunSynchronousCommand>
<RunSynchronousCommand wcm:action="add">
<Order>4</Order>
<Path>reg add HKLM\SYSTEM\Setup\LabConfig /v BypassCPUCheck /t REG_DWORD /d 1 /f</Path>
</RunSynchronousCommand>
</RunSynchronous>
<DiskConfiguration>
<WillShowUI>OnError</WillShowUI>
<Disk wcm:action="add">
<DiskID>0</DiskID>
<WillWipeDisk>true</WillWipeDisk>
<CreatePartitions>
<CreatePartition wcm:action="add">
<Order>1</Order><Type>EFI</Type><Size>100</Size>
</CreatePartition>
<CreatePartition wcm:action="add">
<Order>2</Order><Type>MSR</Type><Size>16</Size>
</CreatePartition>
<CreatePartition wcm:action="add">
<Order>3</Order><Type>Primary</Type><Extend>true</Extend>
</CreatePartition>
</CreatePartitions>
<ModifyPartitions>
<ModifyPartition wcm:action="add">
<Order>1</Order><PartitionID>1</PartitionID><Format>FAT32</Format><Label>System</Label>
</ModifyPartition>
<ModifyPartition wcm:action="add">
<Order>2</Order><PartitionID>2</PartitionID>
</ModifyPartition>
<ModifyPartition wcm:action="add">
<Order>3</Order><PartitionID>3</PartitionID><Format>NTFS</Format><Label>Windows</Label><Letter>C</Letter>
</ModifyPartition>
</ModifyPartitions>
</Disk>
</DiskConfiguration>
<ImageInstall>
<OSImage>
<InstallTo><DiskID>0</DiskID><PartitionID>3</PartitionID></InstallTo>
</OSImage>
</ImageInstall>
<UserData>
<AcceptEula>true</AcceptEula>
<FullName>kraken</FullName>
<Organization>homelab</Organization>
</UserData>
</component>
</settings>
<settings pass="specialize">
<component name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64"
publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS">
<ComputerName>WINTEST</ComputerName>
<TimeZone>UTC</TimeZone>
</component>
</settings>
<settings pass="oobeSystem">
<component name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64"
publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS">
<OOBE>
<HideEULAPage>true</HideEULAPage>
<HideOEMRegistrationScreen>true</HideOEMRegistrationScreen>
<HideOnlineAccountScreens>true</HideOnlineAccountScreens>
<HideWirelessSetupInOOBE>true</HideWirelessSetupInOOBE>
<ProtectYourPC>3</ProtectYourPC>
<SkipMachineOOBE>true</SkipMachineOOBE>
<SkipUserOOBE>true</SkipUserOOBE>
</OOBE>
<UserAccounts>
<LocalAccounts>
<LocalAccount wcm:action="add">
<Name>kraken</Name>
<Group>Administrators</Group>
<Password><Value>kraken123!</Value><PlainText>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>
+16
View File
@@ -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
+153
View File
@@ -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",
]
}
}