一、安装虚拟化管理工具
1、yum install virt-manager libvirt libvirt-python python-virtinst
1.1、kmod-kvm : kvm kernel module(s)
2.2、kvm : Kernel-based Virtual Machine
3.3、kvm-qemu-img : Qemu disk image utility
4.4、kvm-tools : KVM debugging and diagnostics tools
5.5、python-virtinst : Python modules and utilities for installing virtual machines
6.6、virt-manager : Virtual Machine Manager (GUI app, to install and configure VMs)
7.7、virt-viewer: Virtual Machine Viewer (another lightweight app to view VM console and/or install VMs)
8.9、bridge-utils : Utilities for configuring the Linux Ethernet bridge (this is recommended for KVM networking)
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.
二、有关网络
http://wiki.libvirt.org/page/Networking
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
2、Bridge网络(又名:共享物理网卡):guest可以访问外网,外网也可以访问guest
# cd /etc/sysconfig/network-scripts/
# cp ifcfg-eth0 ifcfg-br0
两个配置文件内容分别为:
/etc/sysconfig/network-scripts/ifcfg-eth0
1.DEVICE=eth0
2.TYPE=Ethernet
3.ONBOOT=yes
4.NM_CONTROLLED=no
5.BRIDGE=br0
/etc/sysconfig/network-scripts/ifcfg-br0
1.DEVICE=br0
2.TYPE=Bridge
3.NM_CONTROLLED=no
4.BOOTPROTO=static
5.IPADDR=221.193.136.121
6.NETMASK=255.255.255.0
7.ONBOOT=yes
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文件中添加:
1.net.bridge.bridge-nf-call-ip6tables = 0
2.net.bridge.bridge-nf-call-iptables = 0
3.net.bridge.bridge-nf-call-arptables = 0
4.net.bridge.bridge-nf-filter-vlan-tagged = 0
也可以通过下面的命令查看桥接网络,可以看到eth0加到了桥接设备br0中:
# brctl show
bridge name bridge id STP enabled interfaces
virbr0 8000.000000000000 yes
br0 8000.000e0cb30550 no eth0
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:
1.# chcon -t virt_image_t /tmp/vmlinuz-rhel54
2.# chcon -t virt_image_t /tmp/initrd-rhel54.img
After you change the security context, verify that the correct security context is assigned to them as shown in the following example:
1.# ls -Z /tmp|grep virt
2.-r--r--r-- root root root:object_r:virt_image_t initrd-rhel54.img
3.-r--r--r-- root root root:object_r:virt_image_t vmlinuz -rhel54
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>
1.<os>
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的内容:
http://www.linuxeden.com/forum/viewthread.php?tid=205581&extra=&page=3
使用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
https://bugzilla.redhat.com/show_bug.cgi?id=647387
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.
1.<graphics type='vnc' listen='221.191.134.123' passwd='123456' port='-1' autoport='yes' keymap='en-us'/>
17、设备定义结束
1.</devices>
18、KVM定义结束
1.</domain>
完整的xml
1.<domain type='kvm'>
错误:
在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