QEMU Windows Guest: Installing the Operating System
Now that we've created the image for our guest, we must continue with installing the operating system on it. In Virtualbox/VMWare, we usually select the CD-ROM to boot from the iso installation image and start the virtual machine, after which the installation is automatically started by booting from the CD-ROM. It's essentially the same in QEMU, except that we must do that from the command line. We have already provided enough information that we now know that we must use the QEMU-kvm executable.
First we must look at the command-line options that we must be aware of before trying to do that. The most basic command line options are presented below (summarized after [2]).
What should you learn next?
- if: which interface type the drive is connected: none, ide, scsi, sd, mtd, floppy, pflash, or virtio.
- bus: define drive bus number.
- unit: define drive unit id.
- media: the type of media: disk, CD-ROM.
- snapshot: allow snapshots of drive.
- cache: how the host cache is used to access block data: none, writeback, unsafe, writethrough.
- format: specifies the disk format to be used.- serial: the serial number to assign to the device.
- b: floppy 2
- c: first hard disk
- d: first CD-ROM
- n-p: network adapter
Now we have a basic understanding of the QEMU-kvm command-line options, which brings us one step closer to the actual operating system install. In the next step we'll write a Bash script that we'll use to install the Windows7 operating system on the previously created image. The complete bash script can be seen below:
[python]
#!/bin/bash
/usr/bin/QEMU-kvm
-machine accel=kvm
-cpu core2duo
-name Windows7
-drive file=Windows7.img,if=ide,cache=writeback
-m 1G
-cdrom Windows7.iso
-boot order=c
-enable-kvm
-monitor stdio
-smp 1,sockets=1,cores=1,threads=1
-nodefaults
-usbdevice tablet
-rtc base=localtime
-vnc 127.0.0.1:1
-vga qxl
-full-screen
-device hda-duplex,id=sound0-codec0,bus=sound0.0,cad=0
-device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x6
-device e1000,netdev=net0
-netdev user,id=net0
We still need to mention a couple of tricks I picked up when started playing with QEMU. First, the mouse cursors are not properly aligned. If you happen to install a Windows operating system and there are actually two cursors on the screen, the host cursor and the guest cursor, you have a problem, because the cursors are not on top of each other as they should be. To resolve this issue, we can add the following command-line parameter to the QEMU-kvm command, which resolves the problem:
[c]
-usbdevice tablet
Another problem is a Windows operating system not recognizing the hard drive, which results in something like the screen below. Notice that Windows didn't recognize the hard drive. This happens because of the "if=virtio" parameter passed to the -drive option specifying the image to use.
This happens because Windows does not have support for a virtio driver and therefore it won't detect the hard drive. If we absolutely must use the virtio driver, then we can use a tip from [3], but I haven't tested it, so I don't know whether it works or not. Rather than that, we can also specify the "if=ide" interface and be done with it; Windows supports that driver and will detect the hard drive without a problem as can be seen on the picture below.
After that we can normally install Windows 7 as a QEMU guest. The same script can also be used to start Windows 7 after the installation; the only thing we need to change is the -boot option, which must be set to boot from hard drive rather than CD-ROM. Now we have Windows 7 guest installed and working and we can start by configuring other important options that we just have to have to make the VM useful.
But we should make the effort to use the virtio driver, since it provides better performance. To do that we need to add the "if=virtio" option to the -drive parameter and we must also add the "-boot order=c" to the command line, because otherwise we won't be able to boot from it.
Conclusion
In this article we've seen how we can go about installing the Windows 7 operating system on the QEMU image and taken a look into most useful command-line parameters that we need to become familiar with to work with QEMU. If you're just starting to use QEMU, you should definitely know about the command-line parameters presented in the previous table. This can also serve as a great reference when we don't remember certain command-line parameter and we don't want to read through the QEMU main page.
References:
[1] QEMU-img man page, http://linux.die.net/man/1/QEMU-img.
[2] QEMU-kvm man page, http://linux.die.net/man/1/QEMU-kvm.
[3]Updating the guest initramfs with the virtio driver, http://www.linux-kvm.org/page/Boot_from_virtio_block_device#Windows_XP.
[4] Ebtables, http://ebtables.sourceforge.net/.
[5] Saying How To Mangle The Packets, http://www.netfilter.org/documentation/HOWTO/NAT-HOWTO-6.html.