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

Python Libvirt:更改正在运行的域的网络接口,python,libvirt,Python,Libvirt,Virt Manager能够修改正在运行的域的网络接口,例如更改连接的网络 我想用python和libvirt API编写脚本 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 =

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

我想用python和libvirt API编写脚本

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经理可以做到这一点

谢谢你的帮助

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

virsh update-device 16 Testdebian.xml
Testdebian.xml必须只包含接口设备,而不是整个域xml


但是如何通过libvirtapi实现这一点呢?似乎没有通过API执行设备更新的方法….

我终于找到了一个解决方案:

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

deb.updateDeviceFlags(xml)
其中xml是包含设备描述的字符串

我在中发现了这一点,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

另一个坏处是,当域处于活动状态时,配置为“网络”类型的接口显示为“网桥”类型,因此您不能使用portgroups来更改正在运行的域的配置。