kvm初体验

2012年4月13日 发表评论 阅读评论 4,838 次浏览

硬件环境如下:一台host主机,上面安装的是CentOS 5.4,ip地址为200.200.77.163,内核已被我升级为支持kvm的2.6.30:

[root@localhost kvm]# cat /etc/issue
CentOS release 5.4 (Final)
Kernel \r on an \m

[root@localhost linux-2.6.30]# uname -a
Linux localhost.localdomain 2.6.30 #5 SMP Tue Aug 10 00:56:36 CST 2010 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost kvm]# ifconfig eth0 | grep inet
          inet addr:200.200.77.163  Bcast:200.200.0.255  Mask:255.255.252.0
          inet6 addr: fe80::201:29ff:fe50:4673/64 Scope:Link
[root@localhost kvm]# 

要让内核支持kvm功能,只需选中如下一些内核编译选项即可:

--- Virtualization
  <M>   Kernel-based Virtual Machine (KVM) support
  <M>     KVM for Intel processors support
  < >     KVM for AMD processors support (NEW)
  [*]     KVM trace support
  <M>   PCI driver for virtio devices (EXPERIMENTAL)
  < >   Virtio balloon driver (EXPERIMENTAL)

kvm还需cpu的虚拟化支持,检查/proc/cpuinfo,如果在cpu flags字段看到了vmx(intel)或smx(amd),那么你的处理器就支持KVM,另外,要真正能够使用它当然还需要在BIOS内开启:
[root@localhost ~]# cat /proc/cpuinfo | grep vmx
这些条件都满足后,执行modprobe kvm-intel或modprobe kvm-amd,把对应的模块加载上,一切准备OK,开始KVM体验。
这台机器放在实验室里,而我的操作是通过另外主机(安装xp系统,以下称之为client主机)上的Xshell远程到该host主机的ssh server进行的,操作界面当然是cli接口。
首先准备kvm的应用层工具,这只需从http://sourceforge.net/projects/kvm/files/qemu-kvm/下载到最新的qemu-kvm-release.tar.gz即可,我这里是qemu-kvm-1.0.tar.gz,解压安装(./configure; make; make install)后,先创建一个磁盘镜像:

[root@localhost kvm]# ls
CentOS-5.5-x86_64-bin-DVD-1of2.iso  qemu-kvm-1.0  qemu-kvm-1.0.tar.gz
[root@localhost kvm]# qemu-img create -f qcow2 vdisk.img 160G
Formatting 'vdisk.img', fmt=qcow2 size=171798691840 encryption=off cluster_size=65536 
[root@localhost kvm]# ls -lsh
total 4.2G
4.1G -rwxr-xr-x  1 root root 4.1G Nov 24  2010 CentOS-5.5-x86_64-bin-DVD-1of2.iso
 24K drwxrwxr-x 50 root root  20K Apr 12 18:03 qemu-kvm-1.0
6.0M -rw-r--r--  1 root root 6.0M Apr 12 01:17 qemu-kvm-1.0.tar.gz
144K -rw-r--r--  1 root root 195K Apr 12 20:19 vdisk.img
[root@localhost kvm]#

这是一个160GB的磁盘镜像,不过不用当心会等很久,因为它采用的是Copy on write策略,并不会一开始就占用160G的实体磁盘,而只是在使用过程中逐步增大,当然最大上限是160GB。
CentOS-5.5-x86_64-bin-DVD-1of2.iso是我们的CentOS光盘iso镜像,下面用它来安装一个全新CentOS的KVM虚拟客户机(以下称之为kvm客户机):

[root@localhost kvm]# qemu-system-x86_64 -hda vdisk.img -cdrom CentOS-5.5-x86_64-bin-DVD-1of2.iso -m 1024 -vnc :1 -daemonize

