目录

Life in Flow

知不知,尚矣;不知知,病矣。
不知不知,殆矣。

X

KVM

虚拟化软件的差别

虚拟化软件(全): qemu 软件纯模拟全虚拟化软件,特别慢!AIX,兼容性好!

xen(半) :性能特别好,需要使用专门修改之后的内核,兼容性差! redhat 5.5 xen

kvm KVM(全): 全虚拟机,它有硬件支持cpu,基于内核,而且不需要使用专门的内核 centos6 kvm 性能较好,兼容较好。

KVM系统需求

  • 1CPU核心
  • 2G内存
  • 6G硬盘
# 查看系统版本
[root@openstack ~]# cat /etc/centos-release

# 首先验证CPU是否支持虚拟化,输入有vmx或svm就支持,支持虚拟化则就支持KVM
[root@openstack ~]# cat /proc/cpuinfo | egrep 'vmx|svm'

关闭防火墙和SElinux

# 关闭selinux
[root@openstack ~]# systemctl stop firewalld.service          #停止firewall
[root@openstack ~]# systemctl disable firewalld.service    #禁止firewall开机启动

# 关闭selinux
[root@openstack ~]# setenforce 0
[root@openstack ~]# vim /etc/sysconfig/selinux 
SELINUX=disabled

安装KVM

# 查看是否加载KVM :这表明已经加载
[root@openstack ~]# lsmod | grep kvm
kvm_intel             170086  0 
kvm                   566340  1 kvm_intel
irqbypass              13503  1 kvm

# 如果没有加载则执行以下命令加载KVM
[root@openstack ~]# modprobe kvm

# 安装KVM相关软件包
## 网络参考
[root@openstack ~]# yum install qemu-kvm qemu-img virt-manager libvirt libvirt-python virt-manager libvirt-client virt-install virt-viewer -y
qemu-kvm: KVM模块
libvirt: 虚拟管理模块
virt-manager: 图形界面管理虚拟机
virt-install: 虚拟机命令行安装工具

## 真实实验
[root@localhost yum.repos.d]# yum install libvirt virt-install qemu-kvm -y

# 启动libvirt并设置开机自启动
[root@openstack ~]# systemctl start libvirtd
[root@openstack ~]# systemctl enable libvirtd
[root@localhost software]# systemctl status libvirtd.service
   Active: active (running) 

# 远程桌面管理软件
VNC-Viewer-6.19.325-Windows-64bit

# 文件传输软件
WinSCP_5.9.0.6786_Setup

安装一台KVM 虚拟机

[root@localhost software]# virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name centos7 --memory 1024 --vcpus 1 --disk /vmdisk/centos2.raw,format=raw,size=10 --cdrom /software/CentOS-7.6-x86_64-DVD-1810.iso --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole
参数作用
--virt-type kvm虚拟化类型,默认是qemu
--os-type=linux系统类型
--os-variant rhel7系统版本
--name centos7虚拟机的名字
--memory 1024虚拟机的内存
--vcpus 1虚拟机cpu的核数
--disk /opt/centos2.raw,format=raw,size=10虚拟机硬盘路径
--cdrom /opt/CentOS-7-x86_64-DVD-1708.iso
--network network=default使用默认NAT的网络
--graphics vnc,listen=0.0.0.0将控制台通过VNC输出(服务端),使用VNC客户端
--noautoconsole

查看虚拟机监听的端口

[root@localhost /]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name  
tcp        0      0 0.0.0.0:5900            0.0.0.0:*               LISTEN      33359/qemu-kvm  
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      22483/dnsmasq   
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      9396/sshd   
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      9832/master   
tcp6       0      0 :::22                   :::*                    LISTEN      9396/sshd   
tcp6       0      0 ::1:25                  :::*                    LISTEN      9832/master 

virsh日常操作

# 查看运行状态的虚拟机
[root@localhost /]# virsh list

# 查看所有的虚拟机
[root@localhost /]# virsh list --all

# 启动虚拟机
[root@localhost /]# virsh start centos7

# 正常关机
[root@localhost /]# virsh shutdown centos7

# 强制关机(尽可能不用)
[root@localhost /]# virsh destory centos7

# 重启
[root@localhost /]# virsh reboot centos7

