libvirt - 确定域通过API使用的卷

时间:2014-01-24 20:39:33

标签: python libvirt

virt-manager中,查看“连接详细信息”下的“存储”选项卡时,会显示“使用者”列,其中显示使用每个卷的域:

enter image description here

如何使用API​​(python绑定)确定相同的信息,即使用给定卷的域?

我浏览了API documentation并在libvirt,libvirt.virConnect,libvirt.virStoragePool和libvirt.virStorageVol上运行dir(),但我仍然对此感到茫然。

1 个答案:

答案 0 :(得分:3)

这是我现在找到的解决方案。使用虚拟机域的名称,它返回域使用的卷的绝对路径。

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