一.kvm虚拟机的完整克隆
1.完整克隆概述
所谓的完整克隆就是将模板机磁盘完整的拷贝一份。
完整克隆的优缺点:
优点:
模板机损坏并不会影响克隆机器环境。
缺点:
- 克隆占用空间较大;
- 克隆时间较长;
2.使用virt-clone工具进行克隆(推荐)
1.关闭虚拟机
[root@yinzhengjie-kvm189 ~]# virsh list --all
Id Name State
----------------------------------------------------
4 yinzhengjie-kvm01 running
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh destroy yinzhengjie-kvm01
Domain yinzhengjie-kvm01 destroyed
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh list --all
Id Name State
----------------------------------------------------
- yinzhengjie-kvm01 shut off
[root@yinzhengjie-kvm189 ~]#
2.克隆前查看KVM虚拟机磁盘快照信息
[root@yinzhengjie-kvm189 ~]# virsh snapshot-list yinzhengjie-kvm01
Name Creation Time State
------------------------------------------------------------
yinzhengjie_kvm_os_installd 2024-04-08 05:05:18 +0800 running
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# qemu-img info /opt/yinzhengjie-kvm01.qcow2
image: /opt/yinzhengjie-kvm01.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 1.0G
cluster_size: 65536
Snapshot list:
ID TAG VM SIZE DATE VM CLOCK
2 yinzhengjie_kvm_os_installd 282M 2024-04-08 05:05:18 00:15:26.803
Format specific information:
compat: 1.1
lazy refcounts: false
[root@yinzhengjie-kvm189 ~]#
3.使用virt-clone工具进行克隆
[root@yinzhengjie-kvm189 ~]# virsh list --all # 克隆前查看状态
Id Name State
----------------------------------------------------
- yinzhengjie-kvm01 shut off
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virt-clone -o yinzhengjie-kvm01 -n yinzhengjie-kvm02 --auto-clone
Allocating 'yinzhengjie-kvm02.qcow2' | 10 GB 00:00:15
Clone 'yinzhengjie-kvm02' created successfully.
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh list --all # 克隆后查看状态
Id Name State
----------------------------------------------------
- yinzhengjie-kvm01 shut off
- yinzhengjie-kvm02 shut off
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh start yinzhengjie-kvm02 # 启动虚拟机
Domain yinzhengjie-kvm02 started
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh list --all
Id Name State
----------------------------------------------------
6 yinzhengjie-kvm02 running
- yinzhengjie-kvm01 shut off
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# qemu-img info /opt/yinzhengjie-kvm02.qcow2 # 克隆后的磁盘默认的存储路径和模板机磁盘路径相同。
image: /opt/yinzhengjie-kvm02.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 3.0G
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: true
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# ll -h /opt/yinzhengjie-kvm02.qcow2 /opt/yinzhengjie-kvm01.qcow2
-rw-r--r--. 1 root root 1.4G Apr 8 17:29 /opt/yinzhengjie-kvm01.qcow2
-rw-------. 1 root root 1.6G Apr 8 17:36 /opt/yinzhengjie-kvm02.qcow2
[root@yinzhengjie-kvm189 ~]#
温馨提示:
- 克隆的虚拟机并不会克隆模板机的快照信息;
- 默认克隆的虚拟机磁盘和模板机在同一个目录,如果想要指定克隆机器的磁盘文件到指定路径,可以做如下操作。
[root@yinzhengjie-kvm189 ~]# virt-clone -o yinzhengjie-kvm01 -n yinzhengjie05 --file /tmp/yinzhengjie-kvm05.qcow3
Allocating 'yinzhengjie-kvm05.qcow3' | 10 GB 00:00:13
Clone 'yinzhengjie05' created successfully.
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# ll -h /tmp/yinzhengjie-kvm05.qcow3
-rw-------. 1 root root 1.6G Apr 8 18:06 /tmp/yinzhengjie-kvm05.qcow3
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh list --all
Id Name State
----------------------------------------------------
5 yinzhengjie-kvm03 running
6 yinzhengjie-kvm02 running
- yinzhengjie-kvm01 shut off
- yinzhengjie05 shut off
[root@yinzhengjie-kvm189 ~]#
3.手动克隆虚拟机(比较繁琐)
1.先克隆模板机的磁盘文件("qemu-img convert"工具将一块qcow2格式磁盘转换为另一块qcow2格式时会自动抹除快照信息)
[root@yinzhengjie-kvm189 ~]# qemu-img info /opt/yinzhengjie-kvm01.qcow2
image: /opt/yinzhengjie-kvm01.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 1.0G
cluster_size: 65536
Snapshot list:
ID TAG VM SIZE DATE VM CLOCK
2 yinzhengjie_kvm_os_installd 282M 2024-04-08 05:05:18 00:15:26.803
Format specific information:
compat: 1.1
lazy refcounts: false
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# qemu-img convert -f qcow2 -O qcow2 /opt/yinzhengjie-kvm01.qcow2 /opt/yinzhengjie-kvm03.qcow2
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# qemu-img info /opt/yinzhengjie-kvm03.qcow2
image: /opt/yinzhengjie-kvm03.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 3.0G
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# ll -h /opt/yinzhengjie-kvm03.qcow2 /opt/yinzhengjie-kvm01.qcow2
-rw-r--r--. 1 root root 1.4G Apr 8 17:29 /opt/yinzhengjie-kvm01.qcow2
-rw-r--r--. 1 root root 1.6G Apr 8 17:42 /opt/yinzhengjie-kvm03.qcow2
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]#
2.为虚拟机生成配置文件并修改4处位置
[root@yinzhengjie-kvm189 ~]# virsh dumpxml yinzhengjie-kvm01 > /mnt/vm_yinzhengjie_kvm03.xml
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# vim /mnt/vm_yinzhengjie_kvm03.xml
...
<domain type='kvm'>
... (1.修改虚拟机的名称,虚拟机的名字要唯一)
<name>yinzhengjie-kvm03</name>
... (2.删除uuid,因为每个镜像的磁盘UUID不一致,我们直接删除"<uuid></uuid>"标签即可,启动虚拟机时会自动生成。)
<devices>
...
<disk type='file' device='disk'>
... (3.修改磁盘文件路径)
<source file='/opt/yinzhengjie-kvm03.qcow2'/>
...
</disk>
...
<interface type='network'>
... (4.删除"<mac>"标签,其会自动生成MAC地址,如果mac地址冲突,则虚拟机可能无法联网。)
</interface>
...
3.导入虚拟机配置文件并启动虚拟机测试
[root@yinzhengjie-kvm189 ~]# virsh list --all
Id Name State
----------------------------------------------------
- yinzhengjie-kvm01 shut off
- yinzhengjie-kvm02 shut off
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh define /mnt/vm_yinzhengjie_kvm03.xml # 导入虚拟机配置文件
Domain yinzhengjie-kvm03 defined from /mnt/vm_yinzhengjie_kvm03.xml
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh list --all
Id Name State
----------------------------------------------------
- yinzhengjie-kvm01 shut off
- yinzhengjie-kvm02 shut off
- yinzhengjie-kvm03 shut off
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh start yinzhengjie-kvm03 # 启动虚拟机
Domain yinzhengjie-kvm03 started
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh list --all
Id Name State
----------------------------------------------------
5 yinzhengjie-kvm03 running
- yinzhengjie-kvm01 shut off
- yinzhengjie-kvm02 shut off
[root@yinzhengjie-kvm189 ~]#
二.kvm虚拟机的链接克隆
1.链接克隆概述
所谓的链接克隆就是将模板机以模板机为模板进行克隆机器,并不会完整拷贝模板机。
链接克隆是对模板机磁盘进行引用,新克隆的机器仅保留其变化的内容。
完整克隆的优缺点:
优点:
- 克隆占用时间少;
- 占用空间少;
缺点:
- 模板机不能开机,若模板机器损坏,所有链接克隆的虚拟机都打不开了。
2.链接克隆测试
1.移除完整克隆的测试环境
[root@yinzhengjie-kvm189 ~]# virsh list --all
Id Name State
----------------------------------------------------
5 yinzhengjie-kvm03 running
6 yinzhengjie-kvm02 running
- yinzhengjie-kvm01 shut off
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh destroy yinzhengjie-kvm02
Domain yinzhengjie-kvm02 destroyed
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh destroy yinzhengjie-kvm03
Domain yinzhengjie-kvm03 destroyed
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh undefine yinzhengjie-kvm02
Domain yinzhengjie-kvm02 has been undefined
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh undefine yinzhengjie-kvm03
Domain yinzhengjie-kvm03 has been undefined
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh list --all
Id Name State
----------------------------------------------------
- yinzhengjie-kvm01 shut off
[root@yinzhengjie-kvm189 ~]#
2.移除模板机的快照信息
[root@yinzhengjie-kvm189 ~]# virsh snapshot-list yinzhengjie-kvm01
Name Creation Time State
------------------------------------------------------------
yinzhengjie_kvm_os_installd 2024-04-08 05:05:18 +0800 running
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh snapshot-delete yinzhengjie-kvm01 --snapshotname yinzhengjie_kvm_os_installd
Domain snapshot yinzhengjie_kvm_os_installd deleted
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh snapshot-list yinzhengjie-kvm01
Name Creation Time State
------------------------------------------------------------
[root@yinzhengjie-kvm189 ~]#
3.创建引用磁盘,基于yinzhengjie-kvm01磁盘作为模板机
[root@yinzhengjie-kvm189 ~]# qemu-img create -f qcow2 -b /opt/yinzhengjie-kvm01.qcow2 /mnt/yinzhengjie-kvm04.qcow2
Formatting '/mnt/yinzhengjie-kvm04.qcow2', fmt=qcow2 size=10737418240 backing_file='/opt/yinzhengjie-kvm01.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# qemu-img info /mnt/yinzhengjie-kvm04.qcow2
image: /mnt/yinzhengjie-kvm04.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes) # 磁盘的上限容量大小
disk size: 196K # 占用的空间大小
cluster_size: 65536
backing file: /opt/yinzhengjie-kvm01.qcow2 # 后端的磁盘设备
Format specific information:
compat: 1.1
lazy refcounts: false
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# ll -h /mnt/yinzhengjie-kvm04.qcow2 /opt/yinzhengjie-kvm01.qcow2
-rw-r--r--. 1 root root 193K Apr 8 18:18 /mnt/yinzhengjie-kvm04.qcow2 # 但是链接克隆的磁盘文件却很小哟~
-rw-r--r--. 1 root root 1.4G Apr 8 18:25 /opt/yinzhengjie-kvm01.qcow2 # 模板机的磁盘文件很大
[root@yinzhengjie-kvm189 ~]#
4.为虚拟机生成配置文件并修改4处位置
[root@yinzhengjie-kvm189 ~]# virsh list --all
Id Name State
----------------------------------------------------
- yinzhengjie-kvm01 shut off
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh dumpxml yinzhengjie-kvm01 > /mnt/vm_yinzhengjie_k8s231.xml
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# vim /mnt/vm_yinzhengjie_k8s231.xml
...
<domain type='kvm'>
... (1.修改虚拟机的名称,虚拟机的名字要唯一)
<name>yinzhengjie_k8s231</name>
... (2.删除uuid,因为每个镜像的磁盘UUID不一致,我们直接删除"<uuid></uuid>"标签即可,启动虚拟机时会自动生成。)
<devices>
...
<disk type='file' device='disk'>
... (3.修改磁盘文件路径)
<source file='/mnt/yinzhengjie-kvm04.qcow2'/>
...
</disk>
...
<interface type='network'>
... (4.删除"<mac>"标签,其会自动生成MAC地址,如果mac地址冲突,则虚拟机可能无法联网。)
</interface>
...
5.移除模板机引用(避免误操作启动了模板机,我们可以将其从KVM虚拟机列表移除)并未磁盘文件添加特殊权限
[root@yinzhengjie-kvm189 ~]# virsh list --all
Id Name State
----------------------------------------------------
- yinzhengjie-kvm01 shut off
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh undefine yinzhengjie-kvm01
Domain yinzhengjie-kvm01 has been undefined
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh list --all
Id Name State
----------------------------------------------------
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# chattr +i /opt/yinzhengjie-kvm01.qcow2 # 添加特殊权限
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# rm -f /opt/yinzhengjie-kvm01.qcow2 # 发现该文件无法被删除啦~
rm: cannot remove ‘/opt/yinzhengjie-kvm01.qcow2’: Operation not permitted
[root@yinzhengjie-kvm189 ~]#
6.导入克隆的虚拟机配置文件
[root@yinzhengjie-kvm189 ~]# virsh define /mnt/vm_yinzhengjie_k8s231.xml
Domain yinzhengjie-k8s231 defined from /mnt/vm_yinzhengjie_k8s231.xml
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh list --all
Id Name State
----------------------------------------------------
- yinzhengjie-k8s231 shut off
[root@yinzhengjie-kvm189 ~]#
7.启动虚拟机
[root@yinzhengjie-kvm189 ~]# virsh list --all
Id Name State
----------------------------------------------------
- yinzhengjie-k8s231 shut off
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh start yinzhengjie-k8s231
Domain yinzhengjie-k8s231 started
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh list --all
Id Name State
----------------------------------------------------
8 yinzhengjie-k8s231 running
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh vncdisplay yinzhengjie-k8s231
:0
[root@yinzhengjie-kvm189 ~]#
三.全自动链接克隆脚本
1.编写克隆脚本
[root@yinzhengjie-kvm189 ~]# cat yinzhengjie-clone_qcow2_VM.sh
#!/bin/bash
# auther: Jason Yin
# blog: https://www.cnblogs.com/yinzhengjie
# email: y1053419035@qq.com
# 指定你的qcow2的磁盘存放路径
BASEDIR=/opt
# 指定主机名称
HOSTNAME=`hostname`
function main(){
case $1 in
clone|c)
clone_vm $2 $3
echo "尹正杰编写脚本: $2 虚拟机克隆成功[successfully]"
;;
delete|d)
rm_vm $2
echo "尹正杰编写脚本: $2 虚拟机删除成功"
;;
*)
echo "Usage: $0 deploy[i]|delete[r] oldVmName [newVmName]"
;;
esac
}
function rm_vm() {
# 定义虚拟机的名称
vmName=$1
# 先停止虚拟机
virsh destroy $vmName &> /dev/null
[ $? -eq 0 ] && echo "${vmName} KVM虚拟机停止成功" || echo "${vmName} KVM虚拟机不存在哟~"
# 移除虚拟机及其配置文件
virsh undefine $vmName
# 清除磁盘文件
rm -f ${BASEDIR}/${vmName}.qcow2
}
function clone_vm() {
# 定义老的虚拟机名称
oldVmName=$1
# 定义旧的虚拟机名称
newVmName=$2
# 生成虚拟机磁盘文件
old_disk=`virsh dumpxml $oldVmName | grep "<source file" | awk -F "'" 'NR==1{print $2}'`
disk_tmp=`dirname $old_disk`
qemu-img create -f qcow2 -b ${old_disk} ${disk_tmp}/${newVmName}.qcow2
# 生成虚拟机的配置文件
virsh dumpxml $oldVmName > /tmp/${newVmName}.xml
# 修改虚拟机的名称
sed -ri "s#(<name>)(.*)(</name>)#\1${newVmName}\3#g" /tmp/${newVmName}.xml
# 删除虚拟机的UUID
sed -i "/<uuid>/d" /tmp/${newVmName}.xml
# 删除mac地址
sed -i '/<mac address/d' /tmp/${newVmName}.xml
# 修改磁盘路径
sed -ri "s#(<source file=')(.*)(.qcow2'/>)#\1${disk_tmp}/${newVmName}\3#" /tmp/${newVmName}.xml
# 导入虚拟机并进行启动测试
virsh define /tmp/${newVmName}.xml
virsh start $newVmName
}
main $1 $2 $3
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# chmod +x yinzhengjie-clone_qcow2_VM.sh
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# ll yinzhengjie-clone_qcow2_VM.sh
-rwxr-xr-x. 1 root root 1898 Apr 8 20:11 yinzhengjie-clone_qcow2_VM.sh
[root@yinzhengjie-kvm189 ~]#
2.创建测试虚拟机
virt-install --virt-type kvm \
--os-type=linux \
--os-variant rhel7 \
--name yinzhengjie-k8s \
--memory 2048 \
--vcpus 2 \
--disk /opt/yinzhengjie-k8s.qcow2,format=qcow2,size=10 \
--cdrom /opt/CentOS-7-x86_64-DVD-2009.iso \
--network network=default \
--graphics vnc,listen=0.0.0.0 \
--noautoconsole
3.克隆KVM虚拟机测试
[root@yinzhengjie-kvm189 ~]# virsh list --all
Id Name State
----------------------------------------------------
37 yinzhengjie-k8s running
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# ./yinzhengjie-clone_qcow2_VM.sh c yinzhengjie-k8s master231
Formatting '/opt/master231.qcow2', fmt=qcow2 size=10737418240 backing_file='/opt/yinzhengjie-k8s.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off
Domain master231 defined from /tmp/master231.xml
Domain master231 started
尹正杰编写脚本: yinzhengjie-k8s 虚拟机克隆成功[successfully]
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# ./yinzhengjie-clone_qcow2_VM.sh c yinzhengjie-k8s worker232
Formatting '/opt/worker232.qcow2', fmt=qcow2 size=10737418240 backing_file='/opt/yinzhengjie-k8s.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off
Domain worker232 defined from /tmp/worker232.xml
Domain worker232 started
尹正杰编写脚本: yinzhengjie-k8s 虚拟机克隆成功[successfully]
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh list --all
Id Name State
----------------------------------------------------
37 yinzhengjie-k8s running
38 master231 running
39 worker232 running
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# ll /opt/ | egrep "231|232" # 会创建对应的磁盘文件哟~
-rw-r--r--. 1 qemu qemu 197120 Apr 8 20:18 master231.qcow2
-rw-r--r--. 1 qemu qemu 197120 Apr 8 20:18 worker232.qcow2
[root@yinzhengjie-kvm189 ~]#
4.删除KVM虚拟机测试
[root@yinzhengjie-kvm189 ~]# virsh list --all
Id Name State
----------------------------------------------------
37 yinzhengjie-k8s running
38 master231 running
39 worker232 running
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# ./yinzhengjie-clone_qcow2_VM.sh d master231
master231 KVM虚拟机停止成功
Domain master231 has been undefined
尹正杰编写脚本: master231 虚拟机删除成功
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# ./yinzhengjie-clone_qcow2_VM.sh d worker232
worker232 KVM虚拟机停止成功
Domain worker232 has been undefined
尹正杰编写脚本: worker232 虚拟机删除成功
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh list --all
Id Name State
----------------------------------------------------
37 yinzhengjie-k8s running
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# ll /opt/ | egrep "231|232" # Duang~磁盘文件被删除啦!
[root@yinzhengjie-kvm189 ~]#
5.脚本后续优化思路
本脚本使用临时写了一个样例脚本,以供大家学习环境测试使用。
生成环境如果要使用需要做如下优化:
- 脚本是否需要删除磁盘文件?
- 是否支持raw格式KVM虚拟机的克隆和删除呢?
- 虚拟机的磁盘存储路径你的生产环境也在/opt目录吗?