# 导出虚拟机的配置文件
[root@localhost vmdisk]# virsh dumpxml centos7 > /vmdisk/centos7.xml

# 导入配置文件
[root@localhost vmdisk]# virsh define cento7.xml

# 删除虚拟机的配置文件(仅仅是删除虚拟机的配置文件)
[root@localhost vmdisk]# virsh undefine centos7

# 导入虚拟机配置文件
[root@localhost vmdisk]# virsh define centos7.xml

# 虚拟机配置文件默认路径在 /etc/libvirt/qemu 目录下
[root@localhost vmdisk]# ls /etc/libvirt/qemu
centos7.xml  networks

# 修改虚拟机配置文件
[root@localhost vmdisk]# virsh edit centos7

# 查看运行的虚拟机的磁盘路径
[root@localhost vmdisk]# virsh dumpxml centos7 | grep raw
      <driver name='qemu' type='raw'/>
      <source file='/vmdisk/centos2.raw'/>

# 修改虚拟机的名字(在关机的状态下重命名)
[root@localhost vmdisk]# virsh domrename centos7 centos7_one
Domain successfully renamed

# 挂起虚拟机
[root@localhost vmdisk]# virsh suspend centos7

# 恢复虚拟机
[root@localhost vmdisk]# virsh resume centos7

# 查看VNC端口号  直接连接192.168.31.240:0 或 192.168.31.240:5900
[root@localhost vmdisk]# virsh vncdisplay centos7_one
:0

# kvm虚拟机开机启动(libvirtd服务器启动的时候虚拟机就会跟着启动) systemctl restart libvirtd
[root@localhost vmdisk]# virsh autostart centos7_one

# 查看哪条虚拟机为开机自启动(设置开机自启动的虚拟机会有对应的xml文件,其实就是创建centos7.xml的软连接文件)
[root@localhost vmdisk]# ls /etc/libvirt/qemu/autostart/

# 禁止开机自启动
[root@localhost vmdisk]# virsh autostart --disable centos7_one

# 修改KVM虚拟机的内核参数配置参数
[root@localhost ~]# ssh root@192.168.122.186	#连接KVM虚拟机
[root@localhost ~]# grubby --update-kernel=ALL --args="console=ttyS0,115200n8"
[root@localhost ~]# reboot

# 控制台 (可以取代VNC)
[root@localhost vmdisk]# virsh console centos7_one
[root@localhost vmdisk]# enter #回车
[root@kvm1 ~]# lscpu | grep -i kvm
Hypervisor vendor:     KVM
[root@localhost vmdisk]# logout
[root@localhost vmdisk]# ctrl + ]

磁盘管理

磁盘格式功能性
raw裸格式,占用空间比较大,不支持快照功能,性能较好,不方便传输
qcow2cow(copyon write)占用空间小,支持快照,性能比raw差一点,方便传输

常用命令

# 查看磁盘文件信息
[root@localhost ~]# qemu-img info /vmdisk/centos2.raw 
image: /vmdisk/centos2.raw
file format: raw
virtual size: 10G (10737418240 bytes)
disk size: 1.3G

# 创建磁盘
[root@localhost ~]# qemu-img create -f qcow2 /vmdisk/oldboy.qcow2 1G
Formatting '/vmdisk/oldboy.qcow2', fmt=qcow2 size=1073741824 encryption=off cluster_size=65536 lazy_refcounts=off 

# 查看磁盘大小
[root@localhost vmdisk]# ll -h
total 1.3G
-rw-------. 1 qemu qemu  10G Apr 30 10:03 centos2.raw
-rw-r--r--. 1 root root 5.4K Apr 30 07:56 centos7.xml
-rw-r--r--. 1 root root 193K Apr 30 10:05 oldboy.qcow2

# 调整虚拟磁盘的容量
[root@localhost vmdisk]# qemu-img resize /vmdisk/oldboy.qcow2 1T
Image resized.
[root@localhost vmdisk]# qemu-img info /vmdisk/oldboy.qcow2 
image: /vmdisk/oldboy.qcow2
file format: qcow2
virtual size: 1.0T (1099511627776 bytes)
disk size: 212K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false

