Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
1  
after I asked this question the fine folks on the libvirt project have added the 'virsh edit my-vm-name' command. –  Arthur Ulfeldt Nov 29 '11 at 7:37

3 Answers 3

up vote 6 down vote accepted

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
share|improve this answer
5  
need to add one step: (before defining the vm with the new name) "virsh undefine name-of-vm" otherwise it complains about duplicate UUID. –  Arthur Ulfeldt Nov 18 '10 at 17:12
    
i also had to rename the hd.img file and edit the line in the xml file –  Arthur Ulfeldt Nov 18 '10 at 18:45
    
Correct order is in the other answer. You don't have to rename img if you don't use the old machine. –  Nux Oct 8 '12 at 9:28
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

share|improve this answer

To change many machines you can use this:

virsh shutdown old-name

Wait for above to finish and run:

virsh dumpxml old-name > old-name.xml
virsh undefine old-name

Wait for above to finish and run:

sed -i 's/<name>old-name<\/name>/<name>new-name<\/name>/g' old-name.xml
virsh define old-name.xml

Run this one-by-one for each machine. You can use this RegExp if you have a list containing old-name new-name:

([^\r\n]+?)[ \t]+([^\r\n]+)
virsh shutdown $1\n#WAIT!\nvirsh dumpxml $1 > $1.xml\nvirsh undefine $1\n\#WAIT!\nsed -i 's/<name>$1<\\/name>/<name>$2<\\/name>/g' $1.xml\nvirsh define $1.xml\n
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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