01 kvm技术基础
配置环境:
node1 192.168.1.121 CentOS release 6.7
1、KVM安装
#确保CPU支持HVM
[root@node1 ~]# grep -E --color=auto "(vmx|svm)" /proc/cpuinfo
#装载模块
[root@node1 ~]# modprobe kvm
[root@node1 ~]# modprobe kvm-intel
[root@node1 ~]# yum grouplist | grep -i 'virtualization'
Virtualization
Virtualization Client
Virtualization Platform
Virtualization Tools
[root@node1 ~]# yum install qemu-kvm -y
[root@node1 ~]# ln -sv /usr/libexec/qemu-kvm /usr/bin/qemu-kvm
02 使用qemu-kvm管理kvm虚拟机
配置环境:
node1 192.168.1.131 CentOS Linux release 7.2
1、KVM安装
[root@node1 ~]# modinfo kvm
[root@node1 ~]# modinfo kvm-intel
[root@node1 ~]# modinfo kvm-amd
[root@node1 ~]# modprobe kvm
[root@node1 ~]# lsmod | grep kvm
kvm_intel 162153 0
kvm 525259 1 kvm_intel
#确保CPU支持HVM
[root@node1 ~]# grep -E "(vmx|svm)" /proc/cpuinfo
[root@node1 ~]# yum install qemu-kvm -y
[root@node1 ~]# ln -s /usr/libexec/qemu-kvm /usr/bin/
[root@node1 ~]# qemu-img info cirros-no_cloud-0.3.0-i386-disk.img
p_w_picpath: cirros-no_cloud-0.3.0-i386-disk.img
file format: qcow2
virtual size: 39M (41126400 bytes)
disk size: 11M
cluster_size: 65536
Format specific information:
compat: 0.10
#启动虚拟机
[root@node1 ~]# qemu-kvm -m 128 -smp 2 -name "test" -hda cirros-no_cloud-0.3.0-i386-disk.img
#安装vnc
[root@node1 ~]# yum install tigervnc
#连接至打开的虚拟机
[root@node1 ~]# vncviewer :5900
[root@node1 ~]# qemu-kvm -m 128 -cpu host -smp 2 -name "test" -drive file=cirros-no_cloud-0.3.0-i386-disk.img,if=virtio,media=disk,format=qcow2,cache=writeback
[root@node1 ~]# mkdir -p /p_w_picpaths/windows
[root@node1 ~]# qemu-img -o ? -f qcow2 /p_w_picpaths/windows/winxp.qcow2
[root@node1 ~]# qemu-img create -o size=20G,preallocation=metadata -f qcow2 /p_w_picpaths/windows/winxp.qcow2
[root@node1 ~]# qemu-kvm -m 512 -smp 2 -cpu host -driver file=/p_w_picpaths/windows/winxp.qcow2,media=disk -driver file=/root/winxp_ghost.iso media=cdrom order=dc,once=d
03 使用qemu-kvm管理kvm虚拟机
[root@node1 ~]# qemu-kvm -m 128 -cpu host -smp 2 -name "test" -drive file=cirros-no_cloud-0.3.0-i386-disk.img,if=virtio,media=disk,format=qcow2,cache=writeback -vnc 192.168.1.131:1,password -monitor stdio
[root@node1 ~]# qemu-kvm -m 128 -cpu host -smp 2 -name "test" -drive file=cirros-no_cloud-0.3.0-i386-disk.img,if=virtio,media=disk,format=qcow2,cache=writeback -nographic -monitor stdio
1、管理桥设备
#添加桥
[root@node1 ~]# brctl addbr br0
[root@node1 ~]# ifconfig -a
br0: flags=4098
ether 32:5c:c1:7e:42:c7 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eno16777736: flags=4163
inet 192.168.1.131 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::20c:29ff:fe50:27c4 prefixlen 64 scopeid 0x20
ether 00:0c:29:50:27:c4 txqueuelen 1000 (Ethernet)
RX packets 17736 bytes 9576805 (9.1 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 13771 bytes 9831475 (9.3 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@node1 ~]# brctl stp br0 off
#显示桥
[root@node1 ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000000000000 no
#删除桥
[root@node1 ~]# brctl delbr br0
#激活桥
[root@node1 ~]# ifconfig br0 up
[root@node1 ~]# ip link set dev br0 down
[root@node1 ~]# ip link set dev br0 up
[root@node1 ~]# qemu-kvm -m 128 -cpu host -smp 2 -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,name=vif0.0,script=no
[root@node1 ~]# vim /etc/qemu-ifup
#!/bin/bash
#
bridge=br0
if [ -n "$1" ];then
ip link set $1 up
sleep 1
brctl addif $bridge $1
[ $? -eq 0 ] && exit 0 || exit 1
else
echo "Error: no interface specified."
exit 1
fi
[root@node1 ~]# chmod +x /etc/qemu-ifup
[root@node1 ~]# vim /etc/qemu-ifdown
#!/bin/bash
#
bridge=br0
if [ -n "$1" ];then
brctl delif $bridge $1
ip link set $1 down
exit 0
else
echo "Error: no interface specified."
exit 1
fi
[root@node1 ~]# chmod +x /etc/qemu-ifdown
[root@node1 ~]# qemu-kvm -m 128 -cpu host -smp 2 -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@node1 ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.12188d08e408 no vif0.0
[root@node1 ~]# qemu-kvm -m 128 -cpu host -smp 2 -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=vif1.0,script=/etc/qemu-ifup
[root@node1 ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.12188d08e408 no vif0.0
vif1.0
[root@node1 ~]# ip link add veth1.0 type veth peer veth1.1
[root@node1 ~]# ip link set veth0 up
[root@node1 ~]# ip link set veth1.0 up
[root@node1 ~]# brctl addif br0 veth1.0
[root@node1 ~]# ifconfig veth0 172.16.100.67/24
04 使用qemu-kvm管理kvm虚拟机
[root@node1 ~]# mkdir /p_w_picpaths/centos
[root@node1 ~]# qemu-img create /p_w_picpaths/centos/centos6.img -o size=120G,preallocation=metadata -f qcow2
[root@node1 ~]# qemu-kvm -m 512 -smp 2 -name centos -drive file=/p_w_picpaths/centos/centos6.img,media=disk -net nic,macaddr=52:54:00:55:32:19 -net tap,ifname=centos6.0,script=/etc/qemu-ifup -boot order=nc,once=n
[root@node1 ~]# qemu-kvm -m 512 -smp 2 -name centos -drive file=/p_w_picpaths/centos/centos6.img,media=disk,if=virtio -net nic,model=virtio,macaddr=52:54:00:55:32:19 -net tap,ifname=centos6.0,script=/etc/qemu-ifup -boot order=nc,once=n