# raw格式的虚拟磁盘支持增减(缩容会导致数据丢失,生产环境中千万别用),扩容可以用。
[root@localhost vmdisk]# qemu-img create /vmdisk/oldboy.raw 5G
[root@localhost vmdisk]# qemu-img resize /vmdisk/oldboy.raw -2G
Image resized.
[root@localhost vmdisk]# qemu-img info /vmdisk/oldboy.raw 
image: /vmdisk/oldboy.raw
file format: raw
virtual size: 3.0G (3221225472 bytes)
disk size: 0
[root@localhost vmdisk]# qemu-img resize /vmdisk/oldboy.raw +2G

# 磁盘格式转换:将raw格式的磁盘转换为qcow2格式 (虚拟机处于关机状态)
[root@localhost vmdisk]# qemu-img convert -f raw -O qcow2 /vmdisk/oldboy.raw /vmdisk/oldboy2.qcow2
修改虚拟机配置文件中的2行
      <driver name='qemu' type='qcow2'/>
      <source file='/vmdisk/centos2.qcow2'/>

快照管理

# 创建快照
[root@localhost vmdisk]# virsh snapshot-create centos7_one
Domain snapshot 1619750229 created

# 查看指定虚拟机的快照列表    1619750229是时间戳
[root@localhost vmdisk]# virsh snapshot-list centos7_one
 Name                 Creation Time             State
------------------------------------------------------------
 1619750229           2021-04-30 10:37:09 +0800 running

# 删除快照
[root@localhost vmdisk]# virsh snapshot-delete centos7_one --snapshotname 1619750229
Domain snapshot 1619750229 deleted

# 创建快照指定名字 env_ok
[root@localhost vmdisk]# virsh snapshot-create-as --name env_ok centos7_one
Domain snapshot env_ok created

# 还原快照  (系统时间也会随着快照的还原而还原,一定要加定时任务同步时间)
[root@localhost vmdisk]# virsh snapshot-revert centos7_one --snapshotname env_ok

克隆

  • 完整克隆
  • 链接克隆

完整克隆

自动克隆

# 完成克隆,基于centos7_one 克隆 backup
[root@localhost ~]# virt-clone --auto-clone -o centos7_one -n backup

# 查看克隆之后磁盘的位置
[root@localhost ~]# virsh dumpxml backup | grep -i 'qcow2'
      <driver name='qemu' type='qcow2'/>
      <source file='/vmdisk/centos2-clone.qcow2'/>

# 虚拟机的配置文件在这里
[root@localhost ~]# ls /etc/libvirt/qemu/backup.xml 

手动克隆

# 克隆磁盘文件
[root@localhost vmdisk]# cp centos2-clone.qcow2  backup02.qcow2

# 导出之前backup 的虚拟机配置文件,命名为backup02.xml
[root@localhost vmdisk]# virsh dumpxml backup > /vmdisk/backup02.xml

# 修改虚拟机配置文件
[root@localhost vmdisk]# vim /vmdisk/backup02.xml
<name>backup02</name>
<uuid>e3064380-2878-4451-b0b5-de82886ef214</uuid>  # 删除UUID这一行
<source file='/vmdisk/backup02.qcow2'/>
<mac address='52:54:00:9b:a5:0f'/>  # 删除mac地址这一行

# 导入虚拟机配置文件 backup02.xml
[root@localhost vmdisk]# virsh define /vmdisk/backup02.xml
Domain backup02 defined from /vmdisk/backup02.xml

# 查看当前虚拟机
[root@localhost vmdisk]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     backup                         shut off
 -     backup02                       shut off
 -     centos7_one                    shut off

# 启动backup02虚拟机
[root@localhost vmdisk]# virsh start backup02
Domain backup02 started

链接克隆

只有手动克隆,这里模板机为centos7_one,对应的磁盘文件是centos2.qcow2

创建出来的链接磁盘必须是qcow2格式,但是模板磁盘可以是raw、qcow2都行

# 创建链接克隆的磁盘
[root@localhost vmdisk]# qemu-img create -f qcow2 -b /vmdisk/centos2.qcow2 /vmdisk/backup03.qcow2
Formatting '/vmdisk/backup03.qcow2', fmt=qcow2 size=10737418240 backing_file='/vmdisk/centos2.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off 

