kvm虚拟化 安装配置

在 CentOS 上安装 kvm 虚拟化环境,包括但不限于:安装 kvm、设置网桥、基本用法、虚拟机快照、虚拟机克隆、虚拟机迁移。

安装 kvm

检查宿主机是否支持 cpu 虚拟化(硬件虚拟化):

egrep 'vmx|svm' /proc/cpuinfo

安装 kvm

yum -y install kvm virt-viewer virt-manager qemu-kvm libvirt python-virtinst bridge-utils

查看 kvm 模块是否加载

lsmod | grep kvm

启动 libvirtd 服务

## centos6
chkconfig libvirtd on
service libvirtd start

## centos7
systemctl enable libvirtd
systemctl start libvirtd

配置网桥

kvm 有两种网络连接方式:nat、bridge;默认是 nat 方式;两种连接方式的区别类似 vmware;

cd /etc/sysconfig/network-scripts/
--- ifcfg-ens38 ---
HWADDR=00:0C:29:0C:7B:71
TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens38
UUID=675a7816-5057-3f6e-87c3-7117cfd12ae8
ONBOOT=yes
AUTOCONNECT_PRIORITY=-999
DEVICE=ens38
BRIDGE=br0  # 添加此行
--- ifcfg-ens38 ---

--- ifcfg-br0 ---
#HWADDR=00:0C:29:0C:7B:71
TYPE=Bridge # 修改为Bridge
BOOTPROTO=dhcp
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=br0    # 修改为br0
#UUID=675a7816-5057-3f6e-87c3-7117cfd12ae8
ONBOOT=yes
AUTOCONNECT_PRIORITY=-999
DEVICE=br0  # 修改为br0
STP=no      # stp;生成树协议;
--- ifcfg-br0 ---

systemctl restart NetworkManager
systemctl restart network   ## 可能会报错,多重启几次就好了

ip addr
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:0c:7b:67 brd ff:ff:ff:ff:ff:ff
    inet 192.168.255.105/24 brd 192.168.255.255 scope global dynamic ens33
       valid_lft 1749sec preferred_lft 1749sec
    inet6 fd15:4ba5:5a2b:1008:add5:e02a:98c9:6dd3/64 scope global noprefixroute dynamic 
       valid_lft 86375sec preferred_lft 14375sec
    inet6 fe80::9321:a09f:3dd2:d7ed/64 scope link 
       valid_lft forever preferred_lft forever
3: ens38:  mtu 1500 qdisc pfifo_fast master br0 state UP qlen 1000
    link/ether 00:0c:29:0c:7b:71 brd ff:ff:ff:ff:ff:ff
4: br0:  mtu 1500 qdisc noqueue state UP qlen 1000
    link/ether 00:0c:29:0c:7b:71 brd ff:ff:ff:ff:ff:ff
    inet 192.168.255.110/24 brd 192.168.255.255 scope global dynamic br0
       valid_lft 1749sec preferred_lft 1749sec
    inet6 fd15:4ba5:5a2b:1008:d24b:e09f:7b1a:7ccd/64 scope global noprefixroute dynamic 
       valid_lft 86375sec preferred_lft 14375sec
    inet6 fe80::8eb2:707c:38a7:b2f9/64 scope link 
       valid_lft forever preferred_lft forever
5: virbr0:  mtu 1500 qdisc noqueue state DOWN qlen 1000
    link/ether 52:54:00:9e:d7:0d brd ff:ff:ff:ff:ff:ff
    inet 192.168.254.254/24 brd 192.168.254.255 scope global virbr0
       valid_lft forever preferred_lft forever
6: virbr0-nic:  mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 1000
    link/ether 52:54:00:9e:d7:0d brd ff:ff:ff:ff:ff:ff

创建虚拟机

## 桥接模式
virt-install \
-n centos7 \
-r 1024 \
--vcpus=1 \
--boot cdrom,hd,network,menu=on \
--disk path=/opt/img/centos7.img,size=20,format=qcow2,bus=virtio \
-c /opt/iso/centos7.iso \
--network bridge=virbr0,model=virtio \
--vnc --vnclisten=0.0.0.0 --vncport=10001 \
--os-type linux \
--os-variant=rhel7 \
--accelerate \
--noautoconsole

## NAT模式(默认)
virt-install \
-n centos7 \
-r 1024 \
--vcpus=1 \
--boot cdrom,hd,network,menu=on \
--disk path=/opt/img/centos7.img,size=20,format=qcow2,bus=virtio \
-c /opt/iso/centos7.iso \
--network network=default,model=virtio \
--vnc --vnclisten=0.0.0.0 --vncport=10001 \
--os-type linux \
--os-variant=rhel7 \
--accelerate \
--noautoconsole

