Windows7 on KVM-QEMU with QCOW2 Snapshots

During the past few weeks I found myself doing lots of work with Windows VMs. I finally started reading-up on the use of snapshots and took the time to figure out how to install the virtual storage drivers on creation of a new Windows VM.

This article is divided into two parts. In part one we will:

  • Discuss the steps involved in building a new Windows 7 VM,
  • Use a QCOW2 format image so that we can save disk space and make use of snapshots with our VM,
  • Get the Redhat Virtual I/O drivers installed during the Windows Installation process so that we can take advantage of the performance improvements that they offer,
  • Install the Redhat Spice Video and other Guest Tools so that, among other things, our Desktop Clipboard will interface properly with our VM clipboard,
  • And we'll perform some important additional customizations to improve the performance and reliability of our Windows 7 VM.

To reduce the size of this article I put all the information about QEMU snapshots into Part Two, which you can find here:

KVM-QEMU, QCOW2, QEMU-IMG and Snapshots

In part Two we will:

  • Take snapshots of the VM at various stages of development so that we can go back, for example, to Internet Explorer 9, 10 or 11 - or at any point where we saved a snapshot of the VM image.
  • Make a second VM, in only a few seconds, which will be based on the first VM.
  • Merge the the second VM image file with the Backing Image file to create a single file that doesn't waste disk space on unused snapshots.

The steps described here are very specific to KVM-QEMU on Linux. The snapshot functions are not yet available in the Virt-Manager GUI so we will execute some commands in a shell. Although I've been using Windows 7 lately I'm guessing that other versions of Windows will likely be similar.

I did try to include some conceptual notes along with the instructions. As always I look forward to hearing from you. If you have any questions or comments please use the contact form that you can find in the About menu on this site.

Let's get started.

New features in KVM-QEMU, VirtIO & Virt-Manager

KVM has been working fine for years so I never really needed to look at it. Consequently I had no idea, until recently, about the great work the QEMU people have been doing in the area of snapshots. It turns out that QEMU has supported, for some time now, snapshots and Backing Images through the QEMU-IMG tool.

Some other improvements include:

  • Previously missing features such as USB support. You can now connect your VM to your USB devices and the glue is getting better every year.
  • Redhat now distributes a Spice Video support package for Windows VMs. Not only does it install support for your Spice Video - you also get clipboard support to allow cutting and pasting between your VM and your desktop.

There are probably many more features hiding under the hood. They may not be obvious but we can see the VMs running smoothly.

Downloading Drivers

Before you begin you will need to collect some Redhat Virtual I/O and Spice Video packages:

There are plenty of other packages you might need for your VMs such as 7-zip, git-bash, bacula, etc. No doubt you've already collected many of them somewhere convenient. Of course you will also need your Windows installation DVD and your product key.

Creating a new VM

At the time of this writing there is a minor bug in Virt-Manager on Ubuntu 13.10. If you follow the normal procedure for creating a new VM, customizing it as we need here, the launch of the new VM fails with an error message about the KVM-QEMU binary not being found. Hopefully, by the time you read this, the bug will be fixed.

As it happens, though, the work-around for the bug is simple and easy: We will create a simple default VM, stop the installation just after it begins, then customize the VM settings before continuing with a normal installation.

Here are the steps:

The first step is to click the New VM button, under the File menu, at the top-left corner of the Virt-Manager window

Browse to your installation DVD and select your OS Type/Version. Click Forward to continue.

(Throughout this document you will want to scroll down the page to get from one image to the next. Note that the images should be lightbox enabled. You should be able to click on them to pop them out at full size if you need to see some detail.)

Next: Virt-Manager wants to know how much RAM and how many CPU cores to allocate to the VM. In practice Windows 7 will only use the first two cores. The amount of RAM that you allocate will depend on your plans for this VM - but and how many CPUs to use

Here Virt-Manager needs to know about the disk image we will be using for our new VM.

Once again: At the time of this writing there is a bug in Virt-Manager that will appear in the next few steps - so let's not try to setup the real VM image for now. Instead we can create a dummy QCOW2 image file as a temporary placeholder.

