Question

In virt-manager, when viewing the Storage tab under Connection Details, there is a "Used By" column that shows the domain using each volume:

enter image description here

How can I determine this same information, namely, the domain using a given volume, using the API (python bindings)?

I browsed the API documentation and ran dir() on libvirt, libvirt.virConnect, libvirt.virStoragePool, and libvirt.virStorageVol, but I'm still at a loss on this one.

Was it helpful?

Solution

Here is the solution that I've found for now. With the name of a virtual machine's domain, it returns the absolute path of the volume used by the domain.

import libvirt
from xml.etree import ElementTree as ET

URI = "qemu:///system"
VM = "truffles"

# Get the virDomain object
conn = libvirt.open(URI)
domain_object = conn.lookupByName(VM)

# Get the XML description of the VM
vm_xml = domain_object.XMLDesc(0)

# Get the volume in use from the element tree
root = ET.fromstring(vm_xml)
disk_source = root.find('./devices/disk/source')
volume_in_use = disk_source.get('file')

print volume_in_use
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top