文章标题
2017-08-18 13:53
232 查看
这几天在用python libvirt api管理kvm虚拟机,写个日志记下来。
conn api
conn api
class _virConn(): def __init__(self,*argv): try: self.__conn = libvirt.open('qemu+ssh://root@{}/system'.format(argv[0])) __string = "conn sys local success" log(__string) except Exception,msg: log(msg) #get the host run all domain,and return a list, the fun() must host,user def _getAllDomains(self,): try: __getList = self.__conn.listAllDomains() return [x.name() for x in __getList] except Exception,msg: log(msg) return False def _getVolObj(self,path): try: obj = self.__conn.storageVolLookupByPath(path=path) return obj except Exception,msg: log(msg) def _defineXML(self,xml): try: self.__conn.defineXML(xml) except Exception,msg: log(msg) return #entry a domain info (name,uuid,id) return the domain object def _getDomainObject(self,__str,__type): try: if __type == 'id': __object = self.__conn.lookupByID(__str) return __object if __type == 'uuid': __object = self.__conn.lookupByUUIDString(__str) return __object if __type == 'name': __object = self.__conn.lookupByName(__str) return __object except Exception,msg: log(msg) return False def _getStoragePoolObject(self, __str, __type): try: if __type == 'name': __object = self.__conn.storagePoolLookupByName(__str) if __type == 'uuid': __object = self.__conn.storagePoolLookupByUUidString(__str) return __object except Exception,msg: log(msg) def _getInterfaceObject(self,__str,__type): try: if __type == 'mac': __object = self.__conn.interfaceLookupByMACString(__str) return __object if __type == 'name': __object = self.__conn.interfaceLookupByName(__str) return __object except Exception,msg: log(msg) return False def __getDomainId(self,__str): try: __object = self.__conn.listDomainsID() return __object except Exception,msg: log(msg) return False def _getHostname(self): try: __object = self.__conn.getHostname() return __object except Exception,msg: log(msg) def _getVersion(self): try: __ob = self.__conn.getVersion() return __ob except Exception,msg: log(msg) def _getMemoryStats(self): try: __ob = self.__conn.getMemoryStats(cellNum=-1) #get host memory status cellNum=-1 get free,total.cache,swap,cellNum=0 get free,total return __ob except Exception,msg: log(msg) def _getNetworks(self): try: __ob = self.__conn.listAllNetworks(flags=0) if __ob: return [x.name() for x in __ob] #get host all network bridge name except Exception,msg: log(msg) def _getInterfaces(self): try: __ob = self.__conn.listAllInterfaces(flags=0) return [x.name() for x in __ob] #get host all network interface name except Exception,msg: log(msg) def _getDevices(self): try: __ob = self.__conn.listAllDevices(flags=0) #get host all return [ x.name() for x in __ob ] except Exception,msg: log(msg) def _getStoragePools(self): try: __ob = self.__conn.listAllStoragePools(flags=0) return [ x.name() for x in __ob] except Exception,msg: log(msg)