howto

KVM Windows Guests

QEMU/KVM Windows Guests installation and management examples

  • NB! Only virtio drivers provide acceptable disk/network IO levels - so you should use them!
  • NB! At the moment we are standardized on using QCow2 image format - using 'preallocation=metadata' option provides better speed - so disk metadata preallocation is highly suggested! ### Windows Server 2008R2

virt-manager ISO install

  • Enter VM hostname
  • Choose local install media

  • Enter ISO path or click on 'Browse'
  • Set correct OS type and version

  • If choosed to browse previously then click on local-iso pool and pick needed ISO file

  • Set VM memory
  • Set VM vCPUs

  • Create VM disk - default format is set in virt-manager preferences - we suggest to set it to qcow2

  • Leave 'Customize configuration on' - as we need to perform some changes still
  • Choose 'Advanced Options' -> Specify shared device name' -> Bridge name: vmbr0
  • Click 'Finish'

  • Choose Disk1
  • Set disk bus/driver to virtio

  • Choose NIC
  • Set NIC driver to Intel E1000

  • Choose 'Add Hardware' -> Storage
  • Add Windows virtio drivers ISO cdrom disk
  • Click 'Finish'
  • Click 'Begin Installation'

virt-install ISO install

# Place WinServ ISO into /storage/local/iso/
# Download VIRTIO driver iso image to use during install
cd /storage/local/iso/
wget http://alt.fedoraproject.org/pub/alt/virtio-win/latest/images/bin/virtio-win-0.1-52.iso

# Pre-create VM disk image
qemu-img create -f qcow2 -o preallocation=metadata /storage/local/images/winserv1.qcow2 50G

# Launch installation inside screen
screen
virt-install --connect qemu:///system --name WinServ1 --ram 2048 --vcpus 2 \
--disk path=/storage/local/images/winserv1.qcow2,format=qcow2,bus=virtio,cache=none \
--disk path=/storage/local/iso/virtio-win-0.1-52.iso,device=cdrom \
--cdrom /storage/local/iso/winserv.iso \
--network=network:default,model=e1000 \
--vnc --os-type=windows --os-variant=win2k8 \
--noautoconsole --accelerate --noapic --keymap=en-us
# Detach screen
CTRL+A+D

# Run virt-manager for VNC display 
# (make sure you have X forwarding enabled on your ssh session for remote display)
virt-manager
# OR
virt-viewer WinServ1

# NB! After Windows installation please dont forget to remove the cdrom devices (live version)
virsh attach-disk WinServ1 '' hda --mode readonly --type cdrom
virsh attach-disk WinServ1 '' hdc --mode readonly --type cdrom
  • Windows installer wont find any disks until you load windows virtio drivers

  • Please browse drive E: (CDROM)

  • Choose WLH->AMD64 for 64bit Guest or WLH->X86 for 32bit Guest

  • Choose 'Red Hat VirtIO SCSI controller' and click 'Next'

  • Pre-prepared disk should appear in installer

Install from VM template

# Get opennode TUI management menu
opennode

# Deploy QEMU/KVM template
# Choose 'Create VM' -> KVM -> Pick template -> Insert required parameters -> Hit 'Create VM'

# Set VM domain name (what you entered as VM hostname in TUI form)
VMDOMAIN=vmhostname

# Dump Guest Definition XML
virsh dumpxml $VMDOMAIN > /storage/local/images/$VMDOMAIN.xml

# Rewrite disk bus and other misc parts
sed -i "s/driver name='qemu' type='qcow2'/driver name='qemu' type='qcow2' cache='none'/" /storage/local/images/$VMDOMAIN.xml
sed -i "s/target dev='hda' bus='ide'/target dev='vda' bus='virtio'/" /storage/local/images/$VMDOMAIN.xml
sed -i "s/address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/" /storage/local/images/$VMDOMAIN.xml
sed -i "s/address type='drive' controller='0' bus='0' target='0' unit='0'/address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/" /storage/local/images/$VMDOMAIN.xml
sed -i "s/<clock offset='utc'\/>/<clock offset='localtime'><timer name='rtc' tickpolicy='catchup'\/><\/clock>/" /storage/local/images/$VMDOMAIN.xml
sed -i "/<source bridge='vmbr0'\/>/a\\      <model type='e1000'\/>" /storage/local/images/$VMDOMAIN.xml

# Import Guest Definition changes
virsh define /storage/local/images/$VMDOMAIN.xml

