15

I'm a bit lost with virt-manager / libvirt / KVM.

I've got a working KVM VM (Windows XP) which works nicely.

The VM is backed by a 4GB file or so (a .img).

Now I want to do something very simple: I want to duplicate my VM.

I thought "OK, no problem, let's copy the 4GB file and copy the XML" file.

But then the libvirt FAQ states in all uppercase: "you SHOULD NOT CARE WHERE THE XML IS STORED"

libvirt FAQ

OK fine, I shouldn't care. But then how do I duplicate my VM?

I want to create a new VM that is a copy of that VM.

6 Answers 6

45

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 details.

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 image.
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"
4
  • This works also for copy over a VM to another host: use sftp instead of cp, (possible) change the image path before 'defining' the new vm and sftp also the that-vm.xml to the other host. Jul 28, 2013 at 15:58
  • Thanks for this, saved me a bunch of time. The problem I had is cloning vs creating new, in that if I create a new node, the package versions would be out of sync with the other nodes which used a local mirror. Aug 14, 2014 at 22:43
  • Does virt-clone handle full disk encryption correctly? Meaning, will it ask me for my decrypt pw before doing its thing?
    – a coder
    Feb 8, 2017 at 15:23
  • I think that sed -i "s/$source_vm/$new_vm" "/tmp/$new_vm.xml" should read sed -i "s/$source_vm/$new_vm/" "/tmp/$new_vm.xml" or at least on my debian derivative sed complains of a missing trailing /. Thanks for the info too, my virsh-clone was just not working at all.
    – number9
    Sep 19, 2018 at 14:14
6

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 >


1
  • this is useful if you are transferring VMs between disks on the same computer, you don't have another computer to boot into one of them to use virt-clone
    – mekb
    Apr 2 at 6:56
4

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.

0

Apparently virt-clone is the way to go.

I tried duplicating the XML but it wouldn't appear under virt-manager.

I still wonder how I can transfer an XML + .img to a new system that said...

0

The following command worked for me (Have to shutdown VSA/Vm before running the command)

# virt-clone --connect=qemu:///system -o present_vsa/vm_name -n vsa/vm_to_be_cloned_name --auto-clone
1
  • 2
    Aside from the pseudo VM names, this is identical to the command from the three-year-old answer — but lacking the supplemental information from that answer. Apr 21, 2015 at 7:19
0

virt-clone --auto-clone doesn't seem to play nicely with LVM. I was expecting it to create a snapshot and use that, but instead it copies all the data.

Better to do the dumpxml/define route; and remember to mount the clone's root filesystem and change IP addresses and hostname appropriately before starting the original and clone.

1
  • How do you mount the clone's root filesystem prior to starting the clone?
    – a coder
    Feb 17, 2021 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.