Use virsh to manage your virtual machines

BobAnkh published on
1 min, 180 words

Categories: Linux

We will introduce how to use virsh to manage your virtual machines.

Here we mainly talk about how to use virsh to remove and clean up virtual machines, as sometimes we have difficulties in removing virtual machines with vagrant.

First we can find all VMs currently valid by:

virsh list --all

Then we can check the configurations of the VM by its name:

virsh dumpxml <vm_name>

We shall also check the disk images of the VM in the configurations, which are usually stored in /var/lib/libvirt/images/. We may have to remove the disk images manually if we want to clean up the VM completely after we undefine the VMs. We can grep the disk images by grep source file.

Afterward, we have to shutdown the VM if it is running:

virsh shutdown <vm_name>

If the VM is not responding, we can force it to shutdown by:

virsh destroy <vm_name>

Also, we have to check if the VM has snapshots and remove them if necessary:

# List all snapshots
virsh snapshot-list <vm_name>
# Delete corresponding snapshots
virsh snapshot-delete <vm_name> [ --snapshotname <snapshot_name> ]

Finally, we can delete(undefine) the VM by:

virsh undefine <vm_name>

Also, don't forget to remove the disk images if you want to clean up the VM completely:

rm /var/lib/libvirt/images/<vm_name>