how to rename a VM using libvirt+KVM

How can I rename a VM on KVM+libvirt?

I would like it to change the name in the 'inventory' as well as change the name of the storage etc.


run

virsh dumpxml name_of_vm > name_of_vm.xml

Undefine the old vm to prevent an error because of an duplicate UUID.

virsh undefine name-of-vm

Edit the xml file then import it.

virsh define name_of_vm.xml

Of course you will have to stop and start the vm for the changes to take effect

virsh destroy name_of_vm
virsh start name_of_vm

virsh dumpxml myvm > foo.xml
<edit foo.xml, change the name, move storage>
virsh undefine myvm
virsh define foo.xml

Source: https://www.redhat.com/archives/libvirt-users/2010-October/msg00072.html


virsh implemented domrename in release 1.2.19: Sep 02 2015. So the current best practice is just:

 virsh domrename oldname newname

As you might expect, thedomain must be stopped, but also it cannot have any snapshots.