Skip to content

Creating Windows Golden Image on KVM host

Reuirements

  1. Windows ISO file
  2. VirtIO Drivers
  3. KVM host
  4. virtual machine

Get Windows ISO File

Download Windows Server or 10/11 from microsoft official website

Note

Path for windows ISO file /var/lib/libvirt/images

Download virtio drivers

Download Virtio Drivers Download Now

Or run the command

wget -P /var/lib/libvirt/images/ "https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/archive-virtio/virtio-win-0.1.285-1/virtio-win.iso"

KVM host

Check if host is capable of 1. Connecting to KVM host 2. Checking if virtualiztion is enabled 3. Check if kvm or libvirt packages are installed

Deploy a VM

Set VM Name

vm_name="VM01"

Set Memory size

vm_mem="8192"

Set nCPU

vm_vcpu="2"

Set Disk Size

vm_disk_size="64"

Set Windows Path

win_img_path="/var/lib/libvirt/images/windows.iso"

Set virtio driver path

virtio_driver_path="/var/lib/libvirt/images/virtio-win.iso"

Run the command

virt-install \
  --name ${vm_name}$ \
  --ram ${vm_mem} \
  --vcpus ${vm_vcpu} \
  --cpu host-passthrough \
  --machine q35 \
  --os-variant win2k22 \
  --disk path=/var/lib/libvirt/images/${vm_name}$.qcow2,bus=virtio,size=${vm_disk_size},cache=writeback \
  --cdrom ${win_img_path} \
  --disk path=${virtio_driver_path},device=cdrom \
  --network bridge=virbr0,model=virtio \
  --graphics vnc \
  --boot uefi \
  --features smm.state=on,hyperv_vapic=on,hyperv_relaxed=on,hyperv_spinlocks=on,kvm_hidden=on

Go through the installation process

Complete the initial setup of the windows OS If disk is not detected, install virtio drivers for AMD64(CPU arch) and NetKVM

Install virtio drivers

Install/Remove application or packages that are required/not required

Enable RDP

Generalizing Windows OS using sysprep

Launch CMD as Administrator

Run

Run this on Command Prompt
C:\Windows\System32\Sysprep\sysprep.exe /oobe/generalize /shutdown

Info

Sysprep will generalize the system and shutsdown the VM

Note

The qcow2 file of the vm can become be used as windows machine template

Errors and Troubleshooting for windows generalizing

For below error

Error

Sysprep was not able to validate your windows installation Review the log file at %WINDIR%\System32\Sysprep\Panther\setupact.log for details. After resolving the issue, use sysprep to validate your installation again.

Fix

Run the below commmand in powershell admin

Get-AppxPackage -Name Microsoft.WindowsTerminal | Remove-AppxPackage
Get-AppxPackage -AllUsers Microsoft.MicrosoftEdge.Stable | Remove-AppxPackage -AllUsers

Preparing template from windows.qcow2 file

virt-sysprep -a /var/lib/libvirt/images/windows.qcow2

Note

As a best practice we can make the windows template to be ReadOnly