【问题标题】:Libvirt: Change network interface of running domainLibvirt:更改运行域的网络接口
【发布时间】:2016-11-20 02:52:03
【问题描述】:

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 执行更新设备的方法....

【问题讨论】:

    标签: python libvirt


    【解决方案1】:

    我终于找到了解决办法:

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

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

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

    【讨论】:

      【解决方案2】:
      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
      

      【讨论】:

      • 另一个坏事是,当域处于活动状态时,配置为“network”类型的接口显示为“bridge”类型,因此您不能使用端口组更改正在运行的域的配置。跨度>
      猜你喜欢
      • 1970-01-01
      • 2016-05-28
      • 1970-01-01
      • 2021-01-03
      • 1970-01-01
      • 2019-04-18
      • 2017-09-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多