#查看你的硬件是否支持虚拟化。命令:
[root@VM_166_143 data]#egrep '(vmx|svm)' /proc/cpuinfo
#安装基础包
[root@VM_166_143 data]#yum -y install kvm libvirt bridge-utils acpid
#启动依赖服务及加载模块
[root@VM_166_143 data]#sed -i '/modprobe kvm/d' /etc/rc.d/rc.local [root@VM_166_143 data]#echo "modprobe kvm" >>/etc/rc.d/rc.local [root@VM_166_143 data]#/etc/init.d/libvirtd start [root@VM_166_143 data]#/etc/init.d/acpid start #检查是否加载虚拟模块 [root@VM_166_143 data]#lsmod |grep kvm kvm_intel 55496 0 kvm 337772 1 kvm_intel
#网桥管理命令查看
#配置网桥 [root@VM_166_143 data]# brctl show bridge name bridge id STP enabled interfaces virbr0 8000.525400671d5e yes virbr0-nic [root@VM_166_143 data]# virsh iface-bridge eth0 br0 Created bridge br0 with attached device eth0 Bridge interface br0 started [root@VM_166_143 data]# brctl show bridge name bridge id STP enabled interfaces br0 8000.000c298a5fb4 yes eth0 virbr0 8000.525400671d5e yes virbr0-nic [root@VM_166_143 data]# ifconfig br0 Link encap:Ethernet HWaddr 00:0C:29:8A:5F:B4 inet addr:192.168.166.143 Bcast:192.168.166.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe8a:5fb4/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:32 errors:0 dropped:0 overruns:0 frame:0 TX packets:31 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:2698 (2.6 KiB) TX bytes:3518 (3.4 KiB) eth0 Link encap:Ethernet HWaddr 00:0C:29:8A:5F:B4 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:21519 errors:0 dropped:0 overruns:0 frame:0 TX packets:10302 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:29679394 (28.3 MiB) TX bytes:1309503 (1.2 MiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:4 errors:0 dropped:0 overruns:0 frame:0 TX packets:4 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:212 (212.0 b) TX bytes:212 (212.0 b) virbr0 Link encap:Ethernet HWaddr 52:54:00:67:1D:5E inet addr:192.168.122.1 Bcast:192.168.122.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
#创建软链接,并创建虚拟机存放目录
[root@VM_166_143 data]# ln -sv /usr/libexec/qemu-kvm /usr/bin/qemu-kvm `/usr/bin/qemu-kvm' -> `/usr/libexec/qemu-kvm' [root@VM_166_143 data]# mkdir -p /data/kvm/ [root@VM_166_143 data]# cd /data/kvm [root@VM_166_143 data]# mkdir test;cd test
#创建虚拟机磁盘文件
[root@VM_166_143 test]# qemu-img create -o preallocation=metadata -f qcow2 linux.qcow2 50G
#创建xml模板文件
[root@VM_166_143 data]# cat test.xml <domain type="kvm"> <name>ebc32bcb-f29a-4450-bb48-b947669a06a8</name> <uuid>ebc32bcb-f29a-4450-bb48-b947669a06a8</uuid> <memory>1048576</memory> <vcpu>1</vcpu> <os> <type arch="x86_64" machine="pc">hvm</type> <boot dev="hd"></boot> </os> <features> <acpi></acpi> <apic></apic> <pae></pae> </features> <clock offset="localtime"></clock> <on_poweroff>destroy</on_poweroff> <on_reboot>restart</on_reboot> <on_crash>destroy</on_crash> <devices> <emulator>/usr/libexec/qemu-kvm</emulator> <disk type="file" device="disk"> <driver name="qemu" type="qcow2"></driver> <source file="/data/kvm/test/linux.qcow2"></source> <target dev="hda" bus="ide"></target> </disk> <interface type="bridge"> <source bridge="br0"></source> </interface> <input type ="tablet" bus="usb"></input> <input type="mouse" bus="ps2"></input> <graphics type="vnc" port="-1" listen="0.0.0.0" keymap="en-us"></graphics> <serial type="pty"> <target port="0"></target> </serial> <console type="pty"> <target type="serial" port="0"></target> </console> </devices> </domain>
#创建虚拟机,不建议使用virsh create,停机后,virsh list --all查看不到虚拟机信息,推荐使用virsh define+virsh start命令
[root@VM_166_143 data]# virsh create test.xml 或者 [root@VM_166_143 data]# virsh define test.xml [root@VM_166_143 data]# virsh start test.xml
#常用命令
#关闭虚拟机 virsh shutdown demo #取消定义虚拟机 virsh undefine demo #重启虚拟机 virsh reboot demo #强制关闭虚拟机 virsh destroy demo #列出所有虚拟机 virsh list --all #在属主机进入虚拟机console virsh console demo 退出虚拟机回到HOST按组合键: Ctrl+]
#虚拟机模板必配的设置
#删除模板的网卡信息,使其可以制作模板 sed -i '/HWADDR/d;/UUID/d' /etc/sysconfig/network-scripts/ifcfg-eth0 rm -f /etc/udev/rules.d/70-persistent-net.rules #虚拟机修改,解决主机virsh console连接不上问题 echo "ttyS0" >> /etc/securetty sed -i 's/quiet console=ttyS0/quiet/g;s/quiet/quiet console=ttyS0/g' /etc/grub.conf sed -i '/respawn/d' /etc/inittab echo "S0:12345:respawn:/sbin/agetty ttyS0 115200" >>/etc/inittab
#常见问题
问题1 #Escape character is ^] #error: internal error cannot find character device (null) 解决方法: https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Virtualization_Host_Configuration_and_Guest_Installation_Guide/App_Domain_Console.html 问题2 [root@VM5137 data]# virsh iface-bridge eth0 br0 Created bridge br0 with attached device eth0 error: Failed to start bridge interface br0 error: internal error failed to create (start) interface br0: failed to execute external program - Running 'ifup eth0' failed with exit code 4: Error: Connection activation failed: Master connection not found or invalid Active connection state: activating Active connection path: /org/freedesktop/NetworkManager/ActiveConnection/1 解决方法: service NetworkManager stop chkconfig NetworkManager off