It’s very convenient to manage all VM with libvirt under Linux. And virsh — is a program to make this managing even more convenient. Today I faced with a task of renaming VM under libvirt.
Assume we define some VM in my_favourite_vm.xml and want to rename it from my_favourite_vm to old_fashioned.
sudo virsh shutdown my_favourite_vm
sudo virsh undefine my_favourite_vm
Rename my_favourite_vm.xml to old_fashioned_vm.xml and change <name>my_favourite_vm</name> to <name>old_fashioned_vm</name> inside xml
sudo virsh create old_fashioned_vm.xml
sudo virsh list
Seems, it’s done. But if you could ssh to your vm earlier you may face that now it’s unavailable by new name.
ssh root@old_fashioned_vm
So you need to fix hostname. Use virsh console to connect to vm
user@host: sudo virsh console tcu
Connected to domain tcu
Escape character is ^]
Login, then:
cat old_fashioned_vm > /etc/hostname
vim /etc/hosts #and fix all occurences
reboot
Press escape character to exit console mode. In my case it’s ctrl + ]
Now the task is done and vm is renamed altogether with it’s hostname! :)