KVM虚拟机的复制分为两种情况,第一种KVM宿主机上对虚拟机直接克隆,第二种通过复制配置文件与磁盘文件的虚拟机复制克隆(适用于异机的静态迁移)。
方法一:KVM宿主机上对虚拟机直接克隆(需要在关机或暂停的状态下操作)
1. Suspend the VM to be cloned. This is a requirement since it ensures that all data and network I/O on the VM is stopped.
# virsh suspend vm1
2. Run the virt-clone command:
# virt-clone \
--connect qemu:///system \
--original vm1 \
--name vm1-clone \
--file /vm-images/vm1-clone.img
This operation will take 2-3 minutes, depending on the size of the VM
3. When done, you can resume the original VM:
# virsh resume vm1
4. The cloned VM is placed in shutdown mode. To start it:
# virsh start vm1-clone
方法二:复制配置文件与磁盘文件进行克隆(可以不用关闭源虚拟机)
复制虚拟机的配置文件:
# virsh dumpxml vm1 > /etc/libvirt/qemu/vm2.xml
复制虚拟机的磁盘文件:
# cd /var/lib/libvirt/images/
# cp vm1.img vm2.img
修改相应的配置文件:
# vi /etc/libvirt/qemu/vm2.xml
通过新虚拟机的配置文件,定义新的虚拟机:
virsh define /etc/libvirt/qemu/vm2.xml
# virsh list --all
Id Name State
----------------------------------------------------
- vm2 shut off