GuestbookSign our guestbook ContactGet in touch with the authors ArchiveAll unixwerk articles since 2003
January 15, 2011

How to Setup KVM for Windows Guests

Contents

  1. Introduction
  2. Required Packages
  3. Kernel Requirements
  4. Create an Empty Image to Use for Windows Installation
  5. Installation of the Windows Guest
  6. Start Virtual Machine
  7. Attach a USB Device to a Windows VM
  8. Snapshots
  9. Setup Network Bridging
  1. Related Information
  2. Links

1. Introduction

This is a brief description of how to get Windows XP running under KVM control with Slackware64 13.1. Note that you need an x86 compatible CPU with enhanced virtualization support (AMD-V or Intel VT).

2. Required Packages

You need to download two packages (for Slackware64 13.1) in order to get qemu-kvm running:

The packages can be installed with:

root@darkstar# installpkg vde-2.3.1-x86_64-1alien.tgz qemu-kvm-0.12.5-x86_64-1alien.tgz

3. Kernel Requirements

Slackware's default kernel is compiled with KVM support. However, the kernel modules for KVM are not loaded by default. You have to load them manually. If your computer is running on an AMD CPU load

root@darkstar# modprobe kvm_amd

On computers running on an Intel CPU load

root@darkstar# modprobe kvm_intel

instead. You can uncomment the appropriate line in /etc/rc.d/rc.modules if you wish to have KVM support turned on by default.

4. Create an Empty Image to Use for Windows Installation

We want to create a 12 GB RAW image file for the Windows VM:

root@darkstar# qemu-img create -f raw /kvmimages/winxp.img 12G

5. Installation of the Windows Guest

In order to be able to install Windows XP as a KVM guest you need an official installation image either as CD/DVD or as an image file on disk. I used the latter variant.

The installation can be started with a command like this:

root@darkstar# qemu-system-x86_64 -cdrom /images/winxp.iso -m 768 -boot d /kvmimages/winxp.img

In the above command /images/winxp.iso is the Windows XP installation CD image and /kvmimages/winxp.img is the RAW image we created with step 4. With -m 768 we assign 768MB of memory to the VM.

Now we see the Windows CD booting into the Windows XP installer.

6. Start Virtual Machine

After the installation completed we are ready to start the Windows Guest. I still kept the installation CD assigned and started the guest with the below command:

root@darkstar# qemu-system-x86_64 -localtime -cdrom /images/winxp.iso -m 768 /kvmimages/winxp.img

The option '-localtime' can be used to set the Linux time and date as local time for the Windows guest.

7. Attach a USB Device to a Windows VM

If your system automatically mounts removable devices, unmount it:

root@darkstar# df | grep media
/dev/sdg1              1000508    498908    501600  50% /media/TOSHIBA
root@darkstar# umount /media/TOSHIBA

If you see more than one device and you're not sure which one you just attached check with 'dmesg'.

Then find the right vendor:device id with lsusb

root@darkstar# lsusb
Bus 007 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 002: ID 046d:c050 Logitech, Inc. RX 250 Optical Mouse
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 002 Device 004: ID 0930:652a Toshiba Corp. TravelDrive       <== this might be our USB stick
Bus 002 Device 002: ID 1307:0330 Transcend Information, Inc. 
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Attach the device along with the start of the VM:

root@darkstar# qemu-system-x86_64 -usb -localtime -usbdevice host:0930:652a -m 768 /kvmimages/winxp.img

8. Snapshots

Creating snapshots is as easy as copying a file:

root@darkstar# cp /kvmimages/winxp.img /kvmimages/.snapshots/winxp.$(date +%Y-%m-%d)

Be sure you stopped Windows before creating the snapshot. I suggest to create the snapshots on a separate filesystem on a different disk. My setup is:

root@darkstar# df | grep kvmimages
/dev/sda5             51612920   3987204  47625716   8% /kvmimages
/dev/sdb5             51613016   3481260  48131756   7% /kvmimages/.snapshots

9. Setup Network Bridging

Setting up a network bridge under Linux is easy. It boils down to these four steps¹:

1. Remove all ip addresses from your physical nic (eth0):

root@darkstar# ip addr delete 192.168.100.10/24 dev eth0
root@darkstar# ip addr delete 192.168.100.110/24 dev eth0
root@darkstar# ip addr delete 192.168.100.210/24 dev eth0

2. Create a bridge device (br0):

root@darkstar# brctl addbr br0

3. Connect the bridge to eth0:

root@darkstar# brctl  addif br0 eth0

4. Assign all ip addresses to br0:

root@darkstar# ip addr add 192.168.100.10/24 brd + dev br0
root@darkstar# ip addr add 192.168.100.110/24 brd + dev br0 label br0:0
root@darkstar# ip addr add 192.168.100.210/24 brd + dev br0 label br0:1
root@darkstar# route add default gw 192.168.100.1 metric 1

Our bridge is ready for use now. But there are few things more to prepare before we can start our Windows guest. First we need a network start script for KVM:

root@darkstar# vi /etc/qemu-ifup
#!/bin/sh
switch=br0

if [ -n "$1" ];then
        /sbin/ip link set $1 up
        sleep 0.5s
        /usr/sbin/brctl addif $switch $1
        exit 0
else
        echo "Error: no interface specified"
        exit 1
fi
root@darkstar# chmod +x /etc/qemu-ifup

We need the kernel module tun for the network virtualization to work:

root@darkstar# modprobe tun

Finally we are ready to start our VM:

root@darkstar# qemu-system-x86_64 -localtime -m 768 /kvmimages/winxp.img -net nic,macaddr=DE:AD:BE:EF:61:C3 -net tap

The last 2 digits of the mac address should be randomly generated to support more than one VM.


¹ Of course, all ip addresses shown here are only examples. Replace them with the proper ones from you setup.

A. Related Information

  1. KVM: Setting Guest Network
  2. A Networking Powerhouse

B. Links

  1. http://connie.slackware.com/~alien/slackbuilds/qemu-kvm/pkg64/13.1
  2. http://connie.slackware.com/~alien/slackbuilds/vde/pkg64/13.1