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

标签 python libvirt

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

enter image description here

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

我浏览了 API documentation并在 libvirt、libvirt.virConnect、libvirt.virStoragePool 和 libvirt.virStorageVol 上运行了 dir(),但我仍然对此一头雾水。

最佳答案

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

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

关于python - libvirt - 通过 API 确定域使用的卷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21342079/

相关文章:

python - 每当我将项目上传到应用引擎服务器时运行一些代码

python - Django 项目结构,跨应用程序共享扩展 auth "User"模型的推荐结构?

python - 在minimummodbus中写入多个从站时出错

c - 通过 libvirt 将文件描述符传递给 qemu (C api)

libvirt 的 KVM/QEMU 网络 TAP 问题

python - 计算多个字符串在另一个字符串中的出现次数

python - dict.clear() 的时间复杂度如何在 python 中为 O(1)?

Windows 上带有 libvirt 的 Java 客户端

java - 参数 Java servlet Libvirt 中的 UUID

android - Libvirt + 安卓模拟器