# Launch Guest VM
virsh start $VMDOMAIN

Manage Guest resources

Displaying Guest info

virsh dominfo WinServ1

Id:             20
Name:           WinServ1
UUID:           b25da21c-bdf6-12b1-466f-fb747c850bc2
OS Type:        hvm
State:          running
CPU(s):         2
CPU time:       195.4s
Max memory:     2097152 kB
Used memory:    2097152 kB
Persistent:     yes
Autostart:      disable
Managed save:   no

Enlarging Windows Guest disk

# Shut down Guest VM
virsh shutdown WinServ1
# Resize guest image 
qemu-img resize /storage/local/images/winserv1.qcow2 +5GB
# Boot up guest
virsh start WinServ1
  • Inside Windows launch 'Computer Management'->'Disk Management'
  • Right-click on root volume and choose 'Extend Volume' from pop-up menu

Hot-Add vCPU

# Shutdown Guest for maxvcpu setting
virsh shutdown WinServ1

# Set vCPU limits in config file - applies after next reboot
virsh setvcpus WinServ1 --maximum 4 --config

# Boot-up the domain again
virsh start WinServ1

# Verify new vCPU limits
virsh vcpucount WinServ1

maximum      config         4
maximum      live           2
current      config         4
current      live           2

# vCPU hot-add (not persistent)
virsh setvcpus WinServ1 4 --live

Memory ballooning

Memory management

You can dynamically change the memory in a VM up to what its maximum memory setting is. Note that by default the maximum memory setting in a VM will always equal the amount of memory you specified when you created the VM with the ram parameter in virt-install.

So for example if you created a VM with 1 GB of memory, you can dynamically reduce this amount without having to shut down the VM. If you want to increase the memory above 1 GB, you will have to first increase its maximum memory setting which requires shutting down the VM first.

# Set current memory in kilobytes (1G example)
virsh setmem domain 1048576

# Set maximum available memory in kilobytes (2G example)
virsh setmaxmem domain 2097152 

Guest Autostart

# Enable
virsh autostart WinServ1

# Disable
virsh autostart --disable WinServ1

Attach/Detach/Change CDROM/ISO

# NB! attach-disk wont work if disk xml definition has been removed from domain xml
virsh attach-disk WinServ1 /storage/local/iso/filename.iso hdc --type cdrom --mode readonly
# Change ISO
virsh attach-disk WinServ1 /storage/local/iso/another.iso hdc --type cdrom --mode readonly
# Detach ISO
virsh attach-disk WinServ1 '' hdc --mode readonly --type cdrom

# CDROM Device ADD
# We need inactive guest to re-add cdrom xml part - other methods wont work currently
virsh dumpxml WinServ1 > /storage/local/images/winserv1.xml
virsh shutdown WinServ1
# Add CDROM xml description part
<disk type='file' device='cdrom'>
  <driver name='qemu' type='raw'/>
  <source file='/storage/local/iso/virtio-win-0.1-52.iso'/>
  <target dev='hdc' bus='ide'/>
  <readonly/>
</disk>
# Refresh Guest Definition
virsh define /storage/local/images/winserv1.xml 
# Boot-up guest with cdrom attached
virsh start WinServ1

# Remove CDROM device (doesn't work on active domain for some unknown reason)
virsh shutdown WinServ1
virsh detach-disk WinServ1 hdc --persistent

Retrive block devices info from guest

# Get devices info
virsh qemu-monitor-command WinServ1 '{ "execute": "query-block"}'

{"return":[{"io-status":"ok","device":"drive-virtio-disk0","locked":false,"removable":false,"inserted":{"ro":false,"drv":"qcow2","encrypted":false,"file":"/storage/local/images/winserv1.qcow2"},"type":"unknown"},{"io-status":"ok","device":"drive-ide0-1-0","locked":false,"removable":true,"inserted":{"ro":true,"drv":"raw","encrypted":false,"file":"/storage/local/iso/virtio-win-0.1-52.iso"},"tray-open":false,"type":"unknown"}],"id":"libvirt-55"}

Create Guest template

# Shutdown Guest for template creation
virsh shutdown WinServ1
# If shutdown wont work then force
virsh destroy WinServ1

Troubleshooting

Guest shutdown wont work

Guest shutdown usually fails if users are still logged in. You have 2 choices: 1. Ensure that all users are logged out and better shutdown Windows inside the Guest 2. Execute 'yum destroy domain' - in order to force VM shutdown

References