在Ubuntu下面需要安装下面的软件:

sudo apt-get install kvm qemu-kvm

加入个人用户到kvm组,以保证个人用户可以读写/dev/kvm,否则,以个人用户无法运行kvm:

sudo adduser $USER kvm

如果想使用物理磁盘,则需要加个人用户到disk组:

sudo adduser $USER disk

现在最好重启一下计算机!

下面开始安装操作系统,我安装了Windows XP和Windows 7,下面以Windows XP为例开始安装。

先准备好一个磁盘文件,有2种常用的格式,一个是qemu的qcow2格式,一个是raw格式,前者是可以增长的文件格式,后者实际是磁盘上一个连续区域。

qcow2:

qemu-img create -f qcow2 winxp.img 10G

Raw:

dd if=/dev/zero of=winxp.img bs=1024K count=10000

下面开始安装,准备好安装光盘文件winxp.iso,下面是安装的指令:

kvm -m 1024 -cdrom=winxp.iso -hda winxp.img -boot d -localtime

XP安装很快,20分钟左右就可以安装好,Win 7大约需要40分钟。下面开始以下后面的工作,会涉及更多的参数。

配置硬盘和网络的virtio模式,virtio是个半虚拟化的模式,速度比传统方式还要快些,根据Using virtio_net For The Guest NIC一文的测试,virtio还是很有优势的。

在虚拟机里面下载virtio windows guest驱动: Storage driver和Network driver,其中已经包含了xp、win7等的驱动。

根据boot from virtio block device一文的介绍,用以下方式进行安装驱动。

建一个临时的磁盘文件:

qemu-img create -f qcow2 fake.img 1G

启动虚拟机:

kvm -drive file=winxp.img,if=ide,boot=on -drive file=fake.img,if=virtio -m 1024 -localtime -net nic,model=virtio -net user -usb -usbdevice tablet

在启动的虚拟机里,会找到virtio设备,然后安装这两个驱动。安装完毕后,关机。以下面方式重新启动:

kvm -drive file=winxp.img,if=virtio,boot=on -m 1024 -net nic,model=virtio -net user -usb -usbdevice tablet -soundhw es1370

下面配置远程桌面对Windows的访问,首先需要在虚拟机的Windows里面开启远程桌面,而且必须给用户加上密码,Windows XP和Windows 7需要是Professional以上版本。然后运行mmc继续配置远程桌面访问参数。

打开mmc后,“添加/删除管理单元“ -> “添加” -> “组策略对象编辑器” -> “本地计算机”。在里面配置:“计算机配置” -> “管理模板” -> “Windows 组件” -> “终端服务” -> “限制最大颜色深度”,设置为24位,原来默认是16位。

关机,重新以可以以新的模式后台运行:

kvm -M pc -drive file=winxp.img,if=virtio,boot=on -localtime -m 1024 -soundhw es1370 -net nic,model=virtio -net user -vnc 127.0.0.1:0 -daemonize -redir tcp:3389::3389 -name WindowsXP -usb -usbdevice tablet

-vnc 127.0.0.1:0表示以vnc模式运行,-daemonize表示kvm以后台运行,-redir tcp:3389::3389表示将虚拟机的3389端口映射为宿主机的3389端口上,这样rdesktop本机就可以进行远程桌面访问了。如果想实时 查看状态,可以使用vncviewer或者vinagre查看。

现在可以rdesktop进行访问了,下面是rdesktop的参数:

rdesktop localhost -u username -p password -f -D -z -P -r sound:local -clipboard

这里-z参数可以保证鼠标运行速度不缓迟,-f表示全屏。

虚拟机如何使用USB设备呢,首先要保证用户可以访问USB设备,qemu支持usb设备在虚拟机里面的拔插,在qemu界面里(或者vnc访问的界面里),Ctrl+Alt+2进入qemu monitor命令行控制模式,然后

usb_add host:vid:pid

查看usb设备:

info usb

Windows 7的运行和访问参数与Windows XP类似。

http://www.linux-kvm.org/page/Boot_from_virtio_block_device
Introduction

Virtio block device is a para-virtualized device for kvm guest. It is different from normal emulated hard drive, because it is simply faster. This small how-to is about how to make a disk para-virtualized and boot from it.

Linux

Steps

  1. create a disk and install os to it by appending "-hda <your_disk_p_w_picpath>" to your virtual machine
  2. in your guest os, upgrade kernel to 2.6.25 which contains virtio_* drivers. or ubuntu 8.04 also has it.
  3. in guest os, change /boot/grub/device.map from "(hd0) /dev/sda" to "(hd0) /dev/vda"
  4. in guest os, change /boot/grub/menu.list from "root=/dev/sda1" to "root=/dev/vda1", if you are using UUID, then no need to do this step.
  5. enable para-virtualization by changing "-hda <your_disk_p_w_picpath" to "-drive file=<your_disk_p_w_picpath>,if=virtio,boot=on"

note: current debian lenny default kernel (2.6.26-2-amd64) will not give any output when running "dmesg | grep virtio"

Error 18

you may experience this error while grub is booting. I solved it by making the /boot the first partition and make it 32MB.

qcow2

I have problem to boot from qcow2 format p_w_picpath, it will hang while kernel booting. you may convert your disk p_w_picpath to raw format by "qemu-img convert -f qcow2 <your_disk_p_w_picpath> -O raw <a_new_location>".

Booting with lilo

You must add this to your lilo.conf:

boot=/dev/vda disk=/dev/vda bios=0x80 max-partitions=7 

and after your p_w_picpath clausule change:

root=/dev/vda1 

where vda1 is your boot partition

Updating the guest initramfs with the virtio driver

If you are hosting a debian guest then you may want to update its initramfs with the virtio driver as explained in Using VirtIO NIC

Windows XP

Windows driver installation is a bit tricky.

  • In your guest, download the Windows driver
  • Stop the machine and create a new p_w_picpath that you will add as a second disk
kvm-img create -f qcow2 fake.qcow2 1G 
  • Start the machine. You will be prompted to install the driver. This is enough to allow to boot on the virtio disk
kvm -drive file=windows.qcow2,if=ide,boot=on -drive file=fake.qcow2,if=virtio ... 
  • Restart the machine, but with the virtio driver on the first disk
kvm -drive file=windows.qcow2,if=virtio,boot=on 
  • You will be prompted once again to install the driver.