KVM Virtualization Operations Guide: Rename Virtual Machine

In a KVM (Kernel-based Virtual Machine) virtualization environment, there may be a need to modify the virtual machine name after its creation due to planning issues. This article provides detailed modification steps for a production environment.

Requirement: Change the name from192.168.40.152-kylinsp3-prod-02 to192.168.40.152-kylinsp3-prod-03

[root@localhost ~]# virsh list
 Id   Name                                State
--------------------------------------------------
 1    192.168.40.150-kylinsp3-prod-01   running
 3    192.168.40.151-kylinsp3-prod-02   running
 4    192.168.40.152-kylinsp3-prod-02   running

📌 Main Steps

1️⃣ Backupthe original VM’s XML configuration2️⃣ Renamethe disk file3️⃣ Modify the XML configuration to apply the new name4️⃣ Undefine the original VM to avoid conflicts5️⃣ Redefine and start the new VM

🚀 Step 1: Export the original VM’s XML configuration

First, use the <span><span>virsh dumpxml</span></span> command to export the current virtual machine’s XML configuration file:

virsh dumpxml 192.168.40.152-kylinsp3-prod-02 &gt; /tmp/new-vm152.xml

Then, shut down the virtual machine.

[root@localhost ~]# virsh shutdown 192.168.40.152-kylinsp3-prod-02
Domain 192.168.40.152-kylinsp3-prod-02 has been shut down

[root@localhost ~]# virsh list --all
 Id   Name                                State
--------------------------------------------------
 1    192.168.40.150-kylinsp3-prod-01   running
 3    192.168.40.151-kylinsp3-prod-02   running
 -    192.168.40.152-kylinsp3-prod-02   shut off

At this point, the status of the virtual machine should change to <span><span>shut off</span></span>.

🔍 Step 2: Locate the original disk file

Use the <span><span>virsh dumpxml</span></span> or <span><span>ls</span></span> command to find the path of the virtual machine’s disk file:

[root@localhost ~]# virsh dumpxml  192.168.40.152-kylinsp3-prod-02 | grep "&lt;source file="
      &lt;source file='/data/qcow2/libvirt/images/192.168.40.152-kylinsp3-prod-02'/&gt;

📂 Step 3: Rename the disk file

Rename the disk file.

mv /data/qcow2/libvirt/images/192.168.40.152-kylinsp3-prod-02 /data/qcow2/libvirt/images/192.168.40.152-kylinsp3-prod-03

<span><span>Note: The file properties must be set to qemu:root, otherwise it may fail to start.</span></span>

✏️ Step 4: Modify the XML configuration

Use <span><span>vi</span></span> to edit the exported XML file.

vi  /tmp/new-vm152.xml

Modification 1: Adjust the virtual machine name

Find the <span><span><name></span></span> tag and change it to the new VM’s name.

&lt;name&gt;192.168.40.152-kylinsp3-prod-03&lt;/name&gt;

Modification 2: Update the disk file to the new name

&lt;source file='/data/qcow2/libvirt/images/192.168.40.152-kylinsp3-prod-03'/&gt;

Modification 3: Remove UUID to avoid conflicts

Find the <span><span><uuid></span></span> tag, delete or comment it out; a new UUID will be automatically generated later.

&lt;uuid&gt;7fa9bd2a-d5b5-4c01-9a14-0b7739f34a36&lt;/uuid&gt;

Save and exit.

🛠️ Step 5: Undefine the original VM

Delete the old VM definition.

[root@localhost images]# virsh undefine 192.168.40.152-kylinsp3-prod-02
error: Failed to undefine domain 192.168.40.152-kylinsp3-prod-02
error: The operation is not valid: cannot undefine domain with nvram

You need to add the --nvram parameter
[root@localhost images]# virsh undefine 192.168.40.152-kylinsp3-prod-02 --nvram
Domain 192.168.40.152-kylinsp3-prod-02 has been undefined

Note: If the VM uses UEFI boot, the <span>--nvram</span> option must be added, otherwise it cannot be deleted.

📌 Step 6: Import the new XML and define the VM

Use the modified XML to redefine the virtual machine.

virsh define /tmp/new-vm152.xml

<span><span>Verify if it was successful.</span></span>

🚀 Step 7: Start the new virtual machine

virsh list --all
virsh start  192.168.40.152-kylinsp3-prod-03

If everything goes well, the virtual machine should enter the <span><span>running</span></span> state.

🔎 Summary

The above method modifies the KVM virtual machine name and disk file through the exporting + modifying + redefining the XML configuration file approach.

Thank you for reading. If you found this article helpful, please give it a like + follow for support. Thank you!

Recommended previous articles

From Mechanical Hard Drive to SSD: KVM Virtualization Storage Pool Migration Solution

Leave a Comment