参考:kvm镜像制作-CSDN博客或kvm磁盘和快照-CSDN博客
格式: virt-clone --auto-clone -o 虚拟机名称 -n 克隆名称
virt-clone这个命令是基于全克隆的,也就是拷⻉虚拟磁盘⽂件和虚拟配置⽂件来实现的完整克隆,速度慢,占⽤空间多。
[root@admin ~]# virsh net-start default
Network default started
[root@admin ~]# virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name web01 --memory 1024 --vcpus 1 --disk /opt/web01-vda.raw,format=raw,size=20 --cdrom /opt/CentOS-7-x86_64-DVD-2009.iso --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole
Starting install...
Allocating 'web01-vda.raw' | 20 GB 00:00:00
Domain installation still in progress. You can reconnect to
the console to complete the installation process.
[root@admin ~]#
[root@admin ~]# virsh list
Id Name State
----------------------------------------------------
2 web01 running
[root@admin ~]# virsh shutdown web01
Domain web01 is being shutdown
[root@admin ~]# virt-clone --auto-clone -o web01 -n web01copy
Allocating 'web01-vda-clone.raw' | 20 GB 00:00:11
Clone 'web01copy' created successfully.
[root@admin ~]# virsh list --all
Id Name State
----------------------------------------------------
- web01 shut off
- web01copy shut off
[root@admin ~]#
等待安装完成
kvm 链接克隆
KVM的链接克隆是通过创建⼀个链接磁盘⽂件来实现的链接克隆,⽽kvm软件包中并没有实现全⾃动链接克隆的命令或⼯具,只 能⼿动实现。
格式:qemu-img create -f qcow2 -b 源磁盘⽂件 链接磁盘⽂件
-f 指定磁盘⽂件格式类型
-b 执⾏链接磁盘⽂件路径
[root@admin ~]# qemu-img create -f qcow2 -b /opt/web01-vda.raw /opt/web01-clone.qcow2
Formatting '/opt/web01-clone.qcow2', fmt=qcow2 size=21474836480 backing_file=/opt/web01-vda.raw cluster_size=65536 lazy_refcounts=off refcount_bits=16
[root@admin ~]#
查看链接磁盘文件信息
[root@admin ~]# qemu-img info /opt/web01-clone.qcow2
image: /opt/web01-clone.qcow2 //位置
file format: qcow2 //格式
virtual size: 20G (21474836480 bytes) //磁盘大小
disk size: 196K //磁盘实际占用大小
cluster_size: 65536
backing file: /opt/web01-vda.raw //源文件路径
Format specific information:
compat: 1.1
lazy refcounts: false
refcount bits: 16
corrupt: false
[root@admin ~]#
导出原始机的配置文件,并修改磁盘路径
[root@admin ~]# virsh dumpxml web01 > web01-link-clone-1.xml
[root@admin ~]# vim web01-link-clone-1.xml
#修改内容
<name>web01-link-clon3</name>
#删除UUID
<uuid>727a1566-ff36-4ed0-a096-f844f6bf3262</uuid>
#修改磁盘路径
<source file='/opt/web01-clone.qcow2'/>
#删除MAC地址
<mac address='52:54:00:bd:b6:44'/>
导入虚拟机
[root@admin ~]# virsh define web01-link-clone-1.xml
Domain web01-link-clone defined from web01-link-clone-1.xml
[root@admin ~]# virsh list --all
Id Name State
----------------------------------------------------
- web01 shut off
- web01-link-clone shut off
- web01copy shut off
[root@admin ~]#
#启动
[root@admin ~]# virsh start web01-link-clone
Domain web01-link-clone started
[root@admin ~]# virsh list
Id Name State
----------------------------------------------------
3 web01-link-clone running
[root@admin ~]#
ssh登录测试
[root@admin ~]# ssh root@192.168.122.171
The authenticity of host '192.168.122.171 (192.168.122.171)' can't be established.
ECDSA key fingerprint is SHA256:HRaADRjWpJEg0PhTpVjuPwJh/kN9tPvzPHHLNthOCoY.
ECDSA key fingerprint is MD5:36:7c:6e:80:35:28:d0:57:c0:f1:b1:7e:6c:a8:29:bd.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.122.171' (ECDSA) to the list of known hosts.
root@192.168.122.171's password:
Last login: Wed May 29 23:05:17 2024
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# ip a
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:d1:50:48 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.171/24 brd 192.168.122.255 scope global noprefixroute dynamic eth0
valid_lft 3468sec preferred_lft 3468sec
inet6 fe80::5903:76cf:572:98f0/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
link/ether 52:54:00:a9:e5:3e brd ff:ff:ff:ff:ff:ff
inet 192.168.124.1/24 brd 192.168.124.255 scope global virbr0
valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
link/ether 52:54:00:a9:e5:3e brd ff:ff:ff:ff:ff:ff
[root@localhost ~]#