### 参数解释
-n centos7  # 虚拟机名称
-r 1024     # 虚拟机内存大小
--vcpus=1   # 虚拟机cpu数量
--boot cdrom,hd,network,menu=on # 启动顺序
--disk path=/opt/img/centos7.img,size=20,format=qcow2,bus=virtio    # 硬盘
-c /opt/iso/centos7.iso # 系统安装镜像
--network network=default,model=virtio  # 网络模式
--vnc --vnclisten=0.0.0.0 --vncport=10001   # vnc
--os-type linux     # 系统类型
--os-variant=rhel7  # 系统名
--accelerate        # kvm加速器
--noautoconsole     # 取消自动控制

鼠标不同步问题

--- /etc/libvirt/qemu/VM_Name.xml ---
<devices>
...
<input type='tablet' bus='usb'/>
...
</devices>
--- /etc/libvirt/qemu/VM_Name.xml ---

virsh shutdown VM_Name
virsh define /etc/libvirt/qemu/VM_Name.xml
virsh start VM_Name
## 目前个人发现 centos6 有这个问题,centos7 无问题;

virsh 参数

virsh -c qemu+ssh://root@192.168.255.101/system   # 连接libvirtd

virsh list          # 查看虚拟机运行状态
virsh list --all    # 查看所有虚拟机状态

virsh start test    # 启动 虚拟机test
virsh shutdown test # 关闭 虚拟机test
virsh reboot test   # 重启 虚拟机test
virsh destroy test  # 强制关闭 虚拟机test

vitsh define /etc/libvirt/qemu/test.xml # 加载配置文件
virsh undefine test                     # 删除配置文件(虚拟机)
virsh dumpxml test                      # 导出配置
virsh edit test                         # 编辑 配置文件
virsh console test  # 连接 虚拟机test

virsh net-list      # 查看kvm网络状态
virsh net-define /etc/libvirt/qemu/networks/mynet.xml    # 加载网络配置
virsh net-undefine mynet    # 删除网络配置
virsh net-start mynet       # 启用网络mynet
virsh net-destroy mynet     # 停用网络mynet
virsh net-dumpxml mynet     # 导出网络配置
virsh net-edit mynet        # 编辑网络配置

virsh domiflist test    # 查看 虚拟机test 网络状态

qemu-img

qemu-img info /opt/img/centos.img   # 查看img信息
qemu-img create -f qcow2 /opt/img/new.img 20G   # 创建img
qemu-img convert -f raw -O qcow2 test1.img test1.qcow2 ## 转换磁盘格式;raw不支持快照等特性

添加硬盘

virsh shutdown test

qemu-img create -f qcow2 /opt/img/new.img 20G

virsh edit test

--- test.xml ---
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/opt/img/centos.img'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
    </disk>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/opt/img/new.img'/>
      <target dev='vdb' bus='virtio'/>
    </disk>
--- test.xml ---

virsh define /etc/libvirt/qemu/test.xml

virsh start test

添加网卡

virsh shutdown test

virsh edit test

--- test.xml ---
    <interface type='bridge'>
      <mac address='52:54:00:1c:b3:36'/>
      <source bridge='br0'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>

    <interface type='bridge'>   # bridge
      <source bridge='br0'/>
      <model type='virtio'/>
    </interface>

    <interface type='network'>  # nat
      <source network='default'/>
      <model type='virtio'/>
    </interface>
--- test.xml ---

virsh define /etc/libvirt/qemu/test.xml

virsh start test

克隆虚拟机

## virt-clone 方式
virt-clone -o test -n test-clone -f /opt/img/test-clone.img

## 直接复制配置文件及镜像文件
cp -af test.xml test-clone.xml
cp -af test.img test-clone.img

--- test-clone.xml ---
删除uuid、网卡mac地址、修改vnc端口、修改disk源文件等
--- test-clone.xml ---

virsh define test-clone.xml
virsh start test-clone

虚拟机快照

快照配置文件  /var/lib/libvirt/qemu/snapshot/虚拟机名/快照名.xml

virsh snapshot-list test
virsh snapshot-current test # 最近的快照
virsh snapshot-dumpxml test snap1
virsh snapshot-info test snap1
virsh snapshot-create test
virsh snapshot-create-as test snap1 # 自定义快照名
virsh snapshot-delete test snap1
virsh snapshot-edit test snap1
virsh snapshot-revert test snap1    # 恢复快照,须在关机状态下!

虚拟机迁移

## nfs共享存储 - 动态迁移
node1:  192.168.255.101 (src)
node2:  192.168.255.102 (dst)
nfs_share:  192.168.255.103 # 共享/opt/img目录

node1:  mount -t nfs 192.168.255.103:/opt/img /opt/img
node2:  mount -t nfs 192.168.255.103:/opt/img /opt/img

node1:  virsh migrate --live --unsafe --verbose test qemu+ssh://root@192.168.255.102/system
node2:  virsh dumpxml test > /etc/libvirt/qemu/test.xml
        virsh define /etc/libvirt/qemu/test.xml