Libvirt - defining a volume that uses a domain through an API

In virt-managerwhile watching the tab "Storage" in the "Connection Information" has a column "Used", which shows the domain using each volume:

enter image description here

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

I looked at the API documentation and ran dir()on libvirt, libvirt.virConnect, libvirt.virStoragePool and libvirt.virStorageVol, but I still don't get it.

+4
source share
1 answer

, . , .

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
+3

Source: https://habr.com/ru/post/1523611/


All Articles