1.完整克隆
#注意:只有关机状态才能克隆
[root@kvm-server-01 ~]# virt-clone --auto-clone -o centos7.6 -n centos7.6-v1
Allocating 'centos7.6-v1.qcow2' | 10 GB 00:00:50
Clone 'centos7.6-v1' created successfully.
[root@kvm-server-01 ~]# virsh list --all
Id Name State
----------------------------------------------------
- centos7.6 shut off
- centos7.6-v1 shut off
#查看克隆主机的镜像文件
[root@kvm-server-01 ~]# ll /opt/centos7.6-v1.qcow2
-rw------- 1 root root 1511194624 Jul 30 21:34 /opt/centos7.6-v1.qcow2
#查看克隆主机的配置文件
[root@kvm-server-01 ~]# ll /etc/libvirt/qemu/centos7.6-v1.xml
-rw------- 1 root root 4579 Jul 30 21:33 /etc/libvirt/qemu/centos7.6-v1.xml
2.创建链接克隆
#创建一个基于宿主机的磁盘
[root@kvm-server-01 opt]# qemu-img create -f qcow2 -b centos7.6.qcow2 centos7.6-2.qcow2
[root@kvm-server-01 opt]# ll -h centos7.6-2.qcow2
-rw-r--r-- 1 root root 193K Jul 30 22:41 centos7.6-2.qcow2
#导出宿主机的配置信息
[root@kvm-server-01 opt]# virsh dumpxml centos7.6 > centos7.6-2.xml
#修改centos7.6-2.xml配置
[root@kvm-server-01 opt]# vim centos7.6-2.xml
1.删除uuid和mac地址信息
2.修改centos7.6为centos7.6-2
<name>centos7.6-2</name>
<source file='/opt/centos7.6-2.qcow2'/>
#导入配置
[root@kvm-server-01 opt]# virsh define centos7.6-2.xml
Domain centos7.6-2 defined from centos7.6-2.xml
[root@kvm-server-01 opt]# virsh list --all
Id Name State
----------------------------------------------------
31 centos7.6-link-v1 running
- centos7.6 shut off
- centos7.6-2 shut off
#启动链接克隆
[root@kvm-server-01 opt]# virsh start centos7.6-2
[root@kvm-server-01 opt]# virsh list
Id Name State
----------------------------------------------------
31 centos7.6-link-v1 running
32 centos7.6-2 running
[root@kvm-server-01 opt]# cat link-clone.sh
#!/bin/bash
Old_Name=$1
New_Name=$2
#判断函数
judgment_fun(){
if [ $? -eq 0 ];then
echo -e "\033[32m${1}\033[0m"
else
echo -e "\033[31m${2}\033[0m"
exit
fi
}
#关闭宿主机
virsh list | grep "${Old_Name}[ ]*running"
if [ $? -eq 0 ];then
echo -e "\033[31m虚拟主机${Old_Name}正在运行,请关闭后再进行链接克隆!\033[0m"
exit
fi
#创建基于宿主机的硬盘
qemu-img create -f qcow2 -b ${Old_Name}.qcow2 ${New_Name}.qcow2 &>/dev/null
judgment_fun 链接克隆${New_Name}硬盘创建成功! 链接克隆${New_Name}硬盘创建失败!
#导出宿主机配置
virsh dumpxml $Old_Name > ${New_Name}.xml
judgment_fun 宿主机${Old_Name}配置导出成功! 宿主机${Old_Name}配置导出失败!
#修改导出配置的文件
sed -i "s@$Old_Name@$New_Name@g" ${New_Name}.xml
sed -ri '/uuid|mac address/d' ${New_Name}.xml
#导入配置,生成链接克隆
virsh define ${New_Name}.xml
judgment_fun 链接克隆${New_Name}创建成功! 链接克隆${New_Name}创建失败!
#启动链接克隆
virsh start ${New_Name}
judgment_fun 虚拟主机${New_Name}启动成功! 虚拟主机${New_Name}启动失败!
# 清除无效文件
rm -rf ${New_Name}.xml
[root@kvm-server-01 opt]# sh link-clone.sh centos7.6 centos7.6-link-v1
链接克隆centos7.6-link-v1硬盘创建成功!
宿主机centos7.6配置导出成功!
Domain centos7.6-link-v1 defined from centos7.6-link-v1.xml
链接克隆centos7.6-link-v1创建成功!
Domain centos7.6-link-v1 started
虚拟主机centos7.6-link-v1启动成功!
[root@kvm-server-01 opt]# virsh list
Id Name State
----------------------------------------------------
31 centos7.6-link-v1 running