2016-11-20 87 views
0

Virt-Manager能夠修改運行域的網絡接口,例如更改連接的網絡。Libvirt:更改運行域的網絡接口

我想用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來執行更新設備的方法....

回答

0

我終於找到了解決方案:

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

deb.updateDeviceFlags(xml) 

其中XML是包含設備描述的字符串。

我發現這在Libvirt's JavaDocs,Python和C文檔似乎缺乏很多功能。

0
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 
+0

另一個不好的一點是,與「網絡」型配置的接口都爲「橋」式的,當域是活動的,所以你不能用端口組改變運行域的configuragion。 – Magwan