How to create a dupe of a KVM/libvirt/virt-manager VM?

The most convenient is simply:

# virt-clone --connect=qemu://example.com/system -o this-vm -n that-vm --auto-clone

Which will make a copy of this-vm, named that-vm, and takes care of duplicating storage devices. Nothing new here except detail.

More to the point, What the FAQ is saying is that the XML domain descriptions are not directly editable, you need to go through libvirt. To complete the steps taken by the virt-clone command, you could:

source_vm=vm_name
new_vm=new_vm_name

# You cannot "clone" a running vm, stop it.  suspend and destroy
# are also valid options for less graceful cloning
virsh shutdown "$source_vm"

# copy the storage.
cp /var/lib/libvirt/images/{"$source_vm","$new_vm"}.img

# dump the xml for the original
virsh dumpxml "$source_vm" > "/tmp/$new_vm.xml"

# hardware addresses need to be removed, libvirt will assign
# new addresses automatically
sed -i /uuid/d "/tmp/$new_vm.xml"
sed -i '/mac address/d' "/tmp/$new_vm.xml"

# and actually rename the vm: (this also updates the storage path)
sed -i "s/$source_vm/$new_vm" "/tmp/$new_vm.xml"

# finally, create the new vm
virsh define "/tmp/$new_vm.xml"
virsh start "$source_vm"
virsh start "$new_vm"

Other than "virt-clone" you can duplicate the VM this way:

  1. Ensure that existing VM (to be duplicated) is shut down.
  2. do a "sudo virsh dumpxml < domid >" of the existing VM, and save the output xml file.
  3. Modify the < name > tag under the < domain > tag.
  4. Use "uuidgen" to generate a new unique ID, and use that to modify the existing < uuid > tag.
  5. Make a copy of the existing qcow virtual images which the VM uses, (usually stored in /var/lib/libvirt/images, but to be sure just read your XML file for exact location). Command is "sudo cp /var/lib/libvirt/images/xxx.qcow2 yyyy.qcow2", and fill in the new file yyyy.qcow2 in XML file.
  6. Start the new vm: sudo virsh define new.xml
  7. Start the new domid: sudo virsh start < new_domid >



virsh will allow your to edit, export, and import the XML definition for your servers. I would use virt-clone to generate a cloned image file, and export the XML. To be safe I would remove the clone configuration from the original server.

Tags:

Linux

Kvm

Libvirt