Renaming a VM in libvirt isn't as straightforward as a simple CLI command. The operation requires coordinated changes across multiple components:
- Libvirt's XML definition
- Storage pool volumes
- Virtual machine inventory
- Potential guest OS references
Here's how to completely rename a VM called "old-vm" to "new-vm":
# First, shutdown the VM if running
virsh shutdown old-vm
# Dump the current XML definition
virsh dumpxml old-vm > old-vm.xml
# Edit the XML file to update all name references
sed -i 's/old-vm/new-vm/g' old-vm.xml
# Undefine the old VM (this removes it from inventory)
virsh undefine old-vm
# Define the new VM with updated XML
virsh define old-vm.xml
# Optional: Rename storage volumes
for vol in $(virsh vol-list default | grep old-vm | awk '{print $1}'); do
new_vol=$(echo $vol | sed 's/old-vm/new-vm/g')
virsh vol-create-as default $new_vol $(virsh vol-info $vol | grep Capacity | awk '{print $2}')
virsh vol-upload $new_vol $(virsh vol-dumpxml $vol | grep path | sed -e 's/.*//' -e 's/<\/path>.*//')
virsh vol-delete $vol
done
Snapshots: If your VM has snapshots, you'll need to handle them separately:
# List snapshots
virsh snapshot-list old-vm
# For each snapshot, recreate under new name
virsh snapshot-create-as new-vm snapshot1 "First snapshot"
Networking: MAC addresses and network filters may need adjustment:
# Check current network interfaces
virsh dumpxml new-vm | grep -i mac
After renaming, verify everything works:
virsh list --all
virsh domblklist new-vm
virsh domiflist new-vm
For frequent renames, consider this bash script (save as rename-vm.sh):
#!/bin/bash
if [ $# -ne 2 ]; then
echo "Usage: $0 old_vm_name new_vm_name"
exit 1
fi
OLD=$1
NEW=$2
# Shutdown VM if running
virsh shutdown $OLD
# Dump and edit XML
virsh dumpxml $OLD > /tmp/vm.xml
sed -i "s/$OLD/$NEW/g" /tmp/vm.xml
# Undefine old and define new
virsh undefine $OLD
virsh define /tmp/vm.xml
echo "VM renamed from $OLD to $NEW in libvirt inventory"
Renaming a KVM virtual machine involves more than just changing its displayed name. To properly rename a VM using libvirt, you'll need to update multiple components:
- The domain XML definition
- Storage volume references
- Virtual machine inventory entries
- Any attached devices or interfaces
Here's how to completely rename a VM called "old-vm" to "new-vm":
# First, shutdown the VM if it's running
virsh shutdown old-vm
# Dump the current XML configuration
virsh dumpxml old-vm > old-vm.xml
# Edit the XML file to change all references
sed -i 's/old-vm/new-vm/g' old-vm.xml
# Undefine the old VM
virsh undefine old-vm
# Rename storage volumes (example for qcow2 format)
mv /var/lib/libvirt/images/old-vm.qcow2 /var/lib/libvirt/images/new-vm.qcow2
# Define the new VM
virsh define old-vm.xml
# Optional: Clean up old snapshots
virsh snapshot-list old-vm # Check for snapshots
virsh snapshot-delete old-vm --snapshotname snapshot1 # Example
If your VM uses storage pools, you'll need to update those references:
# List current volumes
virsh vol-list default
# For each volume belonging to the VM
virsh vol-path --pool default old-vm.qcow2
virsh vol-rename --pool default old-vm.qcow2 new-vm.qcow2
For frequent renames, consider this Python script using libvirt-python:
import libvirt
conn = libvirt.open('qemu:///system')
def rename_vm(old_name, new_name):
dom = conn.lookupByName(old_name)
xml = dom.XMLDesc()
new_xml = xml.replace(old_name, new_name)
# Undefine old domain
dom.undefine()
# Define new domain
conn.defineXML(new_xml)
print(f"VM renamed from {old_name} to {new_name}")
rename_vm('old-vm', 'new-vm')
After renaming, verify everything is correctly updated:
# Check VM list
virsh list --all
# Verify storage
virsh domblklist new-vm
# Check network interfaces
virsh domiflist new-vm
If you encounter problems:
- Permission errors: Check SELinux contexts with
restorecon -Rv /var/lib/libvirt/
- XML validation errors: Use
virt-xml-validate old-vm.xml
- Network issues: Update interface MAC addresses if they were name-based