在virt-manager
中,查看连接详细信息下的存储选项卡时,有一个“使用者”列,显示使用每个卷的域:
如何使用 API(python 绑定)确定相同的信息,即使用给定卷的域?
我浏览了API 文档并dir()
在 libvirt、libvirt.virConnect、libvirt.virStoragePool 和 libvirt.virStorageVol 上运行,但我仍然对此一无所知。
这是我现在找到的解决方案。使用虚拟机域的名称,它返回域使用的卷的绝对路径。
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