def node_info(self, node_id): """Return kvm node info :param node_id: id ofnode """ if self.conn is None: raise VirtServerError('No connection to libvirt %s host found' % self.id) info = xml2dict(self.conn.getCapabilities()) # main info data = {'id': node_id, 'name': self.hostname} # hardware data['hardware'] = info['host'] # runtime data['runtime'] = None # cofiguration data['config'] = None # firmware data['firmware'] = info['guest'] return data
def __get_vm_info(self, dom): """ info : {state, maxMem, memory, nrVirtCpu, cpouTime} XMLDesc flags: 1 VIR_DOMAIN_XML_SECURE dump security sensitive information too 2 VIR_DOMAIN_XML_INACTIVE dump inactive domain information 4 VIR_DOMAIN_XML_UPDATE_CPU update guest CPU requirements according to host CPU 8 VIR_DOMAIN_XML_MIGRATABLE dump XML suitable for migration TO_DO get machine type: vm or template TO-DO get cpu socket num and frequency TO-DO set status """ infos = dom.info() ext_infos = xml2dict(dom.XMLDesc(8)) # example: x86_64 os_arch = ext_infos['os']['type']['arch'] # example: rhel6.4.0 os_machine = ext_infos['os']['type']['machine'] # example: 5902 vnc_port = ext_infos['devices']['graphics']['port'] # create vm vm = VirtualMachine(dom.name(), dom.UUIDString(), os_machine + '-' + os_arch, None) #vm.set_status(vm_state[vm.get_status()]) vm.set_port('vnc', vnc_port) vm.resource.set_cpu(infos[3], 0, 0) vm.resource.set_memory_max(infos[2]) return vm
def datastore_info(self, name=None, id=None): """TO-DO Exception: VirtServerError. :param name: :param id: :return {'allocation': {'data': '9344090112', 'unit': 'bytes'}, 'available': {'data': '527526821888', 'unit': 'bytes'}, 'capacity': {'data': '536870912000', 'unit': 'bytes'}, 'name': '62350862-632f-3598-97cd-6195ae92c874', 'source': {'dir': {'path': '/mnt/zvol01/clsk_kvm/primary'}, 'format': {'type': 'auto'}, 'host': {'name': 'freenas.clskdom.lab'}}, 'target': {'path': '/mnt/62350862-632f-3598-97cd-6195ae92c874', 'permissions': {'group': '-1', 'mode': '0755', 'owner': '-1'}}, 'type': 'netfs', 'uuid': '62350862-632f-3598-97cd-6195ae92c874'} """ if self.conn is None: raise VirtServerError('No connection to libvirt %s host found' % self.id) try: if name != None: dom = self.conn.storagePoolLookupByName(name) elif id != None: dom = self.conn.storagePoolLookupByUUIDString(id) data = xml2dict(dom.XMLDesc(1)) return data except libvirt.libvirtError as ex: raise VirtServerError(ex)
def info(self): """ Get virtual machine description. Specify at least name or id. Exception: VirtDomainError. :param name: [optional] name of virtual machine :param id: [optional] id of virtual machine """ #try: if True: data = xml2dict(self._domain.XMLDesc(8)) self.switch() #data = xml2dict(self._domain.XMLDesc()) self.logger.debug('Get libvirt domain info: %s' % self._name) #st = None #st = libvirt.virStream(self.conn) #print st.connect() #st = self.conn.newStream(flags=libvirt.VIR_STREAM_NONBLOCK) #st.connect() #print st #print self._domain #'org.qemu.guest_agent.0' #print self._domain.openChannel('charchannel1', st, flags=libvirt.VIR_DOMAIN_CHANNEL_FORCE) #st.finish() #self._domain.reboot(libvirt.VIR_DOMAIN_SHUTDOWN_GUEST_AGENT) #self._domain.shutdownFlags(libvirt.VIR_DOMAIN_SHUTDOWN_GUEST_AGENT) return data
def nw_filters_list(self): """ """ if self.conn is None: raise VirtServerError('No connection to libvirt %s host found' % self.id) data = [] for item in self.conn.listAllNWFilters(0): data.append(xml2dict(item.XMLDesc(0)))
def datastore_tree(self, name=None, id=None, path='/'): """ Exception: VirtServerError. :param name: :param uuid: """ if self.conn is None: raise VirtServerError('No connection to libvirt %s host found' % self.id) data = [] try: if name != None: dom = self.conn.storagePoolLookupByName(name) elif id != None: dom = self.conn.storagePoolLookupByUUIDString(id) storage_path = xml2dict(dom.XMLDesc(1))['target']['path'] vols = dom.listVolumes() for vol in vols: try: vol_path = "%s/%s" % (storage_path, vol) volobj = self.__volume_info(path=vol_path) itime = {} # atime: time of last access (ls -lu) #t = ctime(float(volobj['target']['timestamps']['atime'])) t = gmtime(float(volobj['target']['timestamps']['atime'])) itime['atime'] = strftime("%d-%m-%Y %H:%M:%S", t) # mtime: time of last modification (ls -l) t = gmtime(float(volobj['target']['timestamps']['mtime'])) itime['mtime'] = strftime("%d-%m-%Y %H:%M:%S", t) # ctime: time of last status change (ls -lc) t = gmtime(float(volobj['target']['timestamps']['ctime'])) itime['ctime'] = strftime("%d-%m-%Y %H:%M:%S", t) size = { 'capacity': int(volobj['capacity']['data']), 'used': int(volobj['allocation']['data']) } # add new row to tree raw = DatastoreItem(volobj['name'], volobj['target']['path'], size, volobj['target']['format']['type'], volobj['target']['permissions'], itime) except Exception, e: print e # add new empty row to tree raw = DatastoreItem(vol, None, None, None, None, None) data.append(raw) except libvirt.libvirtError as ex: raise VirtServerError(ex) return data
def _build_device_list(self, device_type): #guest = self._get_guest(refresh_if_necc=refresh_if_necc, inactive=inactive) devs = xml2dict(self._domain.XMLDesc(8))['devices'][device_type] if type(devs) is not list: devs = [devs] count = 0 for dev in devs: dev['vmmindex'] = count count += 1 return devs
def info(self): """ Get virtual machine description. Specify at least name or id. Exception: VirtDomainMonitorError. :param name: [optional] name of virtual machine :param id: [optional] id of virtual machine """ #try: if True: data = xml2dict(self._domain.XMLDesc(8)) self.logger.debug('Get libvirt domain info: %s' % self._name) return data
def __get_vm_description(self, name=None, id=None): """ Get virtual machine description. Specify at least name or id. Exception: VirtServerError. :param name: [optional] name of virtual machine :param id: [optional] id of virtual machine """ try: if name != None: dom = self.conn.lookupByName(name) elif id != None: dom = self.conn.lookupByName(id) data = xml2dict(dom.XMLDesc(8)) return data except libvirt.libvirtError, ex: raise VirtServerError(ex)
def __volume_info(self, name=None, path=None): """ Exception: VirtServerError. :param extended: True show more description fields """ if self.conn is None: raise VirtServerError('No connection to libvirt %s host found' % self.id) data = None try: if name != None: vol = self.conn.storageVolLookupByName(name) elif path != None: vol = self.conn.storageVolLookupByPath(path) data = xml2dict(vol.XMLDesc(0)) data['storage'] = vol.storagePoolLookupByVolume().name() except libvirt.libvirtError as ex: raise VirtServerError(ex) return data
def node_network_conf(self, node_id): """ Describe node network configuration. :param node_id: id of the node. """ if self.conn is None: raise VirtServerError('No connection to libvirt %s host found' % self.id) try: data = {} # get physical interface # inactive interface for item in self.conn.listAllInterfaces(1): desc = xml2dict(item.XMLDesc(0)) interface = self.__get_interface(desc, False) data[interface.name] = interface # active interface for item in self.conn.listAllInterfaces(2): desc = xml2dict(item.XMLDesc(0)) if (desc['type'] == 'ethernet'): interface = self.__get_interface(desc, True) else: interface = self.__get_bridge(desc, True) data[interface.name] = interface # add bridge from libvirt list for net in self.conn.listAllNetworks(0): desc = xml2dict(net.XMLDesc(0)) active = net.isActive() bridge = Bridge(desc['bridge']['name'], active) mac = getattr(getattr(desc, 'mac', None), 'address', None) port = Interface(desc['bridge']['name'], active, mac) port.add_ip_protocol('ipv4', desc['ip']['address'], desc['ip']['netmask']) bridge.add_port(port) bridge.set_param('delay', desc['bridge']['delay']) bridge.set_param('stp', desc['bridge']['stp']) bridge.set_param('forward', desc['forward']) bridge.set_policy('dhcp', desc['ip']['dhcp']) data[bridge.name] = bridge # get sub-bridge # get all running vms vm_status = 16 vm_list = self.vms_list(host=None, status=vm_status) # iter vm list and find bridge used from vnic for item in vm_list: nets = self.vm_network(name=item.name) for net in nets: bridge = net.params['bridge'] if bridge not in data: data[bridge] = Bridge(bridge, True) # create interface interface = Interface(net.name, True, net.mac) # add info of virtual machine parent of interface interface.set_param('vm', item.name) data[bridge].add_port(interface) # get dict for k, v in data.iteritems(): data[k] = v #.get_dict() return data except libvirt.libvirtError as ex: raise VirtServerError(ex)