Python libvirt.open() Examples
The following are 30
code examples of libvirt.open().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
libvirt
, or try the search function
.
Example #1
Source File: getmetrics_kvm.py From InsightAgent with Apache License 2.0 | 7 votes |
def updateDataForHost(currentDataList): # Open connection with the Hypervisor libvertConn = libvirt.open('qemu:///system') if libvertConn == None: print('Failed to open connection to ' '', file=sys.stderr) exit(1) currentTimeStamp = str(int(time.time() * 1000)) CPUDataMap = libvertConn.getCPUMap() mem = libvertConn.getFreeMemory() stats = libvertConn.getCPUStats(-1) currentDataList.append(currentTimeStamp) currentDataList.append(str(round(mem / 1048576., 2))) currentDataList.append(str(CPUDataMap[0])) currentDataList.append(str(stats['kernel'] / 10 ** 9)) currentDataList.append(str(stats['idle'] / 10 ** 9)) currentDataList.append(str(stats['user'] / 10 ** 9)) currentDataList.append(str(stats['iowait'] / 10 ** 9)) libvertConn.close()
Example #2
Source File: install_linux_cdrom.py From libvirt-test-API with GNU General Public License v2.0 | 6 votes |
def install_linux_cdrom_clean(params): """ clean testing environment """ logger = params['logger'] guestname = params.get('guestname') diskpath = params.get('diskpath', '/var/lib/libvirt/images/libvirt-test-api') conn = libvirt.open() domain_common.guest_clean(conn, guestname, logger) if os.path.exists(diskpath): os.remove(diskpath) envfile = os.path.join(HOME_PATH, 'usr/share/libvirt-test-api/config', 'global.cfg') envparser = env_parser.Envparser(envfile) cache_folder = envparser.get_value("variables", "domain_cache_folder") if os.path.exists(cache_folder + '/' + guestname + "_folder"): shutil.rmtree(cache_folder + '/' + guestname + "_folder") guest_dir = os.path.join(HOME_PATH, guestname) if os.path.exists(guest_dir): shutil.rmtree(guest_dir)
Example #3
Source File: getmetrics_kvm.py From InsightAgent with Apache License 2.0 | 6 votes |
def getVMDomains(): # Open connection with the Hypervisor libvertConn = libvirt.open('qemu:///system') if libvertConn == None: print('Failed to open connection to ' '', file=sys.stderr) exit(1) # Get the information about the various guest VMs vmIdentities = libvertConn.listDomainsID() vmDomains = [] if len(vmIdentities) == 1: vmDomains.append(libvertConn.lookupByID(vmIdentities[0])) if len(vmDomains) == 0: print('Failed to find the domain ', file=sys.stderr) exit(1) else: # Handle for multiple domains for id in vmIdentities: vmDomains.append(libvertConn.lookupByID(id)) return vmDomains
Example #4
Source File: getmetrics_kvm.py From InsightAgent with Apache License 2.0 | 6 votes |
def checkNewVMs(vmDomains): newVMNames = [] vmMetaDataFilePath = os.path.join(homePath, dataDirectory + "totalVMs.json") for vmDomain in vmDomains: newVMNames.append(vmDomain.name()) if os.path.isfile(vmMetaDataFilePath) == False: towritePreviousVM = {} towritePreviousVM["allVM"] = newVMNames with open(vmMetaDataFilePath, 'w') as vmMetaDataFile: json.dump(towritePreviousVM, vmMetaDataFile) else: with open(vmMetaDataFilePath, 'r') as vmMetaDataFile: oldVMDomains = json.load(vmMetaDataFile)["allVM"] if cmp(newVMNames, oldVMDomains) != 0: towritePreviousVM = {} towritePreviousVM["allVM"] = newVMNames with open(vmMetaDataFilePath, 'w') as vmMetaDataFile: json.dump(towritePreviousVM, vmMetaDataFile) if os.path.isfile(os.path.join(homePath, dataDirectory + date + ".csv")) == True: oldFile = os.path.join(homePath, dataDirectory + date + ".csv") newFile = os.path.join(homePath, dataDirectory + date + "." + time.strftime("%Y%m%d%H%M%S") + ".csv") os.rename(oldFile, newFile)
Example #5
Source File: connection_getMemoryParameters.py From libvirt-test-API with GNU General Public License v2.0 | 6 votes |
def connection_getMemoryParameters(params): """ test API for getMemoryParameters in class virConnect """ logger = params['logger'] uri = params.get("uri", None) fail = 0 try: conn = libvirt.open(uri) logger.info("get connection to libvirtd") param_dict = conn.getMemoryParameters() for n in node_memory: fail = check_memory_parameter(param_dict, n, logger) except libvirtError as e: logger.error("API error message: %s" % e.get_error_message()) fail = 1 return fail
Example #6
Source File: connection_security_model.py From libvirt-test-API with GNU General Public License v2.0 | 6 votes |
def connection_security_model(params): """test API for getSecurityModel""" logger = params['logger'] uri = params.get("uri", None) if 'uri' in params: conn = libvirt.open(uri) else: conn = sharedmod.libvirtobj['conn'] try: model = conn.getSecurityModel() logger.info("model : %s" % model) driver = get_security_driver(logger) if driver == model[0]: logger.info("Pass : get security model successful.") return 0 else: logger.error("Fail : get security model failed.") return 1 except libvirtError as e: logger.error("API error message: %s" % e.get_error_message()) return 1
Example #7
Source File: connection_cpu_features.py From libvirt-test-API with GNU General Public License v2.0 | 6 votes |
def connection_cpu_features(params): """test libvirt connection functions related to cpu features """ logger = params['logger'] try: if 'conn' in params: conn = libvirt.open(params['conn']) else: conn = libvirt.open() host_cpu = get_host_cpu(conn) logger.debug("Host cpu xml: " + str(host_cpu)) logger.info("Host cpu features: " + str(get_cpu_feature_set(host_cpu))) if baseline_test(conn, host_cpu, logger): return 1 if compare_test(conn, host_cpu, logger): return 1 except libvirtError as e: logger.error("API error message: %s, error code is %s" % (e.get_error_message(), e.get_error_code())) logger.error("start failed") return 1 return 0
Example #8
Source File: connection_getAllDomainStats.py From libvirt-test-API with GNU General Public License v2.0 | 6 votes |
def prepare_shutoff_daemon(logger): if not os.path.exists(hooks_dir): os.makedirs(hooks_dir) if os.path.exists(hooks_file): os.remove(hooks_file) with open(hooks_file, 'w') as f: f.write(hooks_str) if not os.access(hooks_file, os.X_OK): st = os.stat(hooks_file) os.chmod(hooks_file, st.st_mode | stat.S_IEXEC | stat.S_IXGRP | stat.S_IXOTH) cmd = "service libvirtd restart" ret, out = utils.exec_cmd(cmd, shell=True) if ret: logger.error("Restart libvirtd failed: %s" % out) return 1 return 0
Example #9
Source File: app.py From qubes-core-admin with GNU Lesser General Public License v2.1 | 6 votes |
def cpu_family_model(self): """Get CPU family and model""" if self._cpu_family is None or self._cpu_model is None: family = None model = None with open('/proc/cpuinfo') as cpuinfo: for line in cpuinfo.readlines(): line = line.strip() if not line: # take info from the first core break field, value = line.split(':', 1) if field.strip() == 'model': model = int(value.strip()) elif field.strip() == 'cpu family': family = int(value.strip()) self._cpu_family = family self._cpu_model = model return self._cpu_family, self._cpu_model
Example #10
Source File: libvirt_pool.py From see with Apache License 2.0 | 6 votes |
def image(self): path = "%s/%s" % (self.configuration.get( 'storage_pool_path').rstrip('/'), self.name.lstrip('/')) if not os.path.exists(path): raise FileNotFoundError(path) hypervisor = libvirt.open( self.configuration.get('hypervisor', 'qemu:///system')) try: volume = hypervisor.storageVolLookupByPath(path) return volume.path() except libvirt.libvirtError: pool = hypervisor.storagePoolDefineXML(POOL_CONFIG_XML.format( self.configuration.get('storage_pool_path'))) pool.setAutostart(True) pool.create() pool.refresh() return pool.storageVolLookupByName(self.name).path()
Example #11
Source File: create_with_files.py From libvirt-test-API with GNU General Public License v2.0 | 6 votes |
def create_with_files_clean(params): logger = params['logger'] for i in range(len(files)): ret = utils.del_file("/tmp/libvirt-test-api-create-file-%d" % i, logger) ret = utils.del_file("/tmp/libvirt_passfile_check", logger) conn = libvirt.open("lxc:///") dom = conn.lookupByName(guestname) guest_state = dom.info()[0] if guest_state == libvirt.VIR_DOMAIN_RUNNING: logger.debug("destroy guest: %s." % guestname) time.sleep(5) dom.destroyFlags() define_list = conn.listDefinedDomains() if guestname in define_list: time.sleep(3) dom.undefine() time.sleep(3) elif guest_state == libvirt.VIR_DOMAIN_SHUTOFF: time.sleep(5) dom.undefine() time.sleep(3)
Example #12
Source File: lxc.py From see with Apache License 2.0 | 6 votes |
def domain_create(hypervisor, identifier, configuration, network_name=None): """libvirt Domain definition. @raise: ConfigError, IOError, libvirt.libvirtError. """ mounts = [] with open(configuration['configuration']) as config_file: domain_config = config_file.read() if 'filesystem' in configuration: if isinstance(configuration['filesystem'], (list, tuple)): for mount in configuration['filesystem']: mounts.append(mountpoint(mount, identifier)) else: mounts.append(mountpoint(configuration['filesystem'], identifier)) xml_config = domain_xml(identifier, domain_config, tuple(mounts), network_name=network_name) return hypervisor.defineXML(xml_config)
Example #13
Source File: lxc.py From see with Apache License 2.0 | 6 votes |
def allocate(self): """Initializes libvirt resources.""" network_name = None self._hypervisor = libvirt.open( self.configuration.get('hypervisor', 'lxc:///')) if 'network' in self.configuration: self._network = network.create(self._hypervisor, self.identifier, self.configuration['network']) network_name = self._network.name() self._domain = domain_create(self._hypervisor, self.identifier, self.configuration['domain'], network_name=network_name) if self._network is None: self._network = network.lookup(self._domain)
Example #14
Source File: install_suse_ppc.py From libvirt-test-API with GNU General Public License v2.0 | 6 votes |
def install_suse_ppc_clean(params): """ clean testing environment """ logger = params['logger'] guestname = params.get('guestname') diskpath = params.get('diskpath', '/var/lib/libvirt/images/libvirt-test-api') conn = libvirt.open() domain_common.guest_clean(conn, guestname, logger) if os.path.exists(diskpath + '/' + guestname): os.remove(diskpath + '/' + guestname) envfile = os.path.join(HOME_PATH, 'global.cfg') envparser = env_parser.Envparser(envfile) ostree_search = params.get('guestos') + "_" + params.get('guestarch') + "_iso" ostree = envparser.get_value("guest", ostree_search) cache_folder = envparser.get_value("variables", "domain_cache_folder") + "/" +\ ostree.split("/")[-1].split(".iso")[0] if os.path.exists(cache_folder): shutil.rmtree(cache_folder) guest_dir = os.path.join(HOME_PATH, guestname) if os.path.exists(guest_dir): shutil.rmtree(guest_dir)
Example #15
Source File: checkpoint_lookup.py From libvirt-test-API with GNU General Public License v2.0 | 6 votes |
def checkpoint_lookup(params): logger = params['logger'] guestname = params['guestname'] checkpoint_name = params.get('checkpoint_name', None) logger.info("Checkpoint name: %s" % checkpoint_name) if not utils.version_compare('libvirt-python', 5, 6, 0, logger): logger.info("Current libvirt-python don't support checkpointLookupByName().") return 0 try: conn = libvirt.open() dom = conn.lookupByName(guestname) cp = dom.checkpointLookupByName(checkpoint_name, 0) except libvirtError as err: logger.error("API error message: %s" % err.get_error_message()) return 1 # check checkpoint if cp.getName() == checkpoint_name: logger.info("PASS: check checkpoint name successful.") return 0 else: logger.error("FAIL: check checkpoint name failed.") return 1
Example #16
Source File: create_xml_with_files.py From libvirt-test-API with GNU General Public License v2.0 | 6 votes |
def create_xml_with_files_clean(params): logger = params['logger'] guestname = params['guestname'] for i in range(len(files)): ret = utils.del_file("/tmp/libvirt-test-api-create-file-%d" % i, logger) ret = utils.del_file("/tmp/libvirt_passfile_check", logger) conn = libvirt.open("lxc:///") dom = conn.lookupByName(guestname) guest_state = dom.info()[0] if guest_state == libvirt.VIR_DOMAIN_RUNNING: logger.debug("destroy guest: %s." % guestname) time.sleep(5) dom.destroyFlags() define_list = conn.listDefinedDomains() if guestname in define_list: time.sleep(3) dom.undefine() time.sleep(3) elif guest_state == libvirt.VIR_DOMAIN_SHUTOFF: time.sleep(5) dom.undefine() time.sleep(3)
Example #17
Source File: list_all_children.py From libvirt-test-API with GNU General Public License v2.0 | 6 votes |
def list_all_children(params): logger = params['logger'] guestname = params['guestname'] checkpoint_name = params['checkpoint_name'] flag = utils.parse_flags(params) if not utils.version_compare('libvirt-python', 5, 6, 0, logger): logger.info("Current libvirt-python don't support listAllChildren().") return 0 logger.info("Checkpoint name: %s" % checkpoint_name) logger.info("flag: %s" % flag) try: conn = libvirt.open() dom = conn.lookupByName(guestname) cp = dom.checkpointLookupByName(checkpoint_name) cp_lists = cp.listAllChildren(flag) for cp_list in cp_lists: logger.info("Checkpoint children list: %s" % cp_list.getName()) except libvirtError as err: logger.error("API error message: %s" % err.get_error_message()) return 1 return 0
Example #18
Source File: kvm_local_deploy.py From bubble-toolkit with Apache License 2.0 | 6 votes |
def __init__(self, debug=0, dryrun=0, force=0, marvin_config=''): self.config_section_name = None self.DEBUG = debug self.DRYRUN = dryrun self.FORCE = force self.marvin_config = marvin_config self.marvin_data = False # we can run as a user in the libvirt group # self.check_root() self.configfile = os.path.dirname(os.path.realpath(__file__)) + '/config' self.config_data = { } try: self.conn = libvirt.open('qemu:///system') except Exception as e: print("ERROR: Could not connect to Qemu!") print(e) sys.exit(1) self.print_welcome() self.read_config_file(self.configfile) # Check for root permissions
Example #19
Source File: kvm_local_deploy_v2.py From bubble-toolkit with Apache License 2.0 | 6 votes |
def load_marvin_json(self): try: print("Note: Processing Marvin config '" + self.marvin_config + "'") config_lines = [] with open(self.marvin_config) as file_pointer: for line in file_pointer: ws = line.strip() if not ws.startswith("#"): config_lines.append(ws) self.marvin_data = json.loads("\n".join(config_lines)) return True except: print("Error: loading Marvin failed") return False # Get Marvin json
Example #20
Source File: kvm_local_deploy_v2.py From bubble-toolkit with Apache License 2.0 | 6 votes |
def __init__(self, debug=0, dryrun=0, force=0, marvin_config=''): self.config_section_name = None self.DEBUG = debug self.DRYRUN = dryrun self.FORCE = force self.marvin_config = marvin_config self.marvin_data = False # we can run as a user in the libvirt group # self.check_root() self.configfile = os.path.dirname(os.path.realpath(__file__)) + '/config' self.config_data = { } try: self.conn = libvirt.open('qemu:///system') except Exception as e: print("ERROR: Could not connect to Qemu!") print(e) sys.exit(1) self.print_welcome() self.read_config_file(self.configfile) # Check for root permissions
Example #21
Source File: virt.py From dwarf with Apache License 2.0 | 6 votes |
def _create_net_xml(): """ Create a libvirt XML for the network bridge """ with open(os.path.join(os.path.dirname(__file__), 'libvirt-net.xml'), 'r') as fh: xml_template = fh.read() xml_info = { 'uuid': str(uuid.uuid4()), 'network_name': CONF.libvirt_network_name, 'bridge': CONF.libvirt_bridge_name, 'ip': CONF.libvirt_bridge_ip, 'dhcp_start': '.'.join(CONF.libvirt_bridge_ip.split('.')[0:3] + ['2']), 'dhcp_end': '.'.join(CONF.libvirt_bridge_ip.split('.')[0:3] + ['254']), } return Template(xml_template).substitute(xml_info)
Example #22
Source File: utils.py From virtualbmc with Apache License 2.0 | 6 votes |
def __enter__(self): pid = self._fork(parent_exits=False) if pid > 0: return pid os.setsid() self._fork(parent_exits=True) self._change_root_directory() self._change_file_creation_mask() sys.stdout.flush() sys.stderr.flush() si = open(os.devnull, 'r') so = open(os.devnull, 'a+') se = open(os.devnull, 'a+') os.dup2(si.fileno(), sys.stdin.fileno()) os.dup2(so.fileno(), sys.stdout.fileno()) os.dup2(se.fileno(), sys.stderr.fileno()) return pid
Example #23
Source File: utils.py From virtualbmc with Apache License 2.0 | 6 votes |
def __enter__(self): try: if self.sasl_username and self.sasl_password: def request_cred(credentials, user_data): for credential in credentials: if credential[0] == libvirt.VIR_CRED_AUTHNAME: credential[4] = self.sasl_username elif credential[0] == libvirt.VIR_CRED_PASSPHRASE: credential[4] = self.sasl_password return 0 auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_PASSPHRASE], request_cred, None] flags = libvirt.VIR_CONNECT_RO if self.readonly else 0 self.conn = libvirt.openAuth(self.uri, auth, flags) elif self.readonly: self.conn = libvirt.openReadOnly(self.uri) else: self.conn = libvirt.open(self.uri) return self.conn except libvirt.libvirtError as e: raise exception.LibvirtConnectionOpenError(uri=self.uri, error=e)
Example #24
Source File: install_common.py From libvirt-test-API with GNU General Public License v2.0 | 6 votes |
def clean_guest(guestname, logger): conn = libvirt.open(None) running_guests = [] ids = conn.listDomainsID() for id in ids: obj = conn.lookupByID(id) running_guests.append(obj.name()) if guestname in running_guests: logger.info("Destroy guest: %s" % guestname) domobj = conn.lookupByName(guestname) domobj.destroy() define_guests = conn.listDefinedDomains() if guestname in define_guests: logger.info("Undefine guest: %s" % guestname) domobj = conn.lookupByName(guestname) domobj.undefine() conn.close()
Example #25
Source File: oob.py From drydock with Apache License 2.0 | 5 votes |
def init_session(self, node): """Initialize a Libvirt session to the node hypervisor. :param node: instance of objects.BaremetalNode """ if node.oob_type != 'libvirt': raise errors.DriverError( "Node OOB type %s is not 'libvirt'" % node.oob_type) virsh_url = node.oob_parameters.get('libvirt_uri', None) if not virsh_url: raise errors.DriverError( "Node %s has no 'libvirt_url' defined" % (node.name)) url_parts = urlparse(virsh_url) if url_parts.scheme != "qemu+ssh": raise errors.DriverError( "Node %s has invalid libvirt URL scheme %s. " "Only 'qemu+ssh' supported." % (node.name, url_parts.scheme)) self.logger.debug( "Starting libvirt session to hypervisor %s " % (virsh_url)) virsh_ses = libvirt.open(virsh_url) if not virsh_ses: raise errors.DriverError( "Unable to establish libvirt session to %s." % virsh_url) return virsh_ses
Example #26
Source File: connection_getCPUMap.py From libvirt-test-API with GNU General Public License v2.0 | 5 votes |
def connection_getCPUMap(params): """test libvirt connection getCPUMap """ logger = params['logger'] try: # Get connection if 'conn' in params: conn = libvirt.open(params['conn']) else: conn = libvirt.open() result = conn.getCPUMap() expect = gen_hostcpu_online_map() except libvirtError as e: logger.error("API error message: %s, error code is %s" % e.get_error_message()) logger.error("getCPUMap failed") return 1 logger.info("Expect: " + str(expect)) logger.info("Get: " + str(result)) if result != expect: logger.error("getCPUMap fail.") return 1 return 0
Example #27
Source File: connection_getMemoryStats.py From libvirt-test-API with GNU General Public License v2.0 | 5 votes |
def getsysmem(a): return open(a[0]).read().splitlines()[a[1]].split()[a[2]]
Example #28
Source File: connection_version.py From libvirt-test-API with GNU General Public License v2.0 | 5 votes |
def connection_version(params): """test libvirt connection version """ logger = params['logger'] uri = params.get("uri", None) try: # get connection firstly. # If uri is not specified, use conn from sharedmod if 'uri' in params: conn = libvirt.open(uri) else: conn = sharedmod.libvirtobj['conn'] # check libvirt version number if not check_libvirt_ver_num(conn, logger): logger.error("Failed to check libvirt version number") return 1 # check hypervisor version number if not check_hypervisor_ver_num(conn, logger): logger.error("Failed to check hypervisor version number") return 1 except libvirtError as e: logger.error("API error message: %s, error code is %s" % e.get_error_message()) logger.error("start failed") return 1 return 0
Example #29
Source File: migrate_get_max_downtime.py From libvirt-test-API with GNU General Public License v2.0 | 5 votes |
def migrate_get_max_downtime(params): """ migrate get max downtime for a guest """ logger = params['logger'] guestname = params['guestname'] if not utils.version_compare("libvirt-python", 3, 7, 0, logger): logger.info("Current libvirt-python don't support migrateGetMaxDowntime().") return 0 try: conn = libvirt.open() dom = conn.lookupByName(guestname) downtime_limit = get_downtime_from_qemu(dom, logger) logger.info("get downtime from query-migrate-parameters: %s" % downtime_limit) downtime = dom.migrateGetMaxDowntime(0) logger.info("get downtime from migrateGetMaxDowntime: %s" % downtime) except libvirtError as e: logger.error("API error message: %s, error code: %s" % (e.get_error_message(), e.get_error_code())) return 1 if downtime == downtime_limit: logger.info("PASS: get max downtime successful.") return 0 else: logger.info("FAIL: get max downtime failed.") return 1
Example #30
Source File: checkpoint_negative.py From libvirt-test-API with GNU General Public License v2.0 | 5 votes |
def checkpoint_negative(params): logger = params['logger'] guestname = params['guestname'] checkpoint_name = params['checkpoint_name'] flags = params['flags'] if not utils.version_compare('libvirt-python', 5, 6, 0, logger): logger.info("Current libvirt-python don't support checkpoint API.") return 0 logger.info("Checkpoint name: %s" % checkpoint_name) logger.info("Flags: %s" % flags) try: conn = libvirt.open() dom = conn.lookupByName(guestname) cp = dom.checkpointLookupByName(checkpoint_name) if flags == 'invalid': cp.__del__() cp.getName() elif flags == 'no_domain': cp.getParent() except libvirtError as err: logger.error("API error message: %s" % err.get_error_message()) if flags == 'invalid' and err.get_error_code() == 102: logger.info("Negative test PASS: test VIR_ERR_INVALID_DOMAIN_CHECKPOINT successful.") elif flags == 'no_domain' and err.get_error_code() == 103: logger.info("Negative test PASS: test VIR_ERR_NO_DOMAIN_CHECKPOINT successful.") else: logger.error("Negative test FAIL: error code: %s" % err.get_error_code()) return 1 return 0