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

时间:2016-11-20 02:52:04

标签: python libvirt

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)

但这并不奏效。网络没有改变。有人可以给我一个tipp如何用libvirt修改正在运行的域名吗?我在文档中找不到任何相关内容。但必须有可能,因为Virt-Manager可以做到。

感谢您的帮助。

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

virsh update-device 16 Testdebian.xml

Testdebian.xml只能包含接口设备,而不是整个域-XML。

但是我怎么能通过libvirt-API来做到这一点?似乎没有办法通过API执行更新设备....

2 个答案:

答案 0 :(得分:1)

我终于找到了解决方案:

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

deb.updateDeviceFlags(xml)

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

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

答案 1 :(得分:1)

Uri url = new Uri("http://lu32kap.typo3.lrz.de/mensaapp/exportDB.php?mensa_id=all");

        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.UserAgent.TryParseAdd("Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
        HttpResponseMessage response = await client.GetAsync(url);
        response.EnsureSuccessStatusCode();
        IHttpContent content = response.Content;
        if(content != null)
        {
            IBuffer buffer = await content.ReadAsBufferAsync();
            using (DataReader dataReader = DataReader.FromBuffer(buffer))
            {
                string result = dataReader.ReadString(buffer.Length);
                if (result != null)
                {
                    tblock.Text = result;
                }
            }
        }

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

糟糕的是,您无法通过这种方式修改mac或pci地址。例如

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.