KVM之虚拟机克隆

柳三千

文章最后更新时间:2025年06月13日

前言:

KVM虚拟机由磁盘文件和配置文件构成,配置文件中的主机名、uuid、mac地址、磁盘文件路径需唯一,避免与其他虚拟机冲突。

完整克隆

自动克隆

# 语法:virt-clone --auto-clone -o 老虚拟机 -n 新虚拟机
[root@KVM ~]# virsh list
 Id    名称                         状态
----------------------------------------------------
 1     web_mysql                    running

[root@KVM ~]# virt-clone --auto-clone -o web_mysql -n backup
ERROR    必须暂停或者关闭有要克隆设备的域。

[root@KVM ~]# virsh shutdown web_mysql 
域 web_mysql 被关闭

[root@KVM ~]# virt-clone --auto-clone -o web_mysql -n backup

[root@KVM ~]# virsh list
 Id    名称                         状态
----------------------------------------------------
 2     backup                       running
 3     web_mysql                    running

手动克隆

# 1. 复制磁盘文件
[root@KVM ~]# cd /data
[root@KVM data]# ls
Centos7-clone.qcow2  Centos7.qcow2

[root@KVM data]# cp -a Centos7.qcow2 Centos7-test.qcow2 
[root@KVM data]# ll -h
总用量 5.9G
-rw-------. 1 qemu qemu 1.6G 11月 23 17:27 Centos7-clone.qcow2
-rw-r--r--. 1 qemu qemu 2.1G 11月 23 17:29 Centos7.qcow2
-rw-r--r--. 1 qemu qemu 2.1G 11月 23 17:29 Centos7-test.qcow2

# 2. 导出并修改配置文件
[root@KVM data]# virsh dumpxml web_mysql > test.xml
[root@KVM data]# vim test.xml 

# 修改虚拟机名字
[root@KVM data]# cat test.xml | grep -i name
  <name>test</name>

# 删除虚拟机的uuid和MAC地址,修改磁盘路径
[root@KVM data]# cat test.xml | grep -i source
  <resource>
  </resource>
      <source file='/data/Centos7-test.qcow2'/>

# 3. 加载配置文件并启动
[root@KVM data]# virsh define test.xml 
定义域 test(从 test.xml)

[root@KVM data]# virsh list --all
 Id    名称                         状态
----------------------------------------------------
 2     backup                       running
 3     web_mysql                    running
 -     centos7.2                     关闭
 -     test                         关闭

[root@KVM data]# virsh start test 
域 test 已开始

[root@KVM data]# virsh list
 Id    名称                         状态
----------------------------------------------------
 2     backup                       running
 3     web_mysql                    running
 4     test                         running

[root@KVM data]# virsh dumpxml test 
<domain type='kvm' id='4'>
  <name>test</name>
  <uuid>3e93cddc-c080-4094-b838-de093d989bb7</uuid>

链接克隆

生成磁盘文件链接

# 语法:qemu-img create -f 格式 -b 旧磁盘路径 新磁盘路径
[root@KVM data]# ls
Centos7-clone.qcow2  Centos7.qcow2  Centos7-test.qcow2  test.xml
[root@KVM data]# qemu-img create -f qcow2 -b Centos7.qcow2 web.qcow2
Formatting 'web.qcow2', fmt=qcow2 size=10737418240 backing_file='Centos7.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off 
[root@KVM data]# ll -h
总用量 5.7G
-rw-------. 1 qemu qemu 1.6G 11月 23 17:38 Centos7-clone.qcow2
-rw-r--r--. 1 qemu qemu 2.1G 11月 23 17:41 Centos7.qcow2
-rw-r--r--. 1 qemu qemu 2.1G 11月 23 17:43 Centos7-test.qcow2
-rw-r--r--. 1 root root 5.3K 11月 23 17:33 test.xml
-rw-r--r--. 1 root root 193K 11月 23 17:44 web.qcow2
[root@KVM data]# qemu-img info web.qcow2 
image: web.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 196K
cluster_size: 65536
backing file: Centos7.qcow2
Format specific information:
    compat: 1.1
    lazy refcounts: false

导出配置文件并修改

[root@KVM data]# virsh list
 Id    名称                         状态
----------------------------------------------------
 2     backup                       running
 3     web_mysql                    running
 4     test                         running

[root@KVM data]# virsh dumpxml web_mysql > web.xml
[root@KVM data]# vim web.xml
# 修改虚拟机名字,删除uuid和MAC地址,修改磁盘路径

[root@KVM data]# virsh define web.xml 
定义域 web(从 web.xml)

[root@KVM data]# virsh start web 
域 web 已开始

[root@KVM data]# virsh list
 Id    名称                         状态
----------------------------------------------------
 2     backup                       running
 3     web_mysql                    running
 4     test                         running
 5     web                          running
文章版权声明:除非注明,否则均为柳三千运维录原创文章,转载或复制请以超链接形式并注明出处。

取消
微信二维码
微信二维码
支付宝二维码