Saturday, April 16, 2011

How to Shutdown Windows Guest Gracefully in KVM

KVM uses ACPI to send shutdown event to the guest virtual machine. But it can't do that in case your windows settings prohibit shutdown when there is no user logged in, you have to change this settings. Here is how:
  1. Ensure the ACPI is enabled in your virtual machine settings.
  2. Login to your Windows guest and launch Group Policy Object Editor (gpedit.msc).
  3. Locate the following key and change the settings to enabled.
    Computer Configuration\Windows Settings\
    Security Settings\Local Policies\Security Options\
    Shutdown: Allow system to be shut down 
    without having to log on
    
  4. If you want to be able shutdown guest even there is a logged in user add the following to file ShutdownWarningDialogTimeout.reg and enter it into windows registry.
    Windows Registry Editor Version 5.00
    
    [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows]
    "ShutdownWarningDialogTimeout"=dword:00000001
    
Finally here is a simple command using virsh to shutdown guest:
virsh -c qemu:///system shutdown w2k3
Once the above settings is enabled you should be able gracefully shutdown your windows guests using virtual machine ACPI power management.

Thursday, April 14, 2011

Troubleshooting: Windows Guest on KVM

While migrating windows virtual machine from virtualbox to kvm you might experience an issue related to significant virtual hardware changes, in other words you get BSoD (bluescreen). Here are few findings that can help you:
  1. Windows 2003, BSoD. STOP: 0x0000007B. Here is a description. You need apply this before you convert image to kvm.
  2. Error message that computer does not have a parallel port: "The Parallel port driver service failed to start". Read more here.
  3. If your Windows host was configured in KVM configuration first choose IDE drive as disk bus, add SCSI controller. When guest starts let it scan for hardware changes, that might take few minutes, when it finds SCSI controller shutdown guest and change disk bus to use SCSI controller, you can remove IDE controller.
  4. Uninstall virtualbox guest tools, so windows doesn't complain (see system event log).
The above "fixes" should let you start your windows guest in kvm.

How to Convert VirtualBox VDI image to KVM

The idea is very simple. We convert virtualbox vdi image to raw format and than from raw format to kvm specific. I have chosen qcow2 format based on this.
  1. Convert img.vdi to raw format. Please note, that raw format means if your vdi image is configured for maximum size of 10 Gb than the resulting raw file will be 10 Gb. Make sure you have enough disk space.
    VBoxManage clonehd --format RAW img.vdi img.raw
    
  2. And now raw image to qcow2 format. Here the resulting file (img.qcow2) will be the same size as the original one (img.vdi).
    qemu-img convert -f raw -O qcow2 img.raw img.qcow2
    
  3. Delete raw file since you will not need it anymore.
You can attach resulting img.qcow2 file to the kvm virtual machine now.

Debian KVM

Kernel-based Virtual Machine (KVM) is a virtual machine implementation using the operating system's kernel (read more here). Here are few steps to install kvm in debian:

Server

  1. Setup SSH. Read more here.
  2. Setup bridge-utils package...
    apt-get install bridge-utils
    
    ... and configure network interface (restart computer so network changes take place):
    auto eth0
    iface eth0 inet manual
    
    auto br0
    iface br0 inet static
         address 192.168.10.11
         netmask 255.255.255.0
         network 192.168.10.0
         broadcast 192.168.10.255
         gateway 192.168.10.1
         bridge_ports eth0
         bridge_stp off
         # 1.
         bridge_fd 0
         bridge_maxwait 0
         # 2.
         #bridge_fd 9
         #bridge_hello 2
         #bridge_maxage 12
    
  3. Install qemu-kvm and libvirt-bin packages:
    apt-get -y install qemu-kvm libvirt-bin
    
  4. Add a user that will be managing kvm to group libvirt (e.g. user1):
    adduser user1 libvirt
    

Client

  1. Setup Password-less ssh login to kvm server. Read more here.
  2. Install virt-manager package:
    apt-get -y install virt-manager
    
  3. If your client is not going to host kvm virtual machines you can disable the following daemons:
    update-rc.d ebtables disable
    update-rc.d libvirt-bin disable
    update-rc.d libvirt-guests disable
    update-rc.d lvm2 disable
    
  4. Open Virtual Machine Manager from Applications > System Tools.
  5. In File menu select Add Connection. In dialog that appears ensure method ssh and user that you added on server to group libvirt).

Performance Tuning

  1. The KVM host can take benefit of KSM by finding and sharing memory blocks between vitual machines (add the following to /etc/rc.local).
    echo 100 > /sys/kernel/mm/ksm/sleep_millisecs
    echo 1 > /sys/kernel/mm/ksm/run
    
    You can take a look at pages sharing / shared:
    cat /sys/kernel/mm/ksm/pages_sharing
    cat /sys/kernel/mm/ksm/pages_shared
    
    Another useful thing is to use vhost-net kernel module to boost virtual machine network performance (ensure guest vm uses virtio network device).
    echo vhost-net >> /etc/modules
    
  2. The KVM linux guest IO performance can be improved by:
    • using virtio as disk bus
    • setting virtual disk performance options to: cache mode - none, IO mode - native
    • using noop IO scheduler for each guest (file /etc/default/grub):
    GRUB_CMDLINE_LINUX_DEFAULT="quiet elevator=noop"
    
    Update grub by issuing update-grub command.