# 查看链接磁盘文件
[root@localhost vmdisk]# qemu-img info /vmdisk/backup03.qcow2 
image: /vmdisk/backup03.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 196K
cluster_size: 65536
backing file: /vmdisk/centos2.qcow2	# 基于此文件进行链接克隆,保存差异数据
Format specific information:
    compat: 1.1
    lazy refcounts: false

# 导出centos7_one的虚拟机配置文件
[root@localhost vmdisk]# virsh dumpxml centos7_one > /vmdisk/backup03.xml

# 修改backup03.xml虚拟机配置文件
[root@localhost vmdisk]# vim /vmdisk/backup03.xml 
  <name>backup03</name>
  <uuid>a4d33da7-73b9-4fe7-97f4-f6c50faf6d7c</uuid>  # 删除
  <source file='/vmdisk/backup03.qcow2'/>
  <mac address='52:54:00:68:2c:6e'/>	# 删除

# 导入backup03.xml 虚拟机配置文件
[root@localhost vmdisk]# virsh define /vmdisk/backup03.xml 
Domain backup03 defined from /vmdisk/backup03.xml

# 启动backup03虚拟机
[root@localhost vmdisk]# virsh start backup03
Domain backup03 started


# 查看虚拟机列表
[root@localhost vmdisk]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 5     backup02                       running
 6     backup03                       running
 -     backup                         shut off
 -     centos7_one                    shut off

# 查看VNC端口号
[root@localhost vmdisk]# virsh vncdisplay backup03
:1

全自动链接克隆脚本

link_clone.sh脚本内容如下

[root@localhost vmdisk]# cat link_clone.sh 
#!/bin/bash 
old_vm=$1 
new_vm=$2 
#a:生成虚拟机磁盘文件 
old_disk=`virsh dumpxml $old_vm|grep "<source file"|awk -F"'" '{print $2}'` 
disk_tmp=`dirname $old_disk` 
qemu-img create -f qcow2 -b $old_disk ${disk_tmp}/${new_vm}.qcow2 

#b:生成虚拟机的配置文件 
virsh dumpxml $old_vm >/tmp/${new_vm}.xml 
#修改虚拟机的名字 
sed -ri "s#(<name>)(.*)(</name>)#\1${new_vm}\3#g" /tmp/${new_vm}.xml 
#删除虚拟机uuid 
sed -i '/<uuid>/d' /tmp/${new_vm}.xml 
#删除mac地址 
sed -i '/<mac address/d' /tmp/${new_vm}.xml 
#修改磁盘路径 
sed -ri "s#(<source file=')(.*)('/>)#\1${disk_tmp}/${new_vm}.qcow2\3#g" /tmp/${new_vm}.xml 

#c:导入虚拟机并进行启动测试 
virsh define /tmp/${new_vm}.xml 
virsh start ${new_vm}

执行脚本

[root@localhost vmdisk]# sh link_clone.sh centos7_one test
Formatting '/vmdisk/test.qcow2', fmt=qcow2 size=10737418240 backing_file='/vmdisk/centos2.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off 
Domain test defined from /tmp/test.xml

Domain test started

桥接模式

默认虚拟机网络是NAT模式,网络192.168.122.0/24

1.创建桥接网卡:基于物理网卡ens33创建 br0 网桥
[root@localhost vmdisk]# virsh iface-bridge ens33 br0
Created bridge br0 with attached device ens33

2.会报错断开连接,登录宿主机执行命令
systemctl restart network

3.可以连上检查网络已有桥接网络 br0
[root@localhost ~]# grep -i "." /etc/sysconfig/network-scripts/ifcfg-*

新虚拟机桥接模式

# 代表新虚拟机交接到br0这个网卡上:安装新的虚拟机
[root@localhost software]# virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name centos7 --memory 1024 --vcpus 1 --disk /vmdisk/centos2.raw,format=raw,size=10 --cdrom /software/CentOS-7.6-x86_64-DVD-1810.iso --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole

# 克隆新的虚拟机
# 创建连接磁盘文件
[root@localhost ~]# qemu-img create -f qcow2 -b /vmdisk/centos2.qcow2 /vmdisk/backup04.qcow2
Formatting '/vmdisk/backup04.qcow2', fmt=qcow2 size=10737418240 backing_file='/vmdisk/centos2.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off 

