Libvirt:更改 运行 域的网络接口

Libvirt: Change network interface of running domain

Virt-Manager 能够修改 运行 域的网络接口,例如更改连接的网络。

我想用 libvirt-API 在 python 中编写脚本。

import libvirt
conn = libvirt.open('qemu:///system')
deb = conn.lookupByName('Testdebian')
xml = deb.XMLDesc()
xml = replace('old-network-name', 'new-network-name')
deb.undefine()
deb = conn.defineXML(xml)

但这不起作用。网络没有改变。有人可以告诉我如何使用 libvirt 修改 运行 域吗?我在文档中找不到任何相关信息。但这一定是可能的,因为 Virt-Manager 可以做到。

感谢您的帮助。

编辑:我设法通过 virsh 执行网络更改:

virsh update-device 16 Testdebian.xml

Testdebian.xml必须只包含接口设备,不能包含整个域-XML.

但是我如何通过 libvirt-API 执行此操作?似乎没有方法可以通过 API.....

执行更新设备

我终于找到了解决办法:

import libvirt
conn = libvirt.open('qemu:///system')
deb = conn.lookupByName('Testdebian')

deb.updateDeviceFlags(xml)

其中 xml 是包含设备描述的字符串。

我在 Libvirt's JavaDocs 中找到了这个,Python 和 C 文档似乎缺少很多功能。

updateDeviceFlags(xml, flags=0) method of libvirt.virDomain instance
Change a virtual device on a domain, using the flags parameter
to control how the device is changed.  VIR_DOMAIN_AFFECT_CURRENT
specifies that the device change is made based on current domain
state.  VIR_DOMAIN_AFFECT_LIVE specifies that the device shall be
changed on the active domain instance only and is not added to the
persisted domain configuration. VIR_DOMAIN_AFFECT_CONFIG
specifies that the device shall be changed on the persisted domain
configuration only.  Note that the target hypervisor must return an
error if unable to satisfy flags.  E.g. the hypervisor driver will
return failure if LIVE is specified but it only supports modifying the
persisted device allocation.

This method is used for actions such changing CDROM/Floppy device
media, altering the graphics configuration such as password,
reconfiguring the NIC device backend connectivity, etc.

其中 VIR_DOMAIN* 是 libvirt 模块中定义的常量。

不好的是你不能这样修改mac或pci地址。例如

libvirtError: operation failed: no device matching mac address 68:80:82:e3:7b:4b found on 0000:00:09.0