只要标记有vmx或svm,就说明支持硬件虚拟化
[root@node2 ~]# grep -E --color=auto "(vmx|svm)" /proc/cpuinfo
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts xtopology tsc_reliable nonstop_tsc aperfmperf unfair_spinlock pni pclmulqdq vmx ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt xsave hypervisor lahf_lm arat epb pln pts dts tpr_shadow vnmi ept vpid fsgsbase smep
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts xtopology tsc_reliable nonstop_tsc aperfmperf unfair_spinlock pni pclmulqdq vmx ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt xsave hypervisor lahf_lm arat epb pln pts dts tpr_shadow vnmi ept vpid fsgsbase smep
装载kvm核心模块:
[root@node2 ~]# modprobe kvm
因为我是Intel的CPU,所以装载intel的kvm模块:
[root@node2 ~]# modprobe kvm-intel
此时内核已经成为kvm-hypervisor
[root@node2 ~]# lsmod | grep kvm
kvm_intel 55496 0
kvm 337772 1 kvm_intel
[root@node2 ~]#
验证是否有KVM文件,存在说明内核已经被kvm寄宿了
[root@node2 ~]# ll /dev/kvm
crw-rw---- 1 root root 10, 232 Sep 15 23:14 /dev/kvm
查看包组:
[root@node2 ~]# yum grouplist | grep -i "virtualization"
Virtualization
Virtualization Client
Virtualization Platform
Virtualization Tools
[root@node2 ~]# yum install qemu-kvm
[root@node2 ~]# rpm -ql qemu-kvm | grep qemu-kvm | head -2
[root@node2 ~]# ln -sv /usr/libexec/qemu-kvm /usr/bin/
`/usr/bin/qemu-kvm‘ -> `/usr/libexec/qemu-kvm‘
只要安装Virtualization包组即可
[root@master1 ~]# yum grouplist | grep -i "virtualization"
Virtualization Host
Virtualization
装载kvm
# modprobe kvm
# modprobe kvm-intel
查看kvm模块:
modinfo kvm
[root@master1 ~]# modinfo kvm-intel
[root@master1 ~]# yum install qemu-kvm
[root@master1 ~]# rpm -ql qemu-kvm | grep /libexec/
/usr/libexec/qemu-kvm
[root@master1 ~]# ln -sv /usr/libexec/qemu-kvm /usr/bin/
‘/usr/bin/qemu-kvm’ -> ‘/usr/libexec/qemu-kvm’
[root@master1 ~]# qemu-kvm -h
访问:https://launchpad.net/cirros
查看该网站发现只更新到0.33,获取源码已经移交到github托管。这边下载0.30做实验
[root@master1 ~]# ls
cirros-no_cloud-0.3.0-x86_64-disk.img
[root@master1 ~]# qemu-img info /root/cirros-no_cloud-0.3.0-x86_64-disk.img
image: /root/cirros-no_cloud-0.3.0-x86_64-disk.img
file format: qcow2
virtual size: 39M (41126400 bytes)
disk size: 11M
cluster_size: 65536
Format specific information:
compat: 0.10
内存128M,CPU 1个
[root@master1 ~]# qemu-kvm -m 128 -smp 1 -name "test" -hda cirros-no_cloud-0.3.0-i386-disk.img
VNC server running on `::1:5900‘
Centos7需要先安装图形界面,并切换到图形界面
yum groupinstall "GNOME Desktop" "Graphical Administration Tools"
查看运行级别:
[root@master1 ~]# systemctl get-default
multi-user.target
修改运行级别为图形化界面:
systemctl set-default graphical.target
或者
ln -s /lib/systemd/system/runlevel5.target /etc/systemd/system/default.target
运行图形界面:
startx &
还是需要重启宿主机,才能转发X11图形调用到xmanager
# reboot
安装vnc客户端:
[root@master1 ~]# yum install tigervnc
连接虚拟机:
[root@master1 ~]# vncviewer :5900
切换为管理员
切换qemu监控接口:Ctrl+Alt+2
显示当前虚拟机的名字
显示当前虚拟机的状态
虚拟机切换回命令行:Ctrl+Alt+1
关闭虚拟机,不是断电,而是向里面发送关机信号:
[root@master1 ~]# ps aux | grep qemu-kvm
root 2504 36.5 1.6 561512 65812 pts/0 Sl+ 19:03 0:24 qemu-kvm -m 128 -smp 1 -name test -hda cirros-no_cloud-0.3.0-i386-disk.img
root 2539 0.0 0.0 112660 976 pts/1 S+ 19:04 0:00 grep --color=auto qemu-kvm
[root@master1 ~]#
[root@master1 ~]# kill -9 2504
查看可以模拟的主机类型:
[root@master1 ~]# qemu-kvm -M ?
查看可以模拟的CPU类型:
[root@master1 ~]# qemu-kvm -cpu ?
默认模拟的是QEMU的虚拟CPU,不是底层硬件CPU
想要启动虚拟机时,模拟底层硬件CPU需要加-cpu参数:
[root@master1 ~]# qemu-kvm -m 128 -cpu host -smp 1 -name "test" -hda cirros-no_cloud-0.3.0-i386-disk.img
磁盘映像高级选项:
[root@master1 ~]# qemu-kvm -m 128 -cpu host -smp 1 -name "test" -drive file=cirros-no_cloud-0.3.0-i386-disk.img,if=virtio,media=disk,format=qcow2,cache=writeback
硬盘格式已经显示为VDA
[root@master1 ~]# ls -lht xp.iso
-rw-r--r-- 1 root root 499M Mar 17 20:17 xp.iso
创建目录:
[root@master1 ~]# mkdir -pv /images/windows/
创建磁盘映像文件(20G大小,磁盘类型为元数据类型【稀疏格式】):
[root@master1 ~]# qemu-img create -o size=20G,preallocation=metadata -f qcow2 /images/windows/winxp.qcow2
[root@master1 ~]# ll -lht /images/windows/winxp.qcow2
-rw-r--r-- 1 root root 21G Mar 17 21:01 /images/windows/winxp.qcow2
[root@master1 ~]# du -sh /images/windows/winxp.qcow2
4.1M /images/windows/winxp.qcow2
启动安装window-xp(512内存,1核CPU,CPU使用底层硬件模拟,使用XP镜像安装,启动顺序为光盘-硬盘,第一次重启剔除光盘启动)
[root@master1 ~]# qemu-kvm -m 512 -smp 1 -cpu host -drive file=/images/windows/winxp.qcow2,media=disk -drive file=/root/xp.iso,media=cdrom -boot order=dc,once=d
登陆VNC查看:
[root@master1 ~]# vncviewer :5900
安装系统中
[root@master1 ~]# yum install -y SDL SDL-devel SDL-static
[root@master1 ~]# qemu-kvm -m 128 -smp 1 -name "test" -hda cirros-no_cloud-0.3.0-i386-disk.img -sdl
SDL support is disabled
由于qemu-kvm编译默认没有加入SDL功能,所以该实验跳过
默认0号面板端口为5900,以此类推
[root@master1 ~]# qemu-kvm -m 128 -cpu host -smp 1 -name "test" -drive file=cirros-no_cloud-0.3.0-i386-disk.img,if=virtio,media=disk,format=qcow2,cache=writeback -vnc 10.201.106.131:1
[root@master1 ~]# vncviewer 10.201.106.131:5901
[root@master1 ~]# qemu-kvm -m 128 -cpu host -smp 1 -name "test" -drive file=cirros-no_cloud-0.3.0-i386-disk.img,if=virtio,media=disk,format=qcow2,cache=writeback -vnc 10.201.106.131:1,password -monitor stdio
QEMU 1.5.3 monitor - type ‘help‘ for more information
(qemu)
设置VNC密码
(qemu) change vnc password
Password: ******
[root@master1 ~]# qemu-kvm -m 128 -cpu host -smp 1 -name "test" -drive file=cirros-no_cloud-0.3.0-i386-disk.img,if=virtio,media=disk,format=qcow2,cache=writeback -vnc 10.201.106.131:1,password
切换控制台或系统界面
Ctrl+a,c
显示控制台帮助:Ctrl+a,h
C-a h print this help
C-a x exit emulator
C-a s save disk data back to file (if -snapshot)
C-a t toggle console timestamps
C-a b send break (magic sysrq)
C-a c switch between console and monitor
C-a C-a sends C-a
[root@master1 ~]# qemu-kvm -m 128 -cpu host -smp 1 -name "test" -drive file=cirros-no_cloud-0.3.0-i386-disk.img,if=virtio,media=disk,format=qcow2,cache=writeback -nographic -vga cirrus
查看桥模块,桥是在内核中实现的:
[root@master1 ~]# modinfo bridge
安装桥管理命令:
# yum install -y bridge-utils
查看其文件路径:
[root@master1 ~]# rpm -ql bridge-utils
创建桥:
[root@master1 ~]# brctl addbr br0
关闭桥:
[root@master1 ~]# brctl stp br0 off
[root@master1 ~]# brctl show
启动桥:
[root@master1 ~]# ip link set br0 up
其他:可以用nmtui图形界面添加桥
# nmtui
[root@master1 ~]# qemu-kvm -net nic,model=?
qemu: Supported NIC models: ne2k_pci,i82551,i82557b,i82559er,rtl8139,e1000,pcnet,virtio
[root@master1 ~]# qemu-kvm -m 128 -cpu host -smp 1 -name "test" -drive file=cirros-no_cloud-0.3.0-i386-disk.img,if=virtio,media=disk,format=qcow2,cache=writeback -nographic -net nic -net tap,ifname=vif0.0,script=no
内部的系统网卡类型默认是e1000
# lsmod | grep e1000
e1000 101773 0
设置宿主机的后端网卡
首先配置启动和关闭脚本:
[root@master1 ~]# vim /etc/qemu-ifup
#!/bin/bash
#
bridge=br0
if [ -n "$1" ]; then
ip link set $1 up
brctl addif $bridge $1
[ $? -eq 0 ] && exit 0 || exit 1
else
echo "Error,no interface specified"
exit 1
fi
[root@master1 ~]# chmod +x /etc/qemu-ifup
启动测试:
[root@master1 ~]# qemu-kvm -m 128 -cpu host -smp 1 -name "test" -drive file=cirros-no_cloud-0.3.0-i386-disk.img,if=virtio,media=disk,format=qcow2,cache=writeback -nographic -net nic -net tap,ifname=vif0.0,script=/etc/qemu-ifup
[root@master1 ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.66fc76ef2ced no vif0.0
[root@master1 ~]# qemu-kvm -m 128 -cpu host -smp 1 -name "test1" -drive file=cirros-no_cloud-0.3.0-i386-disk.img,if=virtio,media=disk,format=qcow2,cache=writeback -nographic -net nic -net tap,ifname=vif1.0,script=/etc/qemu-ifup
[root@master1 ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.66fc76ef2ced no vif0.0
vif1.0
# hostname
test
# ifconfig eth0 192.168.1.1/24
# hostname
test1
# ifconfig eth0 192.168.1.2/24
不通,原因是MAC地址相同,需要随机生成:
[root@master1 ~]# qemu-kvm -m 128 -cpu host -smp 1 -name "test1" -drive file=cirros-no_cloud-0.3.0-i386-disk.img,if=virtio,media=disk,format=qcow2,cache=writeback -nographic -net nic,macaddr=52:54:00:12:34:57 -net tap,ifname=vif1.0,script=/etc/qemu-ifup
网络可以通了:
[root@master1 ~]# ip link add veth0.0 type veth peer veth1.0
[root@master1 ~]# ifconfig -a | grep veth
veth0: flags=4098<BROADCAST,MULTICAST> mtu 1500
veth1.0: flags=4098<BROADCAST,MULTICAST> mtu 1500
veth0留在物理机,veth1添加到br0
激活两块网卡:
[root@master1 ~]# ip link set veth0 up
[root@master1 ~]# ip link set veth1.0 up
将vet1.0添加至br0
[root@master1 ~]# brctl addif br0 veth1.0
[root@master1 ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.c2e8d51d8e78 no veth1.0
[root@master1 ~]# ifconfig veth0 192.168.1.254/24
在KVM上的虚拟机测试ping
# hostname
test1
# ping 192.168.1.254
PING 192.168.1.254 (192.168.1.254): 56 data bytes
64 bytes from 192.168.1.254: seq=0 ttl=64 time=13.576 ms
64 bytes from 192.168.1.254: seq=1 ttl=64 time=2.025 ms
KVM客户机添加去往物理机地址的网关:
# route add default gw 192.168.1.254
已经可以ping通物理机:
# ping 10.201.106.131
PING 10.201.106.131 (10.201.106.131): 56 data bytes
64 bytes from 10.201.106.131: seq=0 ttl=64 time=1.828 ms
64 bytes from 10.201.106.131: seq=1 ttl=64 time=2.080 ms
[root@master1 ~]# cat /proc/sys/net/ipv4/ip_forward
0
[root@master1 ~]# echo 1 > /proc/sys/net/ipv4/ip_forward
但是这时候还是通不了外网,因为包发出去回不来:
root@master1 ~]# iptables -t nat -A POSTROUTING -s 192.168.1.0/24 ! -d 192.168.1.0/24 -o eth0 -j SNAT --to-source 10.201.106.131
KVM虚拟机已经可以ping通外网
[root@master1 ~]# ip link set veth0 down
[root@master1 ~]# ip link set veth1.0 down
[root@master1 ~]# ifconfig br0 192.168.1.254/24 up
KVM测试外网联通性:
# ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8): 56 data bytes
64 bytes from 8.8.8.8: seq=0 ttl=127 time=72.507 ms
64 bytes from 8.8.8.8: seq=1 ttl=127 time=114.481 ms
抓包,br0上没有转换
是在物理网卡eth0转换的
[root@master1 ~]# ip addr del 192.168.1.254/24 dev br0
一旦物理网卡添加到br0,物理网卡就变成了交换机
首先拆除物理网卡地址,然后将物理网卡添加至br0,最后在br0配置原先的物理网卡的IP地址。由于担心xshell断掉,直接在终端操作:
[root@master1 ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c29a77aec no eth0
vif0.0
vif1.0
# ifconfig eth0 10.201.106.81/24
# route add default gw 10.201.106.2
# ifconfig eth0 10.201.106.82/24
# route add default gw 10.201.106.2
又可以ping通外网了:
抓包物理机网卡,源地址全是KVM虚拟机IP发出的包:
关机前:
bridge name bridge id STP enabled interfaces
br0 8000.000c29a77aec no eth0
vif0.0
关机 vif1.0
# poweroff
会自动拆除网卡:
[root@master1 ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c29a77aec no eth0
vif1.0
[root@master1 ~]# mkdir /images/centos
[root@master1 ~]# qemu-img create /images/centos/centos6.img -o size=120G,preallocation=metadata -f qcow2
[root@master1 ~]# qemu-kvm -m 512 -smp 1 -name centos -drive file=/images/centos/centos6.img,media=disk,if=virtio -net nic,model=virtio,macaddr=52:54:00:66:66:66 -net tap,ifname=centos6.0,script=/etc/qemu-ifup -boot order=nc,once=n
我没有PXE环境,所以一直是获取IP地址界面
[root@master1 ~]# qemu-kvm -m 512 -smp 1 -name centos -drive file=/images/centos/centos6.img,media=disk,if=virtio -drive file=/root/centos6.iso,media=cdrom -boot order=dc,once=d -net nic,model=virtio,macaddr=52:54:00:66:66:66 -net tap,ifname=centos6.0,script=/etc/qemu-ifup
安装完毕查看模块,网卡等设备是半虚拟化的virtio
在待迁入目的宿主机使用(指定哪个端口接收待迁移虚拟机进入)
# qemu-kvm -vnc :N -incoming tcp:0:7777
# vncviewer :590N
在源主机使用monitor接口,指定要迁入的宿主机信息:
migrate tcp:DEST_IP:DEST:PORT
Ctrl+Alt+2 切换至控制台
Ctrl+Alt+1 切换回系统操作界面
CPU是用线程模拟的
原文:http://blog.51cto.com/zhongle21/2090767