What Domains(VM’s) are Using what Hard Drive Volumes in KVM/Libvirt?

I noticed in my well-used test KVM virtual environment that I had a few volumes that had different names than did the existing domain (VM) names. I also noticed that I had more volumes than domains. So how do I tell which volumes are being used by which VM? And how can I tell what volumes are orphaned? Note for path purposes, my host is running Ubuntu 18.04.

I tried several “virsh” commands and spent time looking in the man pages. Nothing stood out so here is what I ended up doing.

First: I needed to list and locate the volumes in the “default” pool:

virsh vol-list --pool default

This also provides the path to the “default” pool where the volumes are located. I also noticed that I had a volume listed that was not shown when I did “ls -l” on the “/var/lib/libvirt/images/” directory. No problem, just update the pool listing:

virsh pool-refresh default

Solved that problem. Now I need to tie the volumes back to the VMs:

virsh dumpxml "my vm name"

That works but is cumbersome for multiple VMs and multiple volumes. So where are the VM configuration files located? A quick look around the directories where the volumes are stored turned up nothing, so best guess is /etc:

grep - ir "my vm name" /etc

That returns “/etc/libvirt/qemu/” and looking in that directory indicates we have found the VM config files. So now I need a list of VMs and their associated volume drives. Note that I am literally grepping for “source file” here:

grep -i "source file" /etc/libvirt/qemu/*

That allows me to tie the VM names to volume names that don’t match.

Now I can double check to make sure that a volume is or is not associated to a VM:

grep -i "name.qcow2" /etc/libvirt/qemu/*