def add_interface(self, type, id, mac=None, model_type='virtio'): ''' :param type:' bridge' OR 'network' . If bridge inserts xml code for bridge, If network insert xml code for virtual network :return: ''' if mac is None: mac = randomMAC() if type == 'bridge': interface_etree = etree.parse(StringIO(XML_SNIPPET_BRIDGE)) interface_etree.xpath('/interface')[0].xpath('source')[0].set('bridge', id) elif type == 'network': interface_etree = etree.parse(StringIO(XML_SNIPPET_NETWORK)) interface_etree.xpath('/interface')[0].xpath('source')[0].set('network', id) else: log.error('type of interface incorrect when adding interface in xml') return -1 interface_etree.xpath('/interface')[0].xpath('mac')[0].set('address', mac) interface_etree.xpath('/interface')[0].xpath('model')[0].set('type', model_type) new_interface = interface_etree.xpath('/interface')[0] xpath_same = '/domain/devices/interface' xpath_next = '/domain/devices/input' xpath_previous = '/domain/devices/controller' self.add_device(xpath_same, new_interface, xpath_next=xpath_next, xpath_previous=xpath_previous)
def reset_mac_address(self, dev_index=0, mac=None): '''delete the mac address from xml. In the next boot libvirt create a random mac If you want to fix mac address mac parameter is optional. If there are more than one network device, the dev_index indicates the network device position in the xml definition. dev_index = 0 is the first network interface defined in xml.''' if mac is None: mac = randomMAC() self.tree.xpath('/domain/devices/interface')[dev_index].xpath('mac')[dev_index].set('address', mac)
def new_random_mac(self): new_mac = randomMAC() self.reset_mac_address(mac=new_mac) return new_mac