克隆主要是对磁盘文件进行操作。
1)完整克隆
a、拷贝虚拟机磁盘文件
b、生成虚拟机配置文件
centos7-2
291b0480-955a-45e2-a001-690fded69d1b
[root@centos8 ~]# virt-clone -o centos7 --auto-clone
ERROR 要克隆的域必须已经关闭。
[root@centos8 ~]# virsh destroy centos7
Domain ‘centos7’ destroyed
[root@centos8 ~]# virt-clone -o centos7 --auto-clone
‘centos2-clone.qcow2’ 15% [== ] 102 MB/s | 1.5 GB 00:01:25 正在分配 ‘centos2-clone.qcow2’ | 10 GB 00:00:13
成功克隆 ‘centos7-clone’。
[root@centos8 ~]# ll /opt
总用量 9477500
-rw------- 1 root root 1633353728 2月 27 14:03 centos2-clone.qcow2 #比centos2少个快照,所以文件大小要比centos2小
-rw-r–r-- 1 root root 1887109120 2月 27 14:03 centos2.qcow2
[root@centos8 ~]# virsh list --all
Id 名称 状态
- centos7 关闭
- centos7-clone 关闭
修改虚拟机名称
[root@centos8 ~]# virsh domrename centos7-clone centos7-2
Domain successfully renamed
[root@centos8 ~]# virsh list --all
Id 名称 状态
- centos7 关闭
- centos7-2 关闭
将克隆的虚拟机跟原先的虚拟机xml文件导出来,查看其两者的区别在哪里?
[root@centos8 opt]# virsh dumpxml centos7 > centos7.xml
[root@centos8 opt]# virsh dumpxml centos7-3 > centos7-3.xml
[root@centos8 opt]# vimdiff centos7.xml centos7-3.xml
1、虚拟机name
2、uuid不同
3、source file的磁盘文件不同
4、虚拟机的mac地址不同
手动完整克隆虚拟机:
1、拷贝虚拟机的磁盘文件
cp centos7.qcow2 centos7-clone.qcow2
2、拷贝虚拟机xml配置文件
cp centos7.xml centos7-clone.xml
3、修改name、磁盘文件路径
4、将uuid删掉,下次启动虚拟机时,会自动生成新的uuid
5、将mac删掉,下次启动虚拟机时,会自动生成新的
6、导入虚拟机:virsh define centos7-clone.xml
7、启动虚拟机:virsh start centos7-clone
2)链接克隆
a、基于源磁盘文件、生成链接磁盘文件
[root@centos8 opt]# qemu-img create -f qcow2 -F qcow2 -b centos2-clone.qcow2 centos3.qcow2
Formatting ‘centos3.qcow2’, fmt=qcow2 cluster_size=65536 extended_l2=off compression_type=zlib size=10737418240 backing_file=centos2-clone.qcow2 backing_fmt=qcow2 lazy_refcounts=off refcount_bits=16
[root@centos8 opt]# qemu-img info centos3.qcow2
image: centos3.qcow2
file format: qcow2
virtual size: 10 GiB (10737418240 bytes)
disk size: 196 KiB
cluster_size: 65536
backing file: centos2-clone.qcow2
backing file format: qcow2
Format specific information:
compat: 1.1
compression type: zlib
lazy refcounts: false
refcount bits: 16
corrupt: false
extended l2: false
b、生成虚拟机配置文件
c、导入xml并启动
以上两步合并一条命令:
[root@centos8 opt]# virt-install --virt-type kvm --os-type=linux --os-variant rhel7.4 --name centos7-3 --memory 1024 --vcpus 1 --disk /opt/centos3.qcow2 --boot hd --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole