说明:这篇笔记为更好的管理虚拟化服务,如果仅仅是测试虚拟化,查看另一篇笔记:
补充:这篇笔记使用的libvirt版本为0.6.3,kernel为2.6.18-194.26.1.el5
遇到了几个bug,建议首先升级kernel,然后升级libvirt,kvm。
yum clean all
yum makecache (一定要做)
yum list kernel
20110428:kernel 2.6.18-238.9.1.el5
yum update kernel kernel-devel.x86_64 kernel-headers.x86_64
一、安装虚拟化管理工具
1、yum install virt-manager libvirt libvirt-python python-virtinst
KVM Package GroupRHEL comes with KVM software group which includes full virtualization support with KVM. You can list all packages in the group as follows:
# yum groupinfo KVM
2、Start the libvirtd daemon service
service libvirtd start
关于错误:
注:该错误出现在centos5.4,libvirtd 0.6.3
重启libvirtd后,启动guest时会出现下面的问题。
Apr 22 09:16:20 srv4 kernel: libvirtd[27156]: segfault at 00000000444e3000 rip 0000003e5501e161 rsp 00000000444dfcd0 error 4
Apr 22 09:16:20 srv4 libvirtd: 09:16:20.739: error : internal error Unable to daemonize QEMU process
重启了host,问题解决
原因:https://bugzilla.redhat.com/show_bug.cgi?id=505625
Everytime the virGetCapabilities() method is run, it destroys the existing virCapsPtr object and creates it again....without any of the selinux info. virt-manager runs this method. so once virt-manager has run, subsequent calls to get the security info will give the crash you see.
二、有关网络
1、一般网络(又名NAT):guest可以访问外网,外网不能访问guest。
安装了libvirt后,一个NAT网络即可用。host被设置为192.168.122.1(virbr0),所有的guest将被分配为192.168.122.x
如果看不到virbr0
或者在virsh start <Name of KVM>时出现下面的错误:
Apr 22 08:50:37 srv4 libvirtd: 08:50:37.839: error : internal error Failed to add tap interface 'vnet%d' to bridge 'virbr0' : No such device
查看/var/log/message.log
Apr 22 08:57:10 srv4 dnsmasq[26000]: failed to open pidfile /var/run/libvirt/network/default.pid: Permission denied
Apr 22 08:57:10 srv4 dnsmasq[26000]: FAILED to start up
手动启动default network
virsh net-start default
error: Failed to start network default
error: internal error '/usr/sbin/dnsmasq --strict-order --bind-interfaces --pid-file=/var/run/libvirt/network/default.pid --conf-file= --listen-address 192.168.122.1 --except-interface lo --dhcp-range 192.168.122.2,192.168.122.254 --dhcp-lease-max=253' exited with non-zero status 3 and signal 0:
dnsmasq: failed to open pidfile /var/run/libvirt/network/default.pid: Permission denied
解决办法:
我的当前系统:selinux-policy.noarch 2.4.6-203.el5
yum updata selinux-policy.noarch
selinux-policy.noarch 2.4.6-279.el5_5.2
service libvirtd restart
注:需要重启host,上面写了原因
一个故障:
不论是NAT,还是Bridge模式,guest都无法与其他服务器通信,而且NAT模式时无法通过dhcp获取IP。没有查到错误原因。
不使用virsh来启动,按照http://blog.chinaunix.net/space.php?uid=1838361&do=blog&id=234090文章介绍的方法来启动时,NAT及bridge模式都可以正常访问其他服务器。所以按照该文章的方法,在guest的xml文件中定义interface的部分,指定虚拟网卡为tap0,网络正常了,将指定的网卡名去掉,由virsh自动分配为vnet0,网络也是正常的,也可以通过dhcp获取IP了。原因未知。
另:virsh启动的log文件位于:/var/log/libvirt/qemu/
20110711:
nat方式无法获取IP,service libvirtd restart 重启服务,恢复。
关于NAT方式,guest可以获取ip,但无法与访问公网:
有多个网卡?你的物理机由哪个网卡访问公网?例如,我的物理机由br0访问公网,则,修改default.xml文件,添加forward的dev参数。
- virsh # net-define /etc/libvirt/qemu/network/default.xml
- virsh # net-destroy default
- virsh # net-start default
2、Bridge网络(又名:共享物理网卡):guest可以访问外网,外网也可以访问guest
# cd /etc/sysconfig/network-scripts/
# cp ifcfg-eth0 ifcfg-br0
两个配置文件内容分别为:
/etc/sysconfig/network-scripts/ifcfg-eth0
/etc/sysconfig/network-scripts/ifcfg-br0
The NM_CONTROLLED=no option was added because both device should not be controlled by the Network Manager for bridge to work.
重启network(当前连接会丢失,确保你总是可以访问host,否则要格外小心)
service network restart
关于错误:Bringing up interface eth0: tg3 device eth0 does not seem to be present, delaying initialization.
原因:br0里没有加配置 TYPE=Bridge
对于rhel6和centos6,需要关闭NetworkManager
service NetworkManager stop
chkconfig --level 35 NetworkManager off
Disable Netfilter processing in the bridged traffic.
在/etc/sysctl.conf文件中添加:
验证是否生效
ifconfig
br0现在扮演eth0的角色。
br0 Link encap:Ethernet HWaddr 00:14:5E:C2:1E:40
inet addr:221.193.136.121 Bcast:221.193.136.255 Mask:255.255.255.0
inet6 addr: fe80::214:5eff:fec2:1e40/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:125 errors:0 dropped:0 overruns:0 frame:0
TX packets:81 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:16078 (15.7 KiB) TX bytes:18542 (18.1 KiB)
eth0 Link encap:Ethernet HWaddr 00:14:5E:C2:1E:40
inet6 addr: fe80::214:5eff:fec2:1e40/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:206 errors:0 dropped:0 overruns:0 frame:0
TX packets:58 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:27308 (26.6 KiB) TX bytes:13881 (13.5 KiB)
Interrupt:74 Memory:da000000-da012800
也可以通过下面的命令查看桥接网络,可以看到eth0加到了桥接设备br0中:
# brctl show
bridge name bridge id STP enabled interfaces
virbr0 8000.000000000000 yes
br0 8000.000e0cb30550 no eth0
三、创建KVM虚拟机的XML定义文件
关于KVM XML format http://libvirt.org/formatdomain.html
1.1 用于从磁盘启动的XML文件
1、kvm guest 定义开始
2、guest的short name。由字母和数字组成,不能包含空格
3、uuid,由命令行工具 uuidgen生成。
4、在不reboot guest的情况下,guset可以使用的最大内存,以KB为单位
5、guest启动时内存,可以通过virsh setmem来调整内存,但不能大于最大可使用内存。
6、分配的虚拟cpu
7、有关OS
x86架构:i686
hvm:全虚拟化
kernel:指定guest使用的内核,如果使用ISO(安装时)或guset系统中(系统已经安装完成)的内核,不需要指定该项
initrd:指定guest使用的ram disk,如果使用ISO(安装时)或guest系统中(系统已经安装完成)的ram disk,不需要指定该项
注:kernel 和initrd文件位于RHEL系统光盘的images/pxeboot目录,拷贝这两个文件到本地磁盘,并指定路径。
注:这两个元素,如果是为了安装guset而指定,在安装完成以后即可以删除。
如果host开启了SELINUX,you also need to change the type of security context for the files to virt_image_t to allow libvirtd to access them for booting:
After you change the security context, verify that the correct security context is assigned to them as shown in the following example:
boot:指定启动设备,可以重复多行,指定不同的值,作为一个启动设备列表。
可以在cmdline元素中添加启动参数,例如,使用kickstart文件:
<cmdline>method=http://10.1.1.212/install/rhel5.4/x86_64
ks=http://10.1.1.212/install/autoinst/c20m2n05v3</cmdline>
8、处理器特性
关于处理器特性,查看:
9、时钟。使用本地时间:localtime
<clock offset='utc'/>
仍然需要再计划任务中添加时间同步:
*/15 * * * * (/usr/sbin/ntpdate 210.72.145.44 > /dev/null 2>&1;/sbin/hwclock -w)
10、定义了在kvm环境中power off,reboot,或crash时的默认的动作为destroy。其他允许的动作包括:restart,preserve,rename-restart.
在这个例子中,destory之所以也用在了on_reboot元素中,是因为一旦安装完成,期望的guest动作是stop,以便从the installed guest operating system而不是从the installation kernel or ISO编辑guest的定义。
destroy:停止该虚拟机。相当于关闭电源
11、设备定义开始
12、模拟元素,此处写法用于kvm的guest(xen怎么写?)
13、用于kvm存储的文件。在这个例子中,在guest中显示为IDE设备。
使用qemu-img命令创建该文件,kvm image的默认目录为:/var/lib/libvirt/images/
这里需要注意:使用libvirt 0.6.3写的xml,没有<driver name='qemu' type='qcow2'/>参数,当将该xml放到libvirt 0.8版本时,该参数会默认为raw,所以会造成在0.6中可以启动的guest无法启动,更改参数即可
使用virtio:
采用普通的驱动,即硬盘和网卡都采用默认配置情况下,硬盘是 ide 模式,而网卡工作在 模拟的rtl 8139 网卡下,速度为100M 全双工。采用 virtio 驱动后,网卡工作在 1000M 的模式下,硬盘工作是SCSI模式下。
硬盘采用 virtio 后,安装windows 系统,将不能正常的识别硬盘,解决的方法是:
从kvm 的官网下载virtio的驱动iso。
1. 先采用ide模式安装系统。
2. 安装完成后,添加一个virtio模式的硬盘。
3. 启动vm后,系统会自动搜索 SCSI的驱动,找到下载的virtio 驱动后,安装即可。
4. 修改vm 配置文件,删除掉添加的 vitro 硬盘后,修改ide硬盘为 virtio模式即可。
3. 启动vm后,系统会自动搜索 SCSI的驱动,找到下载的virtio 驱动后,安装即可。
4. 修改vm 配置文件,删除掉添加的 vitro 硬盘后,修改ide硬盘为 virtio模式即可。
A Note About Virtio Drivers
Virtio is paravirtualized drivers for kvm/Linux. With this you can can run multiple virtual machines running unmodified Linux or Windows VMs. Each virtual machine has private virtualized hardware a network card, disk, graphics adapter, etc. According to Redhat:
Para-virtualized drivers enhance the performance of fully virtualized guests. With the para-virtualized drivers guest I/O latency decreases and throughput increases to near bare-metal levels. It is recommended to use the para-virtualized drivers for fully virtualized guests running I/O heavy tasks and applications.
关于cache=none
如果不加该参数,默认cache policy为write through。
kvm官方及IBM都推荐使用raw,设置cache为none,以关闭kvm磁盘的cache策略,让数据可以直接落在实体硬盘上(实体机上其实也有磁盘cache的,并不需要kvm为cache操心)
For the best storage performance on guest operating systems that use raw disk volumes or partitions,completely avoid the page cache on the host.
下面的帖子中有一段关于cache的内容:
补充:可以定义多个磁盘,其他可行的磁盘定义:
Logical volume device:
Disk partition:
CD-ROM device:
14、使用网桥类型。确保每个kvm guest的mac地址唯一。将创建tun设备,名称为vnetx(x为0,1,2...)
补充:使用默认的虚拟网络代替网桥,即guest为NAT模式。也可以省略mac地址元素,这样将自动生成mac地址。
默认分配192.168.122.x/24的地址,也可以手动指定。网关为192.168.122.1
使用virtio:
采用普通的驱动,即硬盘和网卡都采用默认配置情况下,硬盘是 ide 模式,而网卡工作在 模拟的rtl 8139 网卡下,速度为100M 全双工。采用 virtio 驱动后,网卡工作在 1000M 的模式下,硬盘工作是SCSI模式下。
rhel5.6 guest with virtio interface can not boot successfully if the system installation would use the ide interface
性能:通过物理机上拷贝
普通驱动:26.1M
virtio驱动:44.7M
因为交换机为100M,无法测试从其他物理机上拷贝。
15、输入设备
16、定义与guset交互的图形设备。在这个例子中,使用vnc协议。listen的地址为host的地址。prot为-1,表示自动分配端口号,通过以下的命令查找端口号:
virsh vncdisplay <KVM Guest Name>
注意:是passwd 而不是password
VNC is configured to listen on 127.0.0.1 by default. To make it listen on all public interfaces, edit /etc/libvirt/qemu.conf file.
17、设备定义结束
18、KVM定义结束
完整的xml
1.2 用于从ISO启动的XML文件
virt-install --name vm1 --ram 512 --disk path=/usr/local/kvm/vm1/disk.os,size=10 --accelerate --cdrom=/usr/local/centos5.6/CentOS-5.6-x86_64-bin-DVD-1of2.iso --vnc 0.0.0.0:3
启动后,磁盘空间为0?进入virsh ,etdi 虚拟机.xml,查看磁盘类型,为raw?你创建的磁盘是qcow2?
四、 使用virsh创建一个kvm虚拟机
当创建或编辑了一个kvm xml文件后,使用virsh tool创建kvm。
1、定义KVM
virsh define <Name of XML definition file>
For example:
# virsh define /usr/local/kvm/centvm3.xml
在后台,这个命令拷贝该xml文件到/etc/libvirt/qemu目录,文件名为xml里定义的kvm名称。并且正式定义一个新的kvm。
如何修改?
方法1:
virsh edit <Name of KVM>
virsh define <Name of XML definition file>
方法2:
virsh undefine <Name of KVM>
virsh define <Name of XML definition file>
2、启动KVM
virsh start <Name of KVM>
For example:
# virsh start centosvm3
查看vnc信息:
virsh vncdisplay centosvm3
也可以通过virt-viewer查看
virt-viewer <Name of KVM>
关闭kvm虚拟机:virsh destory <Name of KVM>
查看运行中的虚拟机:
virsh list
3、克隆虚拟机
virt-clone -o vm1 -n vm2 -f /usr/local/kvm/vm2/disk.os /usr/local/kvm/vm2/disk.data -d --prompt
因为我的vm1虚拟机有两个磁盘,所以,在克隆时需要制定两个磁盘文件,如果你不清楚顺序,可以将-f部分去掉,后面的--prompt,交互式,可以根据提示输入磁盘文件。
注意:克隆的xml文件会丢失vnc的passwd参数,需要手动添加。
克隆后,某个磁盘变为实际使用的大小?
qemu-img create -F qcow2 -b ./disk.data.1 -f raw disk.data 80G
错误:
在convert 或 create时,报错:
qemu-img could not open 磁盘文件
问题:当 qemu-img info 磁盘文件存在backing file 项时,会报这个错误。
backing file 来源:当使用qemu-img create创建磁盘时,会创建这个项
另外:当虚拟机正在使用该磁盘时,qemu-img info 磁盘文件时,也会出现这个错误。
关于qemu-img convert:
会创建新格式的磁盘,旧格式的磁盘仍然存在。
关于error: unknown OS type hvm
kernel没有加载模块,检查bios里是否开启了虚拟化
杂项:
1、关闭guest的磁盘监控
# service smartd stop # chkconfig --del smartd
一篇不错的文章:
http://www.cyberciti.biz/faq/centos-rhel-linux-kvm-virtulization-tutorial/