There's no need to allocate any space for this file as we will overwrite it in a minute or two. It's more convenient, though, if it has a meaningful name. (I used /kvm/win7demo-kvm.qcow2 for this article.)

Go ahead and click the Finish button in the bottom-right corner so that Virt-Manager will launch the new VM.

We're just launching a default VM for now. Then we'll Force Off the VM and customize it.

The Windows Installation procedure will start loading files. As we don't plan to complete this installation it doesn't really matter; we can go ahead and force-off the VM.

From the Virtual Machine menu, open the Shut Down menu and select the Force Off menu option.

Once the VM has been forced off Virt-Manager will automatically reboot it again. This is normally a convenience feature. Virt-Manager is assuming that the installation process has completed and you need to reboot your VM. You will have to force it off a second time.

The QCOW2 image file

At this point we need to create our QCOW2 image file. Open a shell and change to the directory you used above when you created your dummy VM image file. (You remember I used /kvm/win7demo-kvm.qcow2 for this article.)

Check part two of this article for detailed information about the QEMU Image Create command.

Here is the command that I used to create my QCOW2 image and the output from that command.

$ qemu-img create -f qcow2 win7demo-kvm.qcow2 256G
Formatting 'win7demo-kvm.qcow2', fmt=qcow2 size=274877906944 encryption=off cluster_size=65536 lazy_refcounts=off 

Use the sudo command if you are creating an image in the standard virt-manager images folder as you will not have permission to write files in there.

Customizing the Virtual Hardware

The default configuration of a new VM expects to be using a RAW image file and has one CDROM drive for the Operating System installation CD.

What we want to do now is customize the virtual hardware in a few small ways:

  • Set the main storage device type (called Disk Bus in Virt Manager,) to VirtIO,
  • Tell Virt Manager that we want to use the QCOW2 format instead of RAW,
  • Add a second CDROM drive to hold the Redhat Virtual IO driver files,
  • and Set the Device Model for the network card to VirtIO.

In practice I find the virsh command very useful for this kind of work. I use virsh dumpxml to extract an xml file if I need to create lots of VMs quickly. Then I edit the xml files and use virsh definexml to load them back into the Virt-Manager. In this case we only need to create one VM so let's do this the graphical way:

To start this customization: Open the Virtual Machine Details for the Win7Demo VM and Click on the IDE Disk 1 device in the left panel.

In the Advanced Options for this device you will want to select VirtIO for the Disk Bus and QCOW2 for the Storage Format.

Under performance options you can select a Cache Mode. Unfortunately I haven't found any good documentation on these various modes. Based on notes from one writer it seems that the Default option is supposed to be the safest.

I did find one document that suggested the Default mode is the slowest and WriteBack was faster for the person who wrote that document. During my own tinkering I had no problems with the WriteBack mode. I did not try to test the performance of the various modes.

Use Default mode if in doubt or if you experience any unusual behavior with your VM with other cache modes.

Next you will want to create a second CDROM drive. This drive will be used to load the Redhat Virtual IO drivers after the Windows 7 installer boots.

Browse to the ISO file containing the Redhat Virtual IO drivers.

Don't forget to set the device type to IDE CDROM.

You should now have two CDROM drives in the device list for your VM. The first CDROM drive should be connected to your Windows Installation DVD.

You might also want to check your Boot Options. You want to make sure that your VM is willing to boot from the first CDROM drive. Personally I also like to make sure the Boot Menu is enabled.

When preparing this documentation I forgot to change the Ethernet Device Model to VirtIO. It's more convenient to do it right awaay.

On the other hand not much happens if you forget to change it. The Virtual RTL8139 device will work nicely. You can switch the device to VirtIO later to take advantage of the improved performance.

Installing Windows

At this point everything is ready. Click on the Run button to boot your new VM.

If your toolbar is not visible you need to open the Virtual Machine menu and select the Run menu option.

If all goes well you will see the Windows Installer loading files and asking you to agree to the license terms. About four screens into the process you will be asked if you want to perform an Upgrade or a Custom Install.

Choose Custom Install to begin a new installation.

Loading Redhat Drivers

Here's where we get to load the Redhat Virtual IO drivers.

