主要为了看看图像显示是否有问题,跑起来系能如何,网络连接、文件共享是怎样的。

用的是雨林木风xp sp3的iso。为了提高性能,决定使用qcow2格式,预分配metadata,cache=none,本来还想用vritio结果发现加上之后安装时检测不出硬盘。

kvm-img create -f qcow2 -o size=20G,preallocation=metadata xp.img
kvm -localtime -no-acpi  -localtime -m 512 -cdrom XP_SP3_YS5.6.iso -drive file=xp.img,index=0,media=disk,cache=none -boot d

从安装过程来看,cache=none和默认的writethrough相比的确快了不少,复制文件部分writethrough花了大约10分钟,none只花了5分钟(-m 1024会减少到3分钟)。

安装好了,可以上网、可以浏览网页,看看视频怎么样,结果发现视频可以放,但是没有声音,xp托盘上甚至没有soundman。搜了一下,发现需要加上hw参数。

列出所以可用声卡:

kvm -localtime -m 1024 -drive file=xp.img,index=0,media=disk,cache=none -soundhw ?
Unknown sound card name `b'
 Valid sound card names (comma separated):
 pcspk       PC speaker
 sb16        Creative Sound Blaster 16
 ac97        Intel 82801AA AC97 Audio
 es1370      ENSONIQ AudioPCI ES1370
 hda         Intel HD Audio


 -soundhw all will enable all of the above


使用es1370:

kvm -localtime -m 1024 -drive file=xp.img,index=0,media=disk,cache=none -soundhw es1370

现在有声音了,可以看到视频画面不太流畅,远远不如virtualbox的表现。

看看网络是怎么回事,在xp中输入ipconfig,ip是10.0.2.15,网关是10.0.2.2可ping通,而host机上并没有新增10.0.2.2这个网卡。

原来默认是user mode networking就是这个设置,在10.0.2.2上是一个dhcp服务器,10.0.2.3是一个dns,10.0.2.4是一个samba服务器。user mode networking相当于是命令-net nic -net user。这个模式下网络比较受限,只支持tcp,udp(icmp就不行),并且从host无法访问guest(incoming traffic)。但是可以支持端口转发,以下命令添加共享文件夹:

kvm -localtime -m 1024 -drive file=xp.img,index=0,media=disk,cache=none -soundhw es1370 -redir tcp:5556::445 # 445是windows网络共享cifs协议端口

...

在guest中设置网络共享(在xp中需要去掉资源管理器中的文件->查看->简单文件共享)

....
sudo apt-get install cifs-utils  # 为了使用mount.cifs
sudo mount -t cifs //127.0.0.1/KVM_xpbox /mnt/qemu -o user=Administrator,pass=password,port=5556

进入该目录就可以执行读写了。

以下是虚拟机最最重要的部分:

可以桥接吗?

可以的。按照例子,对/etc/qemu-ifup进行替换:

#! /bin/bash
# 
 # script to bring up the tun device in QEMU in bridged mode 
 # first parameter is name of tap device (e.g. tap0)
 #
 # some constants specific to the local host - change to suit your host
 #
 ETH0IPADDR=172.16.0.100
 GATEWAY=172.16.0.1
 BROADCAST=172.16.0.255
 #
 # First take eth0 down, then bring it up with IP address 0.0.0.0 
 #
 /sbin/ifdown eth0
 /sbin/ifconfig eth0 0.0.0.0 promisc up
 #
 # Bring up the tap device (name specified as first argument, by QEMU)
 #
 /usr/sbin/openvpn --mktun --dev $1 --user `id -un`
 /sbin/ifconfig $1 0.0.0.0 promisc up
 #
 # create the bridge between eth0 and the tap device
 #
 /sbin/brctl addbr br0 
 /sbin/brctl addif br0 eth0
 /sbin/brctl addif br0 $1
 # 
 # only a single bridge so loops are not possible, turn off spanning tree protocol
 #
 /sbin/brctl stp br0 off 
 # 
 # Bring up the bridge with ETH0IPADDR and add the default route 
 #
 /sbin/ifconfig br0 $ETH0IPADDR netmask 255.255.255.0 broadcast $BROADCAST
 /sbin/route add default gw $GATEWAY
 #
 # stop firewall - comment this out if you don't use Firestarter
 #
 #/usr/sbin/service firestarter stop

 

对/etc/qemu-ifdown进行替换:

#!/bin/sh 
 # 
 # Script to bring down and delete bridge br0 when QEMU exits 
 # 
 # Bring down eth0 and br0 
 #
 /sbin/ifdown eth0
 /sbin/ifdown br0 
 /sbin/ifconfig br0 down 
 # 
 # Delete the bridge
 #
 sbin/brctl delbr br0 
 # 
 # bring up eth0 in "normal" mode 
 #
 /sbin/ifconfig eth0 -promisc
 /sbin/ifup eth0 
 #
 # delete the tap device
 #
 /usr/sbin/openvpn --rmtun --dev $1
 #
 # start firewall again
 # 
 #/usr/bin/service firestarter start

要启动第一个虚拟机,执行:

sudo /etc/qemu-ifup tap0
sudo kvm -localtime -m 1024 -drive file=xp.img,index=0,media=disk,cache=none -soundhw es1370 -net nic -net tap,ifname=tap0,script=no,downscript=no

第二个虚拟机,需要指定mac地址,免得与第一个虚拟机冲突:

sudo /etc/qemu-ifup tap1
sudo kvm -localtime -m 1024 -drive file=xp1.img,index=0,media=disk,cache=none -soundhw es1370 -net nic,macaddr=DE:AD:AF:22:33:22 -net tap,ifname=tap1,script=no,downscript=no

可以看到主机是172.16.0.1,tap0和tap1在主机上只是链路层的网口,没有地址,而在虚拟机中它们使用隧道也完成各自IP的分配,IP分别是172.16.0.106和172.16.0.107。三台机器可以互相访问,太棒了。/etc/qemu-ifdown实际上没有多大机会执行。


如果需要后台执行:

-vnc none -daemonize


待续:

host文件共享给guest


参考:

http://en.wikibooks.org/wiki/QEMU/Networking

man kvm