how to rename a VM using libvirt+KVM

29,379

Solution 1

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

Solution 2

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

Solution 3

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.

Solution 4

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:
29,379

Related videos on Youtube

Kevin Son
Author by

Kevin Son

TLDR Clojure, Docker, Linux, Security, Teaching. https://www.linkedin.com/learning/instructors/arthur-ulfeldt?u=2125562 contacting me: If you are interested in learning clojure you can call me at 1-219-CLOJURE For general Clojure chatting you can find me in IRC #clojure on freenode (thearthur) email &lt; my first name &gt;@&lt; my last name &gt;.com Interests I'm a Clojure and Linux nut with a long standing interest in virtual machines and fancy networking of all sorts. At work I write Clojure Web apps and such full time for yummly.com as well as writing "cloud" deployment systems (some would call it "devops", though I think that term is worn out by now). At home I play with Clojure, Linux, docker, Amateur Radio, and Cryptography quite a bit. I have been a functional programming enthusiast for many years and get quite a lot of personal satisfaction every time i use anything map-reduce related. I am interested in network security related projects and people that are trying to steer the world away from "the corporate castle" metaphor. If you have or are thinking about such a project I would love to hear from you. note for recruiters: I would like to politely decline any positions you might have with "devops" or "language-name engineer" in the title. PS: KE6DRD

Updated on September 17, 2022

Comments

  • Kevin Son
    Kevin Son almost 2 years

    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.

    • Kevin Son
      Kevin Son over 12 years
      after I asked this question the fine folks on the libvirt project have added the 'virsh edit my-vm-name' command.
  • Kevin Son
    Kevin Son over 13 years
    need to add one step: (before defining the vm with the new name) "virsh undefine name-of-vm" otherwise it complains about duplicate UUID.
  • Kevin Son
    Kevin Son over 13 years
    i also had to rename the hd.img file and edit the line in the xml file
  • hachi
    hachi over 11 years
    Correct order is in the other answer. You don't have to rename img if you don't use the old machine.
  • reedstrm
    reedstrm almost 8 years
    This answer is now out of date. See my answer below. tldr; virsh now has domrename.
  • Dennis Williamson
    Dennis Williamson over 7 years
    +1 - you can virsh edit newname and change the file path and mv oldname newname to rename the directory if you want everything to match.
  • MaxiReglisse
    MaxiReglisse about 5 years
    in other words, you also have to 1) rename /var/lib/libvirt/images/oldname.qcow2 in /var/lib/libvirt/images/newname.qcow2 ; and 2) edit /etc/libvirt/qemu/newname.xml to set the correct source file balise <source file='/var/lib/libvirt/images/newname.qcow2'
  • reedstrm
    reedstrm almost 5 years
    but only if you need the filenames to match the domainnames (which I admit, is convenient) - this avoids a destroy/create cycle, though still requires a stop (and no snapshots)