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

52,134

Solution 1

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.

Solution 2

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"

Solution 3

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 >


Share:
52,134
SyntaxT3rr0r
Author by

SyntaxT3rr0r

Updated on September 17, 2022

Comments

  • SyntaxT3rr0r
    SyntaxT3rr0r over 1 year

    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.

  • Andreas Florath
    Andreas Florath almost 11 years
    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.
  • Mike Purcell
    Mike Purcell almost 10 years
    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.
  • G-Man Says 'Reinstate Monica'
    G-Man Says 'Reinstate Monica' about 9 years
    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.
  • Gbo
    Gbo over 7 years
    Does virt-clone handle full disk encryption correctly? Meaning, will it ask me for my decrypt pw before doing its thing?
  • number9
    number9 over 5 years
    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.
  • Gbo
    Gbo over 3 years
    How do you mount the clone's root filesystem prior to starting the clone?
  • mekb
    mekb about 2 years
    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