# 安装并从硬盘启动新的虚拟机
[root@localhost ~]# virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name backup04 --memory 1024 --vcpus 1 --disk /vmdisk/backup04.qcow2 --boot hd --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole

# 通过console 或 VNC连接虚拟机(console还没有设置,所以连接不上) 192.168.31.147/24 
[root@MiWiFi-R3P-srv ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:f1:62:b4 brd ff:ff:ff:ff:ff:ff
    inet 192.168.31.147/24 brd 192.168.31.255 scope global noprefixroute dynamic eth0
       valid_lft 42822sec preferred_lft 42822sec
    inet6 fe80::7d27:42de:ddcf:c118/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

老虚拟机从NAT模式改为桥接模式(修改虚拟机配置文件)

# 注意需要虚拟机关机状态下修改,重启虚拟机才会生效
[root@MiWiFi-R3P-srv ~]# virsh edit backup02
<interface type='network'>    ==>   <interface type='bridge'>
<source network='deafult'/>   ==>   <source bridge='br0'/>

# 启动backup02 虚拟机
[root@localhost ~]# virsh start backup02
[root@localhost ~]# virsh vncdisplay backup02
:0

# 登录vnc查看ip,并且使用ssh连接验证

# 登录之后可以自己设置IP地址,从DHCP模式设置为static模式
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"
IPADDR=192.168.31.180
NETMASK=255.255.255.0
GATEWAY=192.168.31.1
DNS1=192.168.31.1
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="eth0"
UUID="12468621-5a40-48c9-b17b-17d7645a9ea4"
DEVICE="eth0"
ONBOOT="yes"

NAT模式

机器网段
KVM宿主机br0 192.168.31.140/24 (外网:桥接模式)
virbr0 192.168.122.1/24(NAT模式,与虚拟机通信)
虚拟机192.168.122.187/24
192.168.122.1/24 (网关)

使用iptables让虚拟机实现上网

# 编辑内核配置文件,开启转发
[root@localhost ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1

# 使内核生效
[root@localhost ~]# sysctl -p

# 清空防火墙的filter表
[root@localhost ~]# iptables -F

# 添加转发规则
[root@localhost ~]# iptables -t nat -A POSTROUTING -s 192.168.122.0/24 -j MASQUERADE 

# 开启iptables
[root@localhost ~]# systemctl start firewalld

热添加硬盘

添加另外一块磁盘

# 创建磁盘文件
[root@localhost ~]# qemu-img create -f qcow2 /vmdisk/backup03-add.qcow2 20G
Formatting '/vmdisk/backup03-add.qcow2', fmt=qcow2 size=21474836480 encryption=off cluster_size=65536 lazy_refcounts=off 

# 查看当前虚拟机所有盘符
[root@localhost ~]# virsh dumpxml backup03 | grep target
      <target dev='vda' bus='virtio'/>

# 为虚拟机附加硬盘:设备名字叫vdb  (这种方式是错误的:添加完之后磁盘容量是0,因为默认是raw类型)
[root@localhost ~]# virsh attach-disk backup03 /vmdisk/backup03-add.qcow2 vdb
Disk attached successfully

# 卸载指定磁盘(临时)
[root@localhost ~]# virsh detach-disk backup03 vdb
Disk detached successfully

# 卸载指定磁盘(永久)
[root@localhost ~]# virsh detach-disk backup03 vdb --config
Disk detached successfully

# 为虚拟机附加硬盘,设备名字叫vdb(并且指定磁盘类型),临时添加,重启系统就会消失
[root@localhost ~]# virsh attach-disk backup03 /vmdisk/backup03-add.qcow2 vdb --subdriver qcow2
Disk attached successfully

# 为虚拟机附加硬盘,设备名字叫vdb(并且指定磁盘类型)
[root@localhost ~]# virsh attach-disk backup03 /vmdisk/backup03-add.qcow2 vdb --subdriver qcow2 --config

# 进入虚拟机查看:发现有 
[root@localhost ~]# fdisk -l
/dev/vdb :20GB

# 格式化
[root@localhost ~]# mkfs.xfs /dev/vdb

# 将/dev/vdb 挂载到/mnt 目录上
[root@localhost ~]# mount /dev/vdb /mnt

# 复制数据到mnt目录
[root@localhost ~]# cp /etc/services /mnt

# 准备扩容(扩容之前需要umount 掉挂载点)
[root@localhost ~]# umount /mnt

# 剥离vdb从虚拟机中
[root@localhost ~]# virsh detach-disk backup03 vdb
Disk detached successfully

# 扩容 +10G
[root@localhost ~]# qemu-img resize /vmdisk/backup03-add.qcow2 +10G
Image resized.

# 查看磁盘信息
[root@localhost ~]# qemu-img info /vmdisk/backup03-add.qcow2 
image: /vmdisk/backup03-add.qcow2
file format: qcow2
virtual size: 30G (32212254720 bytes)

# 再次附加磁盘
[root@localhost ~]# virsh attach-disk backup03 /vmdisk/backup03-add.qcow2 vdb --subdriver qcow2
Disk attached successfully

# 重新的mount 
[root@localhost ~]# mount /dev/vdb /mnt

# 查看/mnt目录下的数据是否丢失
[root@localhost ~]# ls /mnt
services

# 系统重新识别磁盘容量(更新磁盘分区信息)ex4文件系统使用 resize2fs /dev/vdb  更新分区信息
[root@localhost ~]# xfs_growfs /dev/vdb

# 再次查看挂载点容量 (发现变为30G)
[root@localhost ~]# df -h

热添加网卡

# 添加网卡 (临时添加,推荐使用)
[root@localhost ~]# virsh attach-interface backup03 --type bridge --source br0 --model virtio
Interface attached successfully

# 添加网卡 (永久添加,但是需要重启)
[root@localhost ~]# virsh attach-interface backup03 --type bridge --source br0 --model virtio --config
Interface attached successfully

# 移除网卡
[root@localhost ~]# virsh detach-interface backup03 --type bridge --mac 52:54:00:6a:bc:f9
Interface detached successfully

热添加内存

新虚拟机支持内存扩容

# 创建连接磁盘
[root@localhost ~]# qemu-img create -f qcow2 -b /vmdisk/centos2.qcow2 /vmdisk/backup05.qcow2
Formatting '/vmdisk/backup05.qcow2', fmt=qcow2 size=10737418240 backing_file='/vmdisk/centos2.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off

# 创建并启动新的虚拟机
[root@localhost ~]# virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name backup05 --memory 512,maxmemory=2048 --vcpus 1 --disk /vmdisk/backup05.qcow2 --boot hd --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole

# 查看当前内存
[root@MiWiFi-R3P-srv ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:            463          96         246           8         119         217
Swap:             0           0           0

# 热添加内存(永久操作需要 --config)
[root@localhost ~]# virsh setmem backup05 1024M

# 查看当前内存
[root@MiWiFi-R3P-srv ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:            975          96         759           8         119         729
Swap:             0           0           0

老虚拟机支持内存扩容(修改完虚拟机配置文件需要重启)

# 修改最大内存和当前内存
[root@localhost ~]# virsh edit backup04
<memory unit='KiB'>1048576</memory>	==>	<memory unit='MiB'>2048</memory>
<currentMemory unit='KiB'>1048576</currentMemory>	==>	<currentMemory unit='MiB'>1024</currentMemory>

# 重启虚拟机
[root@localhost ~]# virsh shutdown backup04
[root@localhost ~]# virsh start backup04

热添加CPU

# 创建连接磁盘
[root@localhost ~]# qemu-img create -f qcow2 -b /vmdisk/centos2.qcow2 /vmdisk/backup06.qcow2

# 创建并启动新的虚拟机
[root@localhost ~]# virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name backup06 --memory 512,maxmemory=2048 --vcpus 1,maxvcpus=10 --disk /vmdisk/backup06.qcow2 --boot hd --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole

# 查看CPU个数
[root@MiWiFi-R3P-srv ~]# lscpu | grep "CPU(s):"
CPU(s):                1
NUMA node0 CPU(s):     0

# 扩容CPU 为2个
[root@localhost ~]# virsh setvcpus backup06 2

# 查看CPU个数
[root@MiWiFi-R3P-srv ~]# lscpu | grep "CPU(s):"
CPU(s):                2
NUMA node0 CPU(s):     0,1

作者:Soulboy