Windows is not able to see the QCOW2-based virtual storage device because it doesn't have a driver for the VirtIO device (yet.)

Click on the Load Driver link near the left side of the window.

Now click on the browse button...

Select the CDROM drive in which you loaded your Redhat Virtual IO drivers.

And find those files.

In this case I am using Windows 7, 64 bits, so I choose the virtio/WIN7/AMD64 folder.

The Windows Installer will scan the folder and show only the drivers that match the virtual hardware it sees in the VM. Therefore, as a general rule, you will want to select all the drivers that appear in the list and click on the Next button.

The Windows installer will then take a bit of time to load all the drivers and activate them. After that it will search for a usable storage device.

Completing the Installation

Select the device and click on the Next button to begin the installation procedure.

Depending on your system this might take a while.

After rebooting a couple of times, the Windows installer will ask you for some information.

A few screens later you will be able to log into your new VM for the first time.

Spice Guest Tools

Once you log into your new VM you may want to install the Spice Guest Tools package. This will not only provide you with advanced Spice Video drivers - it will also implement the Clipboard interface between your VM and your Desktop.

After working for a bit the Spice Guest Tools package will try to install a driver on your system. Windows will ask you for permission to complete this operation.

The package will try to install a few more drivers before it is finished.

Once the Spice Guest Tools are installed the basic installation is complete.

To snapshot or not to snapshot?

At this point you have a basic VM that is working. If you can think of a case where this might be of value to you in the future you might want to "take a picture of it" so that you can take your image back to this state in the future.

If you decide that you do want a snapshot at this point you can shutdown the VM and take one (the instructions are here: QEMU Image Snapshot command.)

Windows Update

The next step is to get started with Windows Update.

In my case I wanted a snapshot of a clean install of Windows 7 with Internet Explorer 9. To accomplish this I had to install all security and performance updates without accidentally installing any Optional updates representing new functionality.

As you know this takes a long time. Each time you run Windows Update it checks all the known issues with (and between) files in the system. Each time you install updates there is a chance that a newly installed file will not be compatible with a previously installed file. At some point it may turn out that you have, for example, two DLLs in the system that are known by Microsoft to conflict with each other. Windows Update will detect such issues and install any fixes that have been created to resolve them. As such you need to run Windows Update, reboot your system and Check for Updates, over and over again, until it tells you that there are no more updates to install.

I ran Windows update a few times before finally being offered only SP1, IE10, IE11 and some other enhancements (and I took snapshots at each milestone.)

Important VM Customizations

While Windows Update is running it's a good idea to take care of some basic customizations that will improve the performance of the VM.

Although Windows 7 was written in the age of virtualization it does not automatically configure itself to act as a VM. In effect it continues to behave as though it is in control of your bare metal. It tries to manage the internal system fans to control the temperature. It also tries to power-down the hard disk and turn off the screen when it is idle. Most often these operations represent an insignificant overhead but, in some cases, they can cause the VM to crash. In all cases these functions have no value in the context of the life of a VM so it's best to disable them.

Disabling power management

Start by running the Power Options control panel applet.

Look under the Additional Power Plans. Select the High Performance power plan and click on the Change Plan Settings link for that plan.

Choose the Never option for turning off the display and for putting the computer to sleep; don't forget to click on the Save Changes button.

Click on the link called Change Advanced Power Settings.

Verify that the Turn Off Hard Disk After setting is set to Never (ie: zero minutes.)

Disable the USB suspend setting.

That was all I found when I was looking but, in the future, there may be other power-saving settings. Take a look around and disable them.

Going Further

This concludes the Windows VM setup and Part One of this article.

In Part Two we will:

  • Take snapshots of the VM at various stages of development so that we can go back, for example, to Internet Explorer 9, 10 or 11 - or at any point where we saved a snapshot of the VM image.
  • Make a second VM, in only a few seconds, which will be based on the first VM.
  • Merge the the second VM image file with the Backing Image VM image file to create a single file that doesn't waste disk space on unused snapshots.

Part Two continues here:

KVM-QEMU, QCOW2, QEMU-IMG and Snapshots