libvirt确定域通过API使用的卷

2024-10-01 00:32:36 发布

您现在位置:Python中文网/ 问答频道 /正文

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

enter image description here

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

我浏览了API documentation并在libvirt上运行了dir(),libvirt.virConnect, libvirt.virStoragePool,和libvirt.virStorageVol,但这件事我还是不知所措。在


Tags: api信息bydocumentationdirmanager详细信息选项卡
1条回答
网友
1楼 · 发布于 2024-10-01 00:32:36

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

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

相关问题 更多 >