由于我的host主机并没有启动X Window,所以这里加上-vnc选项(http://www.linux-kvm.com/content/tip-how-run-headless-guest-machine-using-vnc-kvm)以便我可以从client主机直接远程链接到kvm客户机,在client主机上安装好vnc客户端后,就可以链接kvm客户机了。我这里安装vnc-P4_2_5-x86_win32_PConline.exe,然后执行开始 –> 所有程序 –> RealVNC –> VNC Viewer 4 –> Run VNC Viewer,在Server:里输入200.200.77.163:1,Encryption:里选择Default,点OK按钮后就连上了:

接下来的CentOS-6.2系统安装无需多说,安装完成后自动重启,正常进入系统,看来我人品比较好,一切OK:

发现kvm客户机只有一个cpu,此时可通过-smp指定,比如指定两个cpu:

[root@localhost kvm]# qemu-system-x86_64 -hda vdisk.img -cdrom CentOS-5.5-x86_64-bin-DVD-1of2.iso -m 1024 -vnc :1 -daemonize -smp 2
[root@localhost kvm]# 


退出kvm客户机的话,执行halt即可。当然,粗暴点的方法也有,比如直接在host主机里kill掉对应的qemu进程。
在进行初步的性能测试,首先下载应用层工具源码:
http://sourceforge.net/projects/bridge/
bridge-utils-1.5.tar.gz
http://sourceforge.net/projects/tunctl/
tunctl-1.5.tar.gz
安装:

[root@localhost kvm]# tar xzf tunctl-1.5.tar.gz 
[root@localhost kvm]# tar xzf bridge-utils-1.5.tar.gz 
[root@localhost kvm]# cd tunctl-1.5
[root@localhost tunctl-1.5]# make
...
[root@localhost tunctl-1.5]# make install
...
[root@localhost tunctl-1.5]# cd ../bridge-utils-1.5
[root@localhost bridge-utils-1.5]# autoconf 
[root@localhost bridge-utils-1.5]# ./configure 
...
[root@localhost bridge-utils-1.5]# make
...
[root@localhost bridge-utils-1.5]# make install
...
[root@localhost bridge-utils-1.5]# 

设置并启动虚拟机:

[root@localhost bridge-utils-1.5]# cd ../
[root@localhost kvm]# brctl addbr br0
[root@localhost kvm]# brctl addif br0 eth8
[root@localhost kvm]# cat network/qemu-ifup 
#!/bin/sh
set -x

switch=br0

if [ -n "$1" ];then
	tunctl -u `whoami` -t $1
	sleep 0.5s
	ip link set $1 up
	brctl addif $switch $1
	exit 0
else
	echo "Error: no interface specified"
	exit 1
fi
[root@localhost kvm]# qemu-system-x86_64 -hda vdisk.img -cdrom CentOS-5.5-x86_64-bin-DVD-1of2.iso -m 1024 -vnc :1 -daemonize -cpu host -net nic,macaddr=00:0C:29:AF:66:66,model=virtio -net tap,script=/home/kvm/network/qemu-ifup
+ switch=br0
+ '[' -n tap0 ']'
++ whoami
+ tunctl -u root -t tap0
TUNSETIFF: Device or resource busy
+ sleep 0.5s
+ ip link set tap0 up
+ brctl addif br0 tap0
+ exit 0
[root@localhost kvm]# 

启动后,在kvm客户机里给eth0配置ip:ifconfig eth0 192.168.166.2,在host主机内给br0设置ip:

[root@localhost kvm]# ifconfig br0 192.168.166.1
[root@localhost kvm]# ping 192.168.166.2
PING 192.168.166.2 (192.168.166.2) 56(84) bytes of data.
64 bytes from 192.168.166.2: icmp_seq=1 ttl=64 time=0.164 ms
64 bytes from 192.168.166.2: icmp_seq=2 ttl=64 time=0.129 ms
^C
--- 192.168.166.2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.129/0.146/0.164/0.021 ms
[root@localhost kvm]# 

再在kvm客户机内ping 192.168.166.1,OK。
在client主机(加一个ip地址:192.168.166.166)ping 192.168.166.2,OK。
在kvm客户机内ping 192.168.166.166,OK。
进行轰流量测试,在host主机内和kvm客户机内都装好iperf:http://sourceforge.net/projects/iperf/,以host主机为例:

[root@localhost kvm]# tar xzf iperf-2.0.5.tar.gz 
[root@localhost kvm]# cd iperf-2.0.5
[root@localhost iperf-2.0.5]# ./configure
...
[root@localhost iperf-2.0.5]# make
...
[root@localhost iperf-2.0.5]# make install
...
[root@localhost iperf-2.0.5]# 

测试结果如下(这里,kvm客户机只使用了一个cpu,性能结果不具备代表性,仅以描述测试过程),执行过程中如果提示“connect failed: No route to host”则需先关闭防火墙:

[root@localhost iperf-2.0.5]# /etc/init.d/iptables stop

kvm客户机内:

[root@localhost iperf-2.0.5]# uname -a
Linux localhost.localdomain 2.6.18-194.el5 #1 SMP Fri Apr 2 14:58:14 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost iperf-2.0.5]# cat /proc/cpuinfo 
processor	: 0
vendor_id	: GenuineIntel
cpu family	: 6
model		: 23
model name	: Pentium(R) Dual-Core  CPU      E6700  @ 3.20GHz
stepping	: 10
cpu MHz		: 3303.978
cache size	: 4096 KB
fpu		: yes
fpu_exception	: yes
cpuid level	: 13
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx lm constant_tsc up pni cx16 lahf_lm
bogomips	: 6607.95
clflush size	: 64
cache_alignment	: 64
address sizes	: 40 bits physical, 48 bits virtual
power management:

[root@localhost iperf-2.0.5]# src/iperf -w 64000 -i 1 -p 5005 -s #udp
------------------------------------------------------------
Server listening on TCP port 5005
TCP window size:  125 KByte (WARNING: requested 62.5 KByte)
------------------------------------------------------------
[  4] local 192.168.166.2 port 5005 connected with 192.168.166.1 port 39589
[ ID] Interval       Transfer     Bandwidth
[  4]  0.0- 1.0 sec   208 MBytes  1.75 Gbits/sec
[  4]  1.0- 2.0 sec   200 MBytes  1.68 Gbits/sec
[  4]  2.0- 3.0 sec   196 MBytes  1.64 Gbits/sec
[  4]  3.0- 4.0 sec   191 MBytes  1.60 Gbits/sec
[  4]  4.0- 5.0 sec   207 MBytes  1.73 Gbits/sec
[  4]  5.0- 6.0 sec   202 MBytes  1.69 Gbits/sec
[  4]  6.0- 7.0 sec   207 MBytes  1.73 Gbits/sec
[  4]  7.0- 8.0 sec   195 MBytes  1.63 Gbits/sec
[  4]  8.0- 9.0 sec   174 MBytes  1.46 Gbits/sec
[  4]  9.0-10.0 sec   194 MBytes  1.63 Gbits/sec
[  4] 10.0-11.0 sec   187 MBytes  1.57 Gbits/sec
[  4] 11.0-12.0 sec   203 MBytes  1.70 Gbits/sec
[  4] 12.0-13.0 sec   204 MBytes  1.72 Gbits/sec
[  4] 13.0-14.0 sec   187 MBytes  1.57 Gbits/sec
[  4] 14.0-15.0 sec   202 MBytes  1.69 Gbits/sec
[  4] 15.0-16.0 sec   204 MBytes  1.71 Gbits/sec
[  4] 16.0-17.0 sec   199 MBytes  1.67 Gbits/sec
[  4] 17.0-18.0 sec   203 MBytes  1.71 Gbits/sec
[  4] 18.0-19.0 sec   199 MBytes  1.67 Gbits/sec
[  4] 19.0-20.0 sec   195 MBytes  1.63 Gbits/sec
[  4]  0.0-20.0 sec  3.86 GBytes  1.66 Gbits/sec

host主机内:

[root@localhost iperf-2.0.5]# iperf -w 64000 -p 5005 -i 1 -t 20 -c 192.168.166.2
------------------------------------------------------------
Client connecting to 192.168.166.2, TCP port 5005
TCP window size:  125 KByte (WARNING: requested 62.5 KByte)
------------------------------------------------------------
[  3] local 192.168.166.1 port 39589 connected with 192.168.166.2 port 5005
[ ID] Interval       Transfer     Bandwidth
[  3]  0.0- 1.0 sec   208 MBytes  1.75 Gbits/sec
[  3]  1.0- 2.0 sec   200 MBytes  1.68 Gbits/sec
[  3]  2.0- 3.0 sec   196 MBytes  1.64 Gbits/sec
[  3]  3.0- 4.0 sec   191 MBytes  1.61 Gbits/sec
[  3]  4.0- 5.0 sec   207 MBytes  1.73 Gbits/sec
[  3]  5.0- 6.0 sec   202 MBytes  1.69 Gbits/sec
[  3]  6.0- 7.0 sec   206 MBytes  1.73 Gbits/sec
[  3]  7.0- 8.0 sec   195 MBytes  1.64 Gbits/sec
[  3]  8.0- 9.0 sec   174 MBytes  1.46 Gbits/sec
[  3]  9.0-10.0 sec   194 MBytes  1.63 Gbits/sec
[  3] 10.0-11.0 sec   188 MBytes  1.57 Gbits/sec
[  3] 11.0-12.0 sec   203 MBytes  1.70 Gbits/sec
[  3] 12.0-13.0 sec   204 MBytes  1.71 Gbits/sec
[  3] 13.0-14.0 sec   188 MBytes  1.57 Gbits/sec
[  3] 14.0-15.0 sec   202 MBytes  1.69 Gbits/sec
[  3] 15.0-16.0 sec   204 MBytes  1.71 Gbits/sec
[  3] 16.0-17.0 sec   199 MBytes  1.67 Gbits/sec
[  3] 17.0-18.0 sec   203 MBytes  1.70 Gbits/sec
[  3] 18.0-19.0 sec   199 MBytes  1.67 Gbits/sec
[  3] 19.0-20.0 sec   194 MBytes  1.63 Gbits/sec
[  3]  0.0-20.0 sec  3.86 GBytes  1.66 Gbits/sec
[root@localhost iperf-2.0.5]#

参考:
http://www.linux-kvm.com/
http://www.linux-kvm.org/

转载请保留地址:http://www.lenky.info/archives/2012/04/1531http://lenky.info/?p=1531


备注:如无特殊说明,文章内容均出自Lenky个人的真实理解而并非存心妄自揣测来故意愚人耳目。由于个人水平有限,虽力求内容正确无误,但仍然难免出错,请勿见怪,如果可以则请留言告之,并欢迎来讨论。另外值得说明的是,Lenky的部分文章以及部分内容参考借鉴了网络上各位网友的热心分享,特别是一些带有完全参考的文章,其后附带的链接内容也许更直接、更丰富,而我只是做了一下归纳&转述,在此也一并表示感谢。关于本站的所有技术文章,欢迎转载,但请遵从CC创作共享协议,而一些私人性质较强的心情随笔,建议不要转载。

法律:根据最新颁布的《信息网络传播权保护条例》,如果您认为本文章的任何内容侵犯了您的权利,请以Email或书面等方式告知,本站将及时删除相关内容或链接。