Beispiel #1
0
def libvirt_domain_get_mac(vm_title):
    mac = None

    conn = libvirt.openReadOnly(None)
    doms = conn.listAllDomains()
    for dom in doms:
        try:
            dom0 = conn.lookupByName(dom.name())
            # Annoyiingly, libvirt prints its own error message here
        except libvirt.libvirtError:
            print("Domain %s is not running" % name)
        ctx = libvirt_domain_get_context(dom0)
        if libvirt_domain_get_val(ctx, "/domain/title") == vm_title:
            return libvirt_domain_get_val(ctx, "/domain/devices/interface/mac/@address")

    conn = libvirt.openReadOnly('qemu:///system')
    doms = conn.listAllDomains()
    for dom in doms:
        try:
            dom0 = conn.lookupByName(dom.name())
            # Annoyiingly, libvirt prints its own error message here
        except libvirt.libvirtError:
            print("Domain %s is not running" % name)
        ctx = libvirt_domain_get_context(dom0)
        if libvirt_domain_get_val(ctx, "/domain/name") == vm_title:
            return libvirt_domain_get_val(ctx, "/domain/devices/interface/mac/@address")
    return mac
Beispiel #2
0
def libvirt_domain_get_mac(vm_title):
    mac = None

    conn = libvirt.openReadOnly(None)
    doms = conn.listAllDomains()
    for dom in doms:
        try:
            dom0 = conn.lookupByName(dom.name())
            # Annoyiingly, libvirt prints its own error message here
        except libvirt.libvirtError:
            print("Domain %s is not running" % name)
        ctx = libvirt_domain_get_context(dom0)
        if libvirt_domain_get_val(ctx, "/domain/title") == vm_title:
            return libvirt_domain_get_val(
                ctx, "/domain/devices/interface/mac/@address")

    conn = libvirt.openReadOnly('qemu:///system')
    doms = conn.listAllDomains()
    for dom in doms:
        try:
            dom0 = conn.lookupByName(dom.name())
            # Annoyiingly, libvirt prints its own error message here
        except libvirt.libvirtError:
            print("Domain %s is not running" % name)
        ctx = libvirt_domain_get_context(dom0)
        if libvirt_domain_get_val(ctx, "/domain/name") == vm_title:
            return libvirt_domain_get_val(
                ctx, "/domain/devices/interface/mac/@address")
    return mac
Beispiel #3
0
    def init_state(self):
        """Initialize a dict for storing the state of the local manager.

        :param config: A config dictionary.
         :type config: dict(str: *)

        :return: A dictionary, initial state of the local manager.
         :rtype: dict
        """
        vir_connection = libvirt.openReadOnly(None)
        if vir_connection is None:
            message = 'Failed to open a connection to the hypervisor'
            LOG.critical(message)
            raise OSError(message)

        physical_cpu_mhz_total = int(
            common.physical_cpu_mhz_total(vir_connection) *
            CONF.host_cpu_usable_by_vms)
        return {'previous_time': 0.,
                'vir_connection': vir_connection,
                'db': db_utils.init_db(),
                'physical_cpu_mhz_total': physical_cpu_mhz_total,
                'hostname': vir_connection.getHostname(),
                'hashed_username': sha1(CONF.os_admin_user).hexdigest(),
                'hashed_password': sha1(CONF.os_admin_password).hexdigest()}
Beispiel #4
0
    def getConnection(self, readOnly = False):
        if not self._connection or self.readOnly != readOnly:
            if self._connection: self._connection.close()

            self._connection = None
            error = None

            uri = self.node.getURI()

            try:
                if readOnly:
                    self._connection = libvirt.openReadOnly(uri)
                else:
                    self._connection = libvirt.open(uri)
                #endif
            except libvirt.libvirtError, e:
                error = unicode(e)
            #endtry

            if not self._connection and not error:
                error = u"Failed to open connection to the hypervisor"
            #endif

            if error:
                logging.error("libvirt: %s" % error)
                if self._connection: self._connection.close()
                self._connection = None
                raise CantConnectException(error)
            #endif

            self.readOnly = readOnly
            return self._connection
Beispiel #5
0
def _is_host_domain(fail_on_error=False):
    """
    This function returns true if this system is currently a host domain.
    Simply having virtualization enabled is sufficient.

    We can figure out if Xen/Qemu is running by checking for the type
    """
    if vdsm_enabled:
        # since vdsm is enabled, lets move further and
        # see what we get
        return True
    if not libvirt:
        # No libvirt, dont bother with the rest
        return False
    try:
        conn = libvirt.openReadOnly(None)
    except libvirt.libvirtError:  # libvirtd is not running
        sys.stderr.write(
            utf8_encode(
                _("Warning: Could not retrieve virtualization information!\n\tlibvirtd service needs to be running.\n"
                  )))
        if fail_on_error:
            sys.exit(1)
        return False
    if conn and conn.getType() in ['Xen', 'QEMU']:
        return True
    return False
Beispiel #6
0
    def add_hyp_to_receive_events(self, hyp_id):
        d_hyp_parameters = get_hyp_hostname_user_port_from_id(hyp_id)
        hostname = d_hyp_parameters['hostname']
        user = d_hyp_parameters.get('user', 'root')
        port = d_hyp_parameters.get('port', 22)

        uri = hostname_to_uri(hostname, user=user, port=port)
        conn_ok = False
        try:
            self.hyps_conn[hyp_id] = libvirt.openReadOnly(uri)
            logs.status.debug(
                '####################connection to {} ready in events thread'.
                format(hyp_id))
            update_uri_hyp(hyp_id, uri)
            conn_ok = True
        except Exception as e:
            logs.status.error(
                'libvirt connection read only in events thread in hypervisor: {}'
                .format(hyp_id))
            logs.status.error(e)

        if conn_ok is True:
            self.events_ids[hyp_id] = self.register_events(
                self.hyps_conn[hyp_id])
            self.hyps[hyp_id] = hostname
Beispiel #7
0
def discoverInterfaces():
    
    # TODO: Actually, may need to try a variety of uri combinations
    
    uri = 'qemu+ssh://stack@kiwi-os-compute-01/system'
    
    conn = libvirt.openReadOnly(uri)
    if conn == None:
        print 'Failed to open connection to the hypervisor'
        return
    
    try:
        networks = conn.listAllNetworks()
        interfaces = conn.listAllInterfaces()
        domains = conn.listAllDomains()
        
        dom0 = conn.lookupByName("Domain-0")
    except:
        print 'Failed to find the main domain'
        sys.exit(1)
    
    print "Domain 0: id %d running %s" % (dom0.ID(), dom0.OSType())
    print dom0.info()
    
    pass
    def get_inventory(self):
        ''' Construct the inventory '''

        inventory = dict(_meta=dict(hostvars=dict()))

        conn = libvirt.openReadOnly(self.libvirt_uri)
        if conn is None:
            print "Failed to open connection to %s" % self.libvirt_uri
            sys.exit(1)

        domains = conn.listAllDomains()
        if domains is None:
            print "Failed to list domains for connection %s" % self.libvirt_uri
            sys.exit(1)

        for domain in domains:
            hostvars = dict(libvirt_name=domain.name(),
                            libvirt_id=domain.ID(),
                            libvirt_uuid=domain.UUIDString())
            domain_name = domain.name()

            # TODO: add support for guests that are not in a running state
            state, _ = domain.state()
            # 2 is the state for a running guest
            if state != 1:
                continue

            hostvars['libvirt_status'] = 'running'

            root = ET.fromstring(domain.XMLDesc())
            ansible_ns = {'ansible': 'https://github.com/ansible/ansible'}
            for tag_elem in root.findall('./metadata/ansible:tags/ansible:tag', ansible_ns):
                tag = tag_elem.text
                _push(inventory, "tag_%s" % tag, domain_name)
                _push(hostvars, 'libvirt_tags', tag)

            # TODO: support more than one network interface, also support
            # interface types other than 'network'
            interface = root.find("./devices/interface[@type='network']")
            if interface is not None:
                source_elem = interface.find('source')
                mac_elem = interface.find('mac')
                if source_elem is not None and \
                   mac_elem    is not None:
                    # Adding this to disable pylint check specifically
                    # ignoring libvirt-python versions that
                    # do not include DHCPLeases
                    # This is needed until we upgrade the build bot to
                    # RHEL7 (>= 1.2.6 libvirt)
                    # pylint: disable=no-member
                    dhcp_leases = conn.networkLookupByName(source_elem.get('network')) \
                                      .DHCPLeases(mac_elem.get('address'))
                    if len(dhcp_leases) > 0:
                        ip_address = dhcp_leases[0]['ipaddr']
                        hostvars['ansible_ssh_host'] = ip_address
                        hostvars['libvirt_ip_address'] = ip_address

            inventory['_meta']['hostvars'][domain_name] = hostvars

        return inventory
Beispiel #9
0
def new_libvirt_connection(conf):
    if not libvirt:
        raise ImportError("python-libvirt module is missing")
    uri = (conf.libvirt_uri or LIBVIRT_PER_TYPE_URIS.get(conf.libvirt_type,
                                                         'qemu:///system'))
    LOG.debug('Connecting to libvirt: %s', uri)
    return libvirt.openReadOnly(uri)
Beispiel #10
0
    def add_hyp_to_receive_events(self, hyp_id):
        d_hyp_parameters = get_hyp_hostname_user_port_from_id(hyp_id)
        hostname = d_hyp_parameters['hostname']
        user = d_hyp_parameters.get('user', 'root')
        port = d_hyp_parameters.get('port', 22)

        uri = hostname_to_uri(hostname, user=user, port=port)
        conn_ok = False
        try:
            self.hyps_conn[hyp_id] = libvirt.openReadOnly(uri)
            logs.status.debug(
                '####################connection to {} ready in events thread'.
                format(hyp_id))
            update_uri_hyp(hyp_id, uri)
            conn_ok = True
        except Exception as e:
            logs.status.error(
                'libvirt connection read only in events thread in hypervisor: {}'
                .format(hyp_id))
            logs.status.error(e)

        if conn_ok is True:
            for i in range(NUM_TRY_REGISTER_EVENTS):
                #try 5
                try:
                    self.events_ids[hyp_id] = self.register_events(
                        self.hyps_conn[hyp_id])
                    self.hyps[hyp_id] = hostname
                    break
                except libvirt.libvirtError as e:
                    logs.status.error(
                        f'Error when register_events, wait {SLEEP_BETWEEN_TRY_REGISTER_EVENTS}, try {i+1} of {NUM_TRY_REGISTER_EVENTS}'
                    )
                    logs.status.error(e)
                time.sleep(SLEEP_BETWEEN_TRY_REGISTER_EVENTS)
Beispiel #11
0
def find_libvirt(env_name):
    """Look for environments qemu images and counting space
    they occupy on file system using by libvirt API

    :param env_name: fuel-devops environment name
    :type: env_name: string

    :return: sum of images sizes in Mbytes
    :rtype: int
    """
    conn = libvirt.openReadOnly('qemu:///system')
    if conn is None:
        raise RuntimeError('Failed to open connection to the hypervisor')
    list_domains = conn.listDefinedDomains()
    r_dom=re.compile(env_name)
    summ = 0
    if filter(r_dom.match, list_domains):
        for domain in list_domains:
            if env_name in domain:
                # getting configuration of libvirt domain in xml format
                # result is identical to output ``virsh dumpxml domain``
                raw_xml = conn.lookupByName(domain).XMLDesc(0)
                xml = minidom.parseString(raw_xml)
                sourceFiles = xml.getElementsByTagName('source')
                for sourceFile in sourceFiles:
                    if sourceFile.getAttribute('file'):
                        summ += os.path.getsize(sourceFile.getAttribute('file'))
        # return used disc space in Mbytes
        conn.close()
        return summ/1024/1024
    else:
        conn.close()
        raise RuntimeError("Libvirt domains are not exist")
    def meter(self):
        """
        Returns a list of (UUID, timestamp, virmem-attr) tuples, one for each
        instance running on a specific host.
        """
        virmem = []

        try:
            # dict of (uuid: (pid, instance-name)) elements
            inst_ids = self._get_inst_ids()
            # list of (uuid, timestamp, virmem [in bytes], virmem usage [in
            # pct of total]) tuples
            virmem = [(uuid,
                       datetime.now(),
                       mem_info.vms,
                       float(mem_info.vms) / self.psutil_smem.total * 100)
                      for (uuid, mem_info) \
                          in [(k, psutil.Process(v[0]).get_memory_info()) \
                              for k, v in inst_ids.iteritems()]]

        except:
            # Warning! Fails silently...
            logger.exception('Inst_VIRMEM_Usage: '
                             'Connection to hypervisor failed; reset.')
            self.conn = libvirt.openReadOnly(_LIBVIRT_SOCKET_URL)

        finally:
            return virmem
Beispiel #13
0
 def init_host(self):
     """
     获取本地主机名;
     """
     local_host = ''
     context = xdrs.context.get_admin_context()
     self.vms_api = vms.API()
     
     try:
         vms_metadata = self.vms_api.get_all_vms_metadata(context)
     except exception.VmMetadataNotFound:
         msg = _('vm metadata not found')
         raise webob.exc.HTTPBadRequest(explanation=msg)
     
     if vms_metadata is None:
         vir_connection = libvirt.openReadOnly(local_host)
         vms_current = self._get_current_vms(vir_connection)
         
         for vm_uuid in vms_current:
             vm_create_values = {
                                 'host_id': local_host,
                                 'vm_id': vm_uuid,
                                 'vm_state': 'vm_normal'
                                 }
             
             vm_metadata = self.conductor_api.create_vm_metadata(context, vm_create_values)
Beispiel #14
0
    def init_state(self):
        """ Initialize a dict for storing the state of the local manager.

        :param config: A config dictionary.
         :type config: dict(str: *)

        :return: A dictionary containing the initial state of the local manager.
         :rtype: dict
        """
        vir_connection = libvirt.openReadOnly(None)
        if vir_connection is None:
            message = 'Failed to open a connection to the hypervisor'
            LOG.critical(message)
            raise OSError(message)

        physical_cpu_mhz_total = int(
            common.physical_cpu_mhz_total(vir_connection) *
            CONF.host_cpu_usable_by_vms)
        return {'previous_time': 0.,
                'vir_connection': vir_connection,
                'db': db_utils.init_db(),
                'physical_cpu_mhz_total': physical_cpu_mhz_total,
                'hostname': vir_connection.getHostname(),
                'hashed_username': sha1(CONF.os_admin_user).hexdigest(),
                'hashed_password': sha1(CONF.os_admin_password).hexdigest()}
Beispiel #15
0
def createConnection():
    conn = libvirt.openReadOnly(None)
    if conn == None:
        print 'Failed to open connection to QEMU/KVM'
    else:
        print '----Connection is created successfully----'
        return conn
Beispiel #16
0
    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)
Beispiel #17
0
 def connection(self):
     if not self.__conn:
         if libvirt and os.path.exists(LIBVIRT_RO_SOCK_NAME):
             try:
                 self.__conn = libvirt.openReadOnly(None)
             except:
                 self.__conn = None
                 self.log(
                     "error in openReadOnly(None): {}".format(
                         process_tools.get_except_info()),
                     logging_tools.LOG_LEVEL_CRITICAL)
             else:
                 if os.getuid():
                     self.log(
                         "not running as root ({:d} != 0)".format(
                             os.getuid()), logging_tools.LOG_LEVEL_ERROR)
         else:
             if not self.__missing_logged:
                 self.__missing_logged = True
                 self.log(
                     "no libvirt defined or socket {} not found".format(
                         LIBVIRT_RO_SOCK_NAME),
                     logging_tools.LOG_LEVEL_ERROR)
             self.__conn = None
     return self.__conn
Beispiel #18
0
def init_state():
    """ 
    Initialize a dict for storing the state of the data collector.
    """
    """
    @@@@注:这里可以放在xdrs-host的初始化过程中来实现,并写入数据表HostInitData;
    这里的操作就是对这个数据表进行数据的初始化操作;
    其实这个系统有个瓶颈,就是若干数据库访问量过大,根据云平台集群规模和所建立的
    虚拟机数量的大小,可能同时会有大量的数据表更新等操作访问数据库。
    db.update_host(hostname,
                   int(host_cpu_mhz * host_cpu_usable_by_vms),
                   physical_cpus,
                   host_ram)
    """
    hosts_api = hosts.API()

    vir_connection = libvirt.openReadOnly(None)
    hostname = vir_connection.getHostname()
    """
    获取本地主机总的CPU MHZ和RAM数据;
    """
    host_cpu_mhz, host_ram = get_host_characteristics(vir_connection)
    """
    通过libvirt获取物理CPU的数目;
    """
    physical_cpus = physical_cpu_count(vir_connection)
    """
    主机中可以分配给虚拟机使用的cpu个数占总体cpu的百分比(阈值);
    """
    host_cpu_usable_by_vms = float(CONF.host_cpu_usable_by_vms)
    local_cpu_mhz = int(host_cpu_mhz * host_cpu_usable_by_vms)
    host_cpu_overload_threshold = float(CONF.host_cpu_overload_threshold) * \
                                host_cpu_usable_by_vms
    physical_core_mhz = host_cpu_mhz / physical_cpus
    host_id = 2
    """在系统初始化的过程中随机生成;"""

    init_data = {
        'host_name': hostname,
        'host_id': host_id,
        'local_cpu_mhz': local_cpu_mhz,
        'physical_cpus': physical_cpus,
        'host_ram': host_ram,
        'previous_time': 0.,
        'previous_cpu_time': dict(),
        'previous_cpu_mhz': dict(),
        'previous_host_cpu_time_total': 0.,
        'previous_host_cpu_time_busy': 0.,
        'previous_overload': -1,
        'host_cpu_overload_threshold': host_cpu_overload_threshold,
        'physical_cpu_mhz': host_cpu_mhz,
        'physical_core_mhz': physical_core_mhz
    }

    try:
        host_init_data = hosts_api.create_host_init_data(init_data)
    except exception.HostInitDataNotFound:
        msg = _('host init data not found')
        raise webob.exc.HTTPBadRequest(explanation=msg)
    """
Beispiel #19
0
def _delete_network_vm(args):
    """
    Deletes a libvirt network

    Parameters
    -----------
    args :
        dict as the one returned by argparse.ArgumentParser().parse_args()
        expected key :
            net : the IP of the vNIC to be used.
    Returns
    -------
        int
            The return value provided by the kvm storage pool create
            call, 0 on success, 1 otherwise.
    """
    libvirtConn = libvirt.openReadOnly(None)
    if libvirtConn is None:
        print('Cannot contact hypervisor', file=sys.stderr)
        return 1
    net = None
    try:
        net = libvirtConn.networkLookupByName(args.network_name)
    except libvirt.libvirtError:
        print('Cannot find network named [%s]' % args.network_name, file=sys.stderr)
        return 1
    print('Network found:\n')
    print(xml.dom.minidom.parseString(net.XMLDesc()).toprettyxml(indent=" ", newl=''))
    print('')

    if input('Really destroy this network ?').strip().lower() in ('y', 'yes'):
        return oci_utils.kvm.virt.delete_virtual_network(network_name=args.network_name)
    return 1
Beispiel #20
0
def checkanyrunningvm(hostip):
    import libvirt
    #print "\nLooking for any running vms on "+hostip+"\n-----------------------------------\n"
    found = False
    if not checkhoststatus(hostip):
        print "\nHost is down\n"
        return False
    try:
        conn = libvirt.openReadOnly('qemu+ssh://root@' + hostip + '/system')
        domains = []
        ids = conn.listDomainsID()
        for id in ids:
            domains.append(conn.lookupByID(id))
        names = conn.listDefinedDomains()
        for name in names:
            domains.append(conn.lookupByName(name))
        for dom in domains:
            print "Checking " + str(dom.name())
            if (dom.info()[0] != 5):
                #print dom.name()+" is not off. Please check"
                found = True
        dom = None
        ids = None
        names = None
        domains = None
        conn.close()
    except:
        import sys, traceback
        found = True
        etype, value, tb = sys.exc_info()
        msg = ''.join(traceback.format_exception(etype, value, tb, 10))
        print "Some Error Occured\n" + msg
    return found
Beispiel #21
0
  def openReadOnly():
    """Open a read-only libvirt connection to the qemu hypervisor.

    Returns:
      A read-only libvirt.virConnect connection to qemu.
    """
    return libvirt.openReadOnly('qemu:///system')
Beispiel #22
0
def movealldeadvms(hostip):
    import libvirt, commands
    print "\nMoving all dead vms of this host " + hostip + "\n-----------------------------------\n"
    if not checkhoststatus(hostip):
        print "\nHost is down\n"
        return
    if (checkanyrunningvm(hostip)):
        print "All the vms on this host are not Off"
        return
    try:
        conn = libvirt.openReadOnly('qemu+ssh://root@' + hostip + '/system')
        domains = []
        host1 = db(db.host.status == 1).select()[0]
        ids = conn.listDomainsID()
        for id in ids:
            domains.append(conn.lookupByID(id))
        names = conn.listDefinedDomains()
        for name in names:
            domains.append(conn.lookupByName(name))
        for dom in domains:
            print "Moving " + str(dom.name()) + " to " + host1.name
            migrateoffdomain(dom.name(), host1.id)
        print "All the vms moved Successfully. Host is empty"
        print commands.getstatusoutput("ssh root@" + hostip +
                                       " virsh list --all")
        domains = None
        names = None
        ids = None
        conn.close()
    except:
        import sys, traceback
        etype, value, tb = sys.exc_info()
        msg = ''.join(traceback.format_exception(etype, value, tb, 10))
        print "Some Error Occured\n" + msg
    return
Beispiel #23
0
def virshlist(*states):

    libvirt_states = {
        "running": libvirt.VIR_DOMAIN_RUNNING,
        "shutoff": libvirt.VIR_DOMAIN_SHUTOFF,
        "shutdown": libvirt.VIR_DOMAIN_SHUTDOWN,
        "paused": libvirt.VIR_DOMAIN_PAUSED,
        "nostate": libvirt.VIR_DOMAIN_NOSTATE,
        "blocked": libvirt.VIR_DOMAIN_BLOCKED,
        "crashed": libvirt.VIR_DOMAIN_CRASHED,
    }

    searchstates = list(libvirt_states[state] for state in states)

    conn = libvirt.openReadOnly(driver)
    if conn == None:
        print "Failed to open connection to the hypervisor"
        sys.exit(3)

    alldomains = map(conn.lookupByID, conn.listDomainsID())
    alldomains += map(conn.lookupByName, conn.listDefinedDomains())

    domains = []
    for domain in alldomains:
        if domain.info()[0] in searchstates:
            domains.append(domain.name())

    conn.close()

    return domains
Beispiel #24
0
    def __init__(self, **args):
        Machine.__init__(self, **args)

        self.run_dir = os.path.join(self.test_dir, "run")

        self._image_image = os.path.join(self.run_dir, "%s.qcow2" % (self.image))
        self._image_additional_iso = os.path.join(self.run_dir, "%s.iso" % (self.image))

        self._images_dir = os.path.join(self.test_data, "images")
        self._image_original = os.path.join(self._images_dir, "%s.qcow2" % (self.image))
        self._iso_original = os.path.join(self._images_dir, "%s.iso" % (self.image))
        self._checksum_original = os.path.join(self._images_dir, "%s-checksum" % (self.image))

        self._fixed_mac_flavors = self._get_fixed_mac_flavors()
        self._network_description = etree.parse(open("./guest/network-cockpit.xml"))

        # it is ESSENTIAL to register the default implementation of the event loop before opening a connection
        # otherwise messages may be delayed or lost
        libvirt.virEventRegisterDefaultImpl()
        self.virt_connection = libvirt.open("qemu:///session")
        self.event_handler = VirtEventHandler(libvirt_connection=self.virt_connection, verbose=self.verbose)

        # network names are currently hardcoded into network-cockpit.xml
        self.network_name = self._read_network_name()
        self.system_connection = libvirt.openReadOnly("qemu:///system")
        self.dhcp_net = self.system_connection.networkLookupByName(self.network_name)

        # we can't see the network itself as non-root, create it using vm-prep as root

        # Unique identifiers for hostnet config
        self._hostnet = 8

        # init variables needed for running a vm
        self._cleanup()
def virshdomstate(domain):

    libvirt_states={ libvirt.VIR_DOMAIN_RUNNING  : 'running',
                     libvirt.VIR_DOMAIN_SHUTOFF  : 'shut off',
                     libvirt.VIR_DOMAIN_SHUTDOWN : 'shut down',
                     libvirt.VIR_DOMAIN_PAUSED   : 'paused',
                     libvirt.VIR_DOMAIN_NOSTATE  : 'no state',
                     libvirt.VIR_DOMAIN_BLOCKED  : 'blocked',
                     libvirt.VIR_DOMAIN_CRASHED  : 'crashed',
    }

    conn = libvirt.openReadOnly('qemu:///system')
    if conn == None:
       print 'Failed to open connection to the hypervisor'
       sys.exit(3)

    try:
        dom = (conn.lookupByName (domain))
    except libvirt.libvirtError:
        return None

    state = libvirt_states[dom.info()[0]]
    conn.close()

    return state
Beispiel #26
0
def old_list_all():
    try:
        import libvirt,commands,string
        vmlist = []
        notreachable = "Error connecting "
        hosts = db(db.host.id >=0).select()
        for host in hosts:
            domains=[]    #will contain the domain information of all the available domains
            vdomains=[]
            ip = host.ip
            try: 
                #Establish a read only remote connection to libvirtd
                #find out all domains running and not running
                #Since it might result in an error add an exception handler
                conn = libvirt.openReadOnly('qemu+ssh://root@'+ip+'/system')
                ids = conn.listDomainsID()
                for id in ids:
                    domains.append(conn.lookupByID(id))
                names = conn.listDefinedDomains()
                for name in names:
                    domains.append(conn.lookupByName(name))                    
                for dom in domains:
                    vdomains.append(dom)
                for dom in vdomains:
                    intstate = dom.info()[0]
                    if(intstate==0):  state="No_State"
                    elif(intstate==1):state="Running"
                    elif(intstate==2):state="Blocked"
                    elif(intstate==3):state="Paused"
                    elif(intstate==4):state="Being_Shut_Down"
                    elif(intstate==5):state="Off"
                    elif(intstate==6):state="Crashed"
                    else: state="Unknown"           
                    name = dom.name()
                    vm = db(db.vm.name == name).select()
                    if len(vm)==1:
                        vm = vm[0]
                        if vm.hostid == host.id:
                            hostid = vm.hostid
                            ram = vm.RAM
                            sport = vm.sport
                            hostip = ip
                            vmid = vm.id
                            owner = db(db.auth_user.id == vm.userid).select(db.auth_user.username)[0].username
                            element = {'name':name,'RAM':ram,'hostip':hostip,'owner':owner,'sport':sport,'state':state, 'id':vmid}
                            vmlist.append(element)
                    dom=""
                conn.close()
            except:notreachable = notreachable+ip+' '
        #if notreachable == "Error connecting ":
        #    response.flash = "All hosts active"
        #else:
        #    response.flash = notreachable
        return dict(vmlist=vmlist,flash=response.flash)
    except:
        import sys, traceback
        etype, value, tb = sys.exc_info()
        msg = ''.join(traceback.format_exception(etype, value, tb, 10))
        session.flash=msg
        redirect(URL(c='default', f='error'))
Beispiel #27
0
def virshlist(*states):

    libvirt_states={ 'running'  : libvirt.VIR_DOMAIN_RUNNING,
                     'shutoff'  : libvirt.VIR_DOMAIN_SHUTOFF,
                     'shutdown' : libvirt.VIR_DOMAIN_SHUTDOWN,
                     'paused'   : libvirt.VIR_DOMAIN_PAUSED,
                     'nostate'  : libvirt.VIR_DOMAIN_NOSTATE,
                     'blocked'  : libvirt.VIR_DOMAIN_BLOCKED,
                     'crashed'  : libvirt.VIR_DOMAIN_CRASHED,
    }

    searchstates = list(libvirt_states[state] for state in states)

    conn = libvirt.openReadOnly('qemu:///system')
    if conn == None:
       print 'Failed to open connection to the hypervisor'
       sys.exit(3)

    alldomains = map(conn.lookupByID, conn.listDomainsID())
    alldomains += map(conn.lookupByName, conn.listDefinedDomains())

    domains = []
    for domain in alldomains:
        if domain.info()[0] in searchstates:
            domains.append(domain.name())

    conn.close()

    return domains
Beispiel #28
0
def settings():
    try:
        import libvirt
        vmname=request.args[0]
        prelim_checks(vmname)
        vminfo = db(db.vm.name == vmname).select()[0]
        hostip=db(db.host.id==vminfo.hostid).select()[0].ip
        conn = libvirt.openReadOnly('qemu+ssh://root@'+hostip+'/system')
        dom = conn.lookupByName(vmname)
        state=vminfotostate(dom.info()[0])
        data={'name':vmname,'hdd':vminfo.HDD,'ram':vminfo.RAM,'vcpus':vminfo.vCPU,'status':state,'hostip':vminfo.hostid.ip,'port':vminfo.sport,'ostype':vminfo.templateid.ostype}
        users=[]
        vmid=db(db.vm.name==vmname).select(db.vm.id)[0]['id']
        uids=db(db.vmaccpermissions.vmid==vmid).select(db.vmaccpermissions.userid)
        for uid in uids:
            uid=uid['userid']
            users.append(db(db.auth_user.id==uid).select(db.auth_user.username)[0]['username'])
        dom=""
        conn.close()
        return dict(data=data,users=users)
    except:
        import sys, traceback
        etype, value, tb = sys.exc_info()
        msg = ''.join(traceback.format_exception(etype, value, tb, 10))
        session.flash=msg
        redirect(URL(c='default', f='error'))
Beispiel #29
0
def unique_domain(domain, host=[]):
    if len(host) > 0:
        for h in host:
            conn = libvirt.openReadOnly("qemu+ssh://" + h + "/system")
            if domain_defined(conn, domain):
                conn.close()
                return False

            conn.close()
    else:
        conn = libvirt.openReadOnly(None)
        defined =  domain_defined(conn, domain)
        conn.close()
        return not defined

    return True
 def libvirt_memory(self):
   logger.debug('Collecting memory metrics')
   try:
     conn = libvirt.openReadOnly()
     if conn is None:
       raise Exception('Failed to open connection to the hypervisor')
     points = []
     for id in conn.listDomainsID():
       dom = conn.lookupByID(id)
       points.append({
         "measurement": "libvirt_memory",
         "tags": {
           "project_uuid": self.nova_metadata(dom)['project']['uuid'],
           "project_name": self.nova_metadata(dom)['project']['name'],
           "instance_uuid": dom.UUIDString(),
           "libvirt_name": dom.name(),
           "libvirt_domid": dom.ID()
         },
         "time": self.now(),
         "fields": {
           "actual": dom.memoryStats()['actual'],
           "rss": dom.memoryStats()['rss']
         }
       })
     logger.debug('Sending %s memory points to influxdb' % len(points))
     self.influxdbclient.write_points(points)
     return points
   except Exception, e:
     logger.error("error: %s" % e)
Beispiel #31
0
 def __enter__(self) -> 'ROLibvirtConnection':
     """
     Set up the connection to libvirt; this will raise its own exception
     should the connection not be possible.
     """
     self.conn = lv.openReadOnly(self.qemu)
     return self
 def libvirt_block(self):
   logger.debug('Collecting block device metrics')
   try:
     conn = libvirt.openReadOnly()
     if conn is None:
       raise Exception('Failed to open connection to the hypervisor')
     points = []
     for id in conn.listDomainsID():
       dom = conn.lookupByID(id)
       for dev in self.domain_xml(dom).findall("devices/disk/target"):
         devname = dev.get("dev")
         stats = dom.blockStats(devname)
         points.append({
           "measurement": "libvirt_block",
           "tags": {
             "project_uuid": self.nova_metadata(dom)['project']['uuid'],
             "project_name": self.nova_metadata(dom)['project']['name'],
             "instance_uuid": dom.UUIDString(),
             "libvirt_name": dom.name(),
             "libvirt_domid": dom.ID()
           },
           "time": self.now(),
           "fields": {
             "read_ops": stats[0],
             "read_bytes": stats[1],
             "write_ops": stats[2],
             "write_bytes": stats[3],
             "errors": stats[4]
           }
         })
     logger.debug('Sending %s block device points to influxdb' % len(points))
     self.influxdbclient.write_points(points)
     return points
   except Exception, e:
     logger.error("error: %s" % e)
Beispiel #33
0
def add_orphan_vm(vm_name, host_id):

    host_details = db.host[host_id]
    host_ip = host_details.host_ip.private_ip
    connection_object = libvirt.openReadOnly("qemu+ssh://root@" + host_ip +
                                             "/system")
    domain = connection_object.lookupByName(vm_name)
    vm_state = domain.info()[0]
    vm_status = vm_state_map[vm_state]
    # Parse domain XML to get information about VM
    root = etree.fromstring(domain.XMLDesc(0))

    ram_elem = root.xpath('memory')[0]
    ram_in_kb = int(ram_elem.text)
    ram_in_mb = int(round(int(ram_in_kb) / (1024), 0))

    cpu_elem = root.xpath('vcpu')[0]
    cpu = int(cpu_elem.text)

    vnc_elem = root.xpath("devices/graphics[@type='vnc']")[0]
    vnc_port = vnc_elem.attrib['port']

    mac_elem = root.xpath("devices/interface[@type='network']/mac")[0]
    mac_address = mac_elem.attrib['address']

    ip_addr = db.private_ip_pool(mac_addr=mac_address)
    ip_address = None
    if ip_addr:
        ip_address = ip_addr['id']

    template_elem = root.xpath("devices/disk[@type='file']/source")[0]
    template_file = template_elem.attrib['file']

    command = "qemu-img info " + template_file + " | grep 'virtual size'"
    ret = execute_remote_cmd(
        host_ip, 'root',
        command)  # Returns e.g. virtual size: 40G (42949672960 bytes)
    hdd = int(ret[ret.index(':') + 1:ret.index('G ')].strip())

    security_domain_row = db.security_domain(vlan=ip_addr['vlan'])

    db.vm_data.insert(
        vm_name=vm_name,
        vm_identity=(vm_name),
        RAM=ram_in_mb,
        HDD=hdd,
        extra_HDD=0,
        vCPU=cpu,
        host_id=host_id,
        template_id=1,  #TBD
        datastore_id=1,  #TBD
        owner_id=SYSTEM_USER,
        requester_id=SYSTEM_USER,
        private_ip=ip_address,
        vnc_port=vnc_port,
        purpose='Added by System',
        security_domain=security_domain_row['id'],
        status=vm_status)

    return
Beispiel #34
0
def view_snapshots():
    vm_name=request.args[0]
    prelim_checks(vm_name)
    import libvirt
    from lxml import etree #for parsing xml trees
    from StringIO import StringIO
    
    host = db(db.vm.name == vm_name).select()
    ip = host[0].hostid.ip
    conn = libvirt.openReadOnly('qemu+ssh://root@'+ip+'/system')
    dom = conn.lookupByName(vm_name)
    snapshots=dom.snapshotListNames(0)
    snap_info=[]
    for snap in snapshots:
        snapinstance = dom.snapshotLookupByName(snap,0)
        tree =  etree.parse(StringIO(snapinstance.getXMLDesc(0)))
        snap_info.append(dict(name=tree.findtext("name"),state=tree.findtext("state"),parent=tree.findtext("parent/name"),time=tree.findtext("creationTime"),status=tree.findtext("state")))
        #snapshot['name']=tree.findtext("name")
        #snapshot['state']=tree.findtext("state")
        #snapshot['parent']=tree.findtext("parent/name")
        #snapshot['created']=tree.findtext("creationTime")
    for snap_name in snap_info:
        status=True
        for snap_parent in snap_info:
            if (snap_name['name']==snap_parent['parent']) :
                status=False
        if (status==False) :
            snap_name['canDelete']=False
        else :
            snap_name['canDelete']=True
    return dict(snapshots=snap_info)
Beispiel #35
0
 def __get_libvirt_connection(self):
     " get libvirt connection"
     conn = libvirt.openReadOnly(None)
     if conn is None:
         return -1
     else:
         return conn
Beispiel #36
0
def nimo_main(nimo_mode):
    init_nimo()

    # Start and run libvirt default event loop implementation
    nimo_virt_api.startVirEventLoop()

    # In 'greenthread' nimo mode, not safe to call this,
    # but it is safe here as green thread are not yet spawned.
    # Lets log all native threads, before spawning green thread
    nimo_utils.log_native_threads()

    # In 'greenthread' nimo mode, not safe to use LOG,
    # but it is safe here as green thread are not yet spawned.
    LOG.info("Connecting to hypervisor using connection uri=%s",
             cfg.CONF.libvirt.uri)
    conn = libvirt.openReadOnly(cfg.CONF.libvirt.uri)
    if conn is None:
        LOG.error('Failed to open connection to uri=%s' % cfg.CONF.libvirt.uri)
        exit(1)

    # Register a callback to receive notifications of domain lifecycle events
    # occurring on a connection
    conn.domainEventRegisterAny(None, libvirt.VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                nimo_callbacks.domain_lifecycle_event_callback,
                                {
                                    'prog': __file__,
                                    'name': __name__
                                })

    conn.setKeepAlive(5, 3)

    dispatcher_thread = None
    dispatcher_thread_type = nimo_mode

    while conn.isAlive() == 1:

        # Create dispatcher thread to read queue and dispatch events
        dispatcher_thread = nimo_dispatcher.Dispatcher.create(
            dispatcher_thread_type,
            nimo_event_processor.wait_and_dispatch_queued_events,
            nimo_callbacks.event_queued_notify_recv, nimo_callbacks.event_q)
        # TODO(jay): Implement sighandlers, when nimo mode = 'nativethread'

        LOG.debug("Main-thread waiting forever "
                  "on Dispatcher thread to finish")

        dispatcher_thread.join()

        # This should be safe as green thread is dead
        LOG.error("Dispatcher thread exited.")

        # at this point, spin back up a dispatcher thread or exit process
        eventlet.sleep(1)

    else:
        # If connection with hypervisor breaks, exit.
        # TODO(jay): Ideally, terminate all threads and recall this main().
        LOG.error("connection to Host is broken. Exiting")

        exit(1)
Beispiel #37
0
def get_host_cpu_siblings():
    """Return core to sibling mapping of the host CPUs

        {core_0: [sibling_a, sibling_b, ...],
         core_1: [sibling_a, sibling_b, ...],
         ...}

    libvirt's getCapabilities() is called to get details about the host
    then a list of siblings per CPU is extracted and formatted to single level
    list
    """
    siblings = {}
    conn = libvirt.openReadOnly('qemu:///system')
    capxml = ET.fromstring(conn.getCapabilities())
    cpu_cells = capxml.findall('./host/topology/cells/cell/cpus')

    for cell in cpu_cells:
        cpus = cell.findall('cpu')
        for cpu in cpus:
            cpu_id = int(cpu.get('id'))
            sib = cpu.get('siblings')
            _siblings = get_siblings_list(sib)

            siblings.update({cpu_id: _siblings})

    return siblings
Beispiel #38
0
    def collect(self):
        if libvirt is None:
            self.log.error('Unable to import libvirt')
            return {}

        conn = libvirt.openReadOnly(self.config['uri'])
        for dom in [conn.lookupByID(n) for n in conn.listDomainsID()]:
            name = dom.name()

            # CPU stats
            vcpus = dom.getCPUStats(True, 0)
            totalcpu = 0
            idx = 0
            for vcpu in vcpus:
                cputime = vcpu['cpu_time']
                self.report_cpu_metric('cpu.%s.time' % idx, cputime, name)
                idx += 1
                totalcpu += cputime
            self.report_cpu_metric('cpu.total.time', totalcpu, name)

            # Disk stats
            disks = self.get_disk_devices(dom)
            accum = {}
            for stat in self.blockStats.keys():
                accum[stat] = 0

            for disk in disks:
                stats = dom.blockStats(disk)
                for stat in self.blockStats.keys():
                    idx = self.blockStats[stat]
                    val = stats[idx]
                    accum[stat] += val
                    self.publish('block.%s.%s' % (disk, stat),
                                 val,
                                 instance=name)
            for stat in self.blockStats.keys():
                self.publish('block.total.%s' % stat,
                             accum[stat],
                             instance=name)

            # Network stats
            vifs = self.get_network_devices(dom)
            accum = {}
            for stat in self.vifStats.keys():
                accum[stat] = 0

            for vif in vifs:
                stats = dom.interfaceStats(vif)
                for stat in self.vifStats.keys():
                    idx = self.vifStats[stat]
                    val = stats[idx]
                    accum[stat] += val
                    self.publish('net.%s.%s' % (vif, stat), val, instance=name)
            for stat in self.vifStats.keys():
                self.publish('net.total.%s' % stat, accum[stat], instance=name)

            # Memory stats
            mem = dom.memoryStats()
            self.publish('memory.nominal', mem['actual'] * 1024, instance=name)
            self.publish('memory.rss', mem['rss'] * 1024, instance=name)
Beispiel #39
0
def _create_network_vm(args):
    """
    Creates a libvirt network

    Parameters
    -----------
    args :
        dict as the one returned by argparse.ArgumentParser().parse_args()

    Returns
    -------
        int
            The return value provided by the kvm storage pool create
            call, 0 on success, 1 otherwise.
    """
    # check network  name unicity
    conn = libvirt.openReadOnly(None)
    _vnets = []
    if conn:
        _vnets = [n.name() for n in conn.listAllNetworks() if n.name() == args.network_name]
        conn.close()
    else:
        print('Cannot contact hypervisor', file=sys.stderr)
        return 1
    if len(_vnets) != 0:
        print("Network with name [%s] already exists" % args.network_name, file=sys.stderr)
        return 1

    return oci_utils.kvm.virt.create_virtual_network(network=args.net,
                                                     network_name=args.network_name,
                                                     ip_bridge=args.ip_bridge,
                                                     ip_prefix=args.ip_prefix,
                                                     ip_start=args.ip_start,
                                                     ip_end=args.ip_end)
Beispiel #40
0
def init_state(config):
    """ 
    Initialize a dict for storing the state of the local manager.

    :param config: A config dictionary.
     :type config: dict(str: *)

    :return: A dictionary containing the initial state of the local manager.
     :rtype: dict
    """
    vir_connection = libvirt.openReadOnly(None)
    if vir_connection is None:
        message = 'Failed to open a connection to the hypervisor'
        raise OSError(message)

    """
    physical_cpu_mhz_total:通过libvirt获取所有CPU核频率之和(MHz);
    host_cpu_usable_by_vms:主机中可以分配给虚拟机使用的cpu个数占总体cpu的百分比(阈值);
    """
    physical_cpu_mhz_total = int(
        _physical_cpu_mhz_total(vir_connection) *
        float(CONF.host_cpu_usable_by_vms))
    
    return {'previous_time': 0.,
            'vir_connection': vir_connection,
            'db': init_db(config['sql_connection']),
            'physical_cpu_mhz_total': physical_cpu_mhz_total,
            'hostname': vir_connection.getHostname(),
            'hashed_username': sha1(config['os_admin_user']).hexdigest(),
            'hashed_password': sha1(config['os_admin_password']).hexdigest()}
Beispiel #41
0
    def init_state(self):
        """ Initialize a dict for storing the state of the data collector."""
        vir_connection = libvirt.openReadOnly(None)
        if vir_connection is None:
            message = 'Failed to open a connection to the hypervisor'
            LOG.critical(message)
            raise OSError(message)

        hostname = vir_connection.getHostname()
        host_cpu_mhz, host_ram = self.get_host_characteristics(vir_connection)
        physical_cpus = common.physical_cpu_count(vir_connection)
        host_cpu_usable_by_vms = CONF.host_cpu_usable_by_vms

        db = db_utils.init_db()
        db.update_host(hostname,
                       int(host_cpu_mhz * host_cpu_usable_by_vms),
                       physical_cpus,
                       host_ram)

        return {'previous_time': 0.,
                'previous_cpu_time': dict(),
                'previous_cpu_mhz': dict(),
                'previous_host_cpu_time_total': 0.,
                'previous_host_cpu_time_busy': 0.,
                'previous_overload': -1,
                'vir_connection': vir_connection,
                'hostname': hostname,
                'host_cpu_overload_threshold':
                    CONF.collector.host_cpu_overload_threshold * \
                    host_cpu_usable_by_vms,
                'physical_cpus': physical_cpus,
                'physical_cpu_mhz': host_cpu_mhz,
                'physical_core_mhz': host_cpu_mhz / physical_cpus,
                'db': db}
def main():
    hosts = sys.argv
    hosts.pop(0)

    for h in hosts:
        if h == "-v" or h == "--verbose":
            VERBOSE = True
            continue

        uri = "qemu+ssh://%s/system" % h

        try:
            conn = libvirt.openReadOnly(uri)
        except libvirt.libvirtError as e:
            print("ERROR connecting to %s: %s" % (uri, e.message))
            continue

        # some code examples imply that older versions
        # returned None instead of raising an exception
        if conn is None:
            print("ERROR connecting to %s: %s" % (uri, e.message))
            continue

        host_info = get_host_info(conn)

        doms = get_domains(conn)

        image_paths = []
        for d in doms:
            image_paths.extend(d['disk_files'])

        df = get_disk_free(image_paths, h)
        host_info['df_bytes'] = df

        ts = int(time.time())
        foo = {
            'type': 'host',
            'name': host_info['hostname'],
            'data': host_info,
            'updated_ts': ts
        }
        print to_json(foo)
        with open("host_%s.json" % host_info['hostname'], 'w') as fh:
            fh.write(to_json(foo))

        for d in doms:
            #print("{host},{name},{ID},{state},{UUID}".format(host=h, name=d['name'], ID=d['ID'], UUID=d['UUID'], state=d['state']))
            foo = {
                'type': 'guest',
                'name': d['name'],
                'uuid': d['UUID'],
                'data': d,
                'host': host_info['hostname'],
                'updated_ts': ts
            }
            print to_json(foo)
            with open(
                    "host_%s_guest_%s.json" %
                (host_info['hostname'], d['name']), 'w') as fh:
                fh.write(to_json(foo))
Beispiel #43
0
def init_state():
    """ 
    Initialize a dict for storing the state of the data collector.
    """
    """
    @@@@注:这里可以放在xdrs-host的初始化过程中来实现,并写入数据表HostInitData;
    这里的操作就是对这个数据表进行数据的初始化操作;
    其实这个系统有个瓶颈,就是若干数据库访问量过大,根据云平台集群规模和所建立的
    虚拟机数量的大小,可能同时会有大量的数据表更新等操作访问数据库。
    db.update_host(hostname,
                   int(host_cpu_mhz * host_cpu_usable_by_vms),
                   physical_cpus,
                   host_ram)
    """
    hosts_api = hosts.API()

    vir_connection = libvirt.openReadOnly(None)
    hostname = vir_connection.getHostname()
    """
    获取本地主机总的CPU MHZ和RAM数据;
    """
    host_cpu_mhz, host_ram = get_host_characteristics(vir_connection)
    """
    通过libvirt获取物理CPU的数目;
    """
    physical_cpus = physical_cpu_count(vir_connection)

    """
    主机中可以分配给虚拟机使用的cpu个数占总体cpu的百分比(阈值);
    """
    host_cpu_usable_by_vms = float(CONF.host_cpu_usable_by_vms)
    local_cpu_mhz = int(host_cpu_mhz * host_cpu_usable_by_vms)
    host_cpu_overload_threshold = float(CONF.host_cpu_overload_threshold) * host_cpu_usable_by_vms
    physical_core_mhz = host_cpu_mhz / physical_cpus
    host_id = 2
    """在系统初始化的过程中随机生成;"""

    init_data = {
        "host_name": hostname,
        "host_id": host_id,
        "local_cpu_mhz": local_cpu_mhz,
        "physical_cpus": physical_cpus,
        "host_ram": host_ram,
        "previous_time": 0.0,
        "previous_cpu_time": dict(),
        "previous_cpu_mhz": dict(),
        "previous_host_cpu_time_total": 0.0,
        "previous_host_cpu_time_busy": 0.0,
        "previous_overload": -1,
        "host_cpu_overload_threshold": host_cpu_overload_threshold,
        "physical_cpu_mhz": host_cpu_mhz,
        "physical_core_mhz": physical_core_mhz,
    }

    try:
        host_init_data = hosts_api.create_host_init_data(init_data)
    except exception.HostInitDataNotFound:
        msg = _("host init data not found")
        raise webob.exc.HTTPBadRequest(explanation=msg)
    """
Beispiel #44
0
def main():
    conn = libvirt.openReadOnly(None)
    if not conn:
        print('Failed to open connection to qemu:///system')
        exit(1)

    domainIDs = conn.listDomainsID()
    if domainIDs == None:
        print('Failed to get a list of domain IDs')
        exit(1)

    domain_info = {}
    for domainID in domainIDs:
        domain = conn.lookupByID(domainID)
        name = domain.name()
        domain_info[name] = {}
        vcpu_list = get_cpus(conn, domainID, "/domain/cputune/vcpupin")
        domain_info[name]['vcpus'] = vcpu_list

        emu_list = get_cpus(conn, domainID, "/domain/cputune/emulatorpin")
        domain_info[name]['emulator'] = emu_list

    cpu_layout = get_cpu_layout()
    for item, value in domain_info.iteritems():
        print(item)
        print(value)
Beispiel #45
0
    async def on_watchdog_timer(self):
        """connection watchdog timer within main asyncio loop
        """
        #~ print("on_watchdog_timer",self.name)
        if not self.vc or self.vc.isAlive() != 1:
            #connect/reconnect
            if self.vc:
                #cleanup previous connection
                for cid in self.callback_ids:
                    try:
                        self.vc.domainEventDeregisterAny(cid)
                    except Exception:
                        pass
                self.vc.close()
                del self.vc

            self.vc = libvirt.openReadOnly(self.uri)

            self.data.update_hv_info(self.id_, self.vc)

            for event, callback in self.event_callback_handlers.items():
                cid = self.vc.domainEventRegisterAny(None, event, callback,
                                                     None)
                self.callback_ids.append(cid)

            # Connection monitoring.
            self.vc.setKeepAlive(5, 3)
        else:
            # ~ print('all is ok')
            pass
Beispiel #46
0
def add_orphan_vm(vm_name, host_id):
    """
    Add Orphan VM information to database. VM information is retrieved from the VM definition XML.
    'System User' is added as owner of the VM.
    """
    host_details = db.host[host_id]
    host_ip = host_details.host_ip.private_ip
    connection_object = libvirt.openReadOnly("qemu+ssh://root@" + host_ip + "/system")
    domain = connection_object.lookupByName(vm_name)
    vm_state = domain.info()[0]
    vm_status = vm_state_map[vm_state]    
    # Parse domain XML to get information about VM
    root = etree.fromstring(domain.XMLDesc(0))

    ram_elem = root.xpath('memory')[0]
    ram_in_kb = int(ram_elem.text)
    ram_in_mb = int(round(int(ram_in_kb)/(1024),0))

    cpu_elem = root.xpath('vcpu')[0]
    cpu = int(cpu_elem.text)

    vnc_elem = root.xpath("devices/graphics[@type='vnc']")[0]
    vnc_port = vnc_elem.attrib['port']
    
    mac_elem = root.xpath("devices/interface[@type='network']/mac")[0]
    mac_address = mac_elem.attrib['address']

    ip_addr = db.private_ip_pool(mac_addr = mac_address)
    ip_address = None
    if ip_addr:
        ip_address = ip_addr['id']
    
    template_elem = root.xpath("devices/disk[@type='file']/source")[0]
    template_file = template_elem.attrib['file']
    
    command = "qemu-img info " + template_file + " | grep 'virtual size'"
    ret = execute_remote_cmd(host_ip, 'root', command) # Returns e.g. virtual size: 40G (42949672960 bytes)
    hdd = int(ret[ret.index(':')+1:ret.index('G ')].strip())

    security_domain_row = db.security_domain(vlan=ip_addr['vlan'])
    
    db.vm_data.insert(
        vm_name = vm_name, 
        vm_identity = (vm_name), 
        RAM = ram_in_mb,
        HDD = hdd,
        extra_HDD = 0,
        vCPU = cpu,
        host_id = host_id,
        template_id = 1, #TBD
        datastore_id = 1, #TBD
        owner_id = SYSTEM_USER,
        requester_id = SYSTEM_USER,
        private_ip = ip_address,
        vnc_port = vnc_port,
        purpose = 'Added by System',
        security_domain = security_domain_row['id'],
        status = vm_status)
        
    return
Beispiel #47
0
def virshdomstate(domain):

    libvirt_states = {
        libvirt.VIR_DOMAIN_RUNNING: "running",
        libvirt.VIR_DOMAIN_SHUTOFF: "shut off",
        libvirt.VIR_DOMAIN_SHUTDOWN: "shut down",
        libvirt.VIR_DOMAIN_PAUSED: "paused",
        libvirt.VIR_DOMAIN_NOSTATE: "no state",
        libvirt.VIR_DOMAIN_BLOCKED: "blocked",
        libvirt.VIR_DOMAIN_CRASHED: "crashed",
    }

    conn = libvirt.openReadOnly(driver)
    if conn == None:
        print "Failed to open connection to the hypervisor"
        sys.exit(3)

    try:
        dom = conn.lookupByName(domain)
    except libvirt.libvirtError:
        return None

    state = libvirt_states[dom.info()[0]]
    conn.close()

    return state
Beispiel #48
0
    def _try_open(self):
        flags = 0

        vmm = self._open_dev_conn(self.get_uri())
        if vmm:
            return vmm

        if self.readOnly:
            logging.info("Caller requested read only connection")
            flags = libvirt.VIR_CONNECT_RO

        if virtinst.support.support_openauth():
            vmm = libvirt.openAuth(self.get_uri(),
                                   [[libvirt.VIR_CRED_AUTHNAME,
                                     libvirt.VIR_CRED_PASSPHRASE,
                                     libvirt.VIR_CRED_EXTERNAL],
                                    self._do_creds, None],
                                   flags)
        else:
            if flags:
                vmm = libvirt.openReadOnly(self.get_uri())
            else:
                vmm = libvirt.open(self.get_uri())

        return vmm
Beispiel #49
0
    def scan_attached_devices(self):
        devices = []
        vm_ids = {}
        conn = libvirt.openReadOnly('qemu:///system')
        domains = []
        self.macs_map = self._get_vfs_macs()
        domains_names = conn.listDefinedDomains()
        defined_domains = map(conn.lookupByName, domains_names)
        domains_ids = conn.listDomainsID()
        running_domains = map(conn.lookupByID, domains_ids)
        for domain in defined_domains:
            [state, maxmem, mem, ncpu, cputime] = domain.info()
            if state in (libvirt.VIR_DOMAIN_PAUSED,
                         libvirt.VIR_DOMAIN_SHUTDOWN,
                         libvirt.VIR_DOMAIN_SHUTOFF):
                domains.append(domain)
        domains += running_domains

        for domain in domains:
            raw_xml = domain.XMLDesc(0)
            tree = etree.XML(raw_xml)
            hostdevs = tree.xpath("devices/hostdev/source/address")
            vm_id = tree.find('uuid').text
            for dev in self._get_attached_hostdevs(hostdevs):
                devices.append(dev)
                vm_ids[dev[0]] = vm_id
        return devices, vm_ids
    def run(self):
        args = self._prepare_args()

        log.info("Running virt-install.")
        log.info("virt-install %s", args)
        try:
            execWithRedirect("virt-install", args, raise_err=True)
        except subprocess.CalledProcessError as e:
            raise InstallError("Problem starting virtual install: %s" % e)

        conn = libvirt.openReadOnly(None)
        dom = conn.lookupByName(self._virt_name)

        # TODO: If vnc has been passed, we should look up the port and print that
        # for the user at this point
        while dom.isActive() and not self._log_check():
            sys.stdout.write(".")
            sys.stdout.flush()
            sleep(10)
        print()

        if self._log_check():
            log.info("Installation error detected. See logfile.")
        else:
            log.info("Install finished. Or at least virt shut down.")
def virshlist(*states):

    libvirt_states={ 'running'  : libvirt.VIR_DOMAIN_RUNNING,
                     'shutoff'  : libvirt.VIR_DOMAIN_SHUTOFF,
                     'shutdown' : libvirt.VIR_DOMAIN_SHUTDOWN,
                     'paused'   : libvirt.VIR_DOMAIN_PAUSED,
                     'nostate'  : libvirt.VIR_DOMAIN_NOSTATE,
                     'blocked'  : libvirt.VIR_DOMAIN_BLOCKED,
                     'crashed'  : libvirt.VIR_DOMAIN_CRASHED,
    }

    searchstates = list(libvirt_states[state] for state in states)

    conn = libvirt.openReadOnly('qemu:///system')
    if conn == None:
       print 'Failed to open connection to the hypervisor'
       sys.exit(3)

    alldomains = map(conn.lookupByID, conn.listDomainsID())
    alldomains += map(conn.lookupByName, conn.listDefinedDomains())

    domains = []
    for domain in alldomains:
        if domain.info()[0] in searchstates:
            domains.append(domain.name())

    conn.close()

    return domains
Beispiel #52
0
    def waitForAction(self, domid, timeout=None, events=None):
        libvirt.virEventRegisterDefaultImpl()
        rocon = libvirt.openReadOnly()
        run = {'state': True, 'timeout': False}

        def timecb(timerid, opaque):
            run['state'] = False
            run['timeout'] = True

        def callback(con, domain, event, detail, opaque):
            if domain.UUIDString() == domid:
                if events is not None and event in events:
                    run['state'] = False
                    return True

        if timeout:
            libvirt.virEventAddTimeout(timeout, timecb, None)
        rocon.domainEventRegisterAny(None,
                                     libvirt.VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                     callback,
                                     rocon)
        while run['state']:
            libvirt.virEventRunDefaultImpl()

        if run['timeout']:
            raise TimeoutError("Failed to wait for state")
Beispiel #53
0
def virshdomstate(domain):

    libvirt_states={ libvirt.VIR_DOMAIN_RUNNING  : 'running',
                     libvirt.VIR_DOMAIN_SHUTOFF  : 'shut off',
                     libvirt.VIR_DOMAIN_SHUTDOWN : 'shut down',
                     libvirt.VIR_DOMAIN_PAUSED   : 'paused',
                     libvirt.VIR_DOMAIN_NOSTATE  : 'no state',
                     libvirt.VIR_DOMAIN_BLOCKED  : 'blocked',
                     libvirt.VIR_DOMAIN_CRASHED  : 'crashed',
    }

    conn = libvirt.openReadOnly('qemu:///system')
    if conn == None:
       print 'Failed to open connection to the hypervisor'
       sys.exit(3)

    try:
        dom = (conn.lookupByName (domain))
    except libvirt.libvirtError:
        return None

    state = libvirt_states[dom.info()[0]]
    conn.close()

    return state
Beispiel #54
0
    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)
Beispiel #55
0
def new_libvirt_connection(conf):
    if not libvirt:
        raise ImportError("python-libvirt module is missing")
    uri = (conf.libvirt_uri
           or LIBVIRT_PER_TYPE_URIS.get(conf.libvirt_type, 'qemu:///system'))
    LOG.debug('Connecting to libvirt: %s', uri)
    return libvirt.openReadOnly(uri)
Beispiel #56
0
def main():
    logging.basicConfig(level=logging.INFO)

    parser = argparse.ArgumentParser()
    parser.add_argument('-p', '--port', type=int, default=1024)
    parser.add_argument('-c', '--connect', dest='libvirt_connection_string', default='qemu:///system')
    parser.add_argument('--enable-xheaders', default=False, action='store_true')
    parser.add_argument('--load-edited-domain-xml', default=False, action='store_true',
                        help='Use this flag to make changes to Domain XML to be reflected immediately (opposed ' +
                        'to requiring a restart of the domain)')
    parser.add_argument('--plugin', action='append', help='Load this plugin. This simply imports the module. ' +
                                                          'See example for more details')
    args = parser.parse_args()

    if args.plugin:
        for plugin in args.plugin:
            __import__(plugin)

    application = tornado.web.Application(
        handlers.routes,
        machine_resolver=utils.machine_resolver.LibvirtMachineResolver(
            libvirt.openReadOnly(args.libvirt_connection_string),
            args.load_edited_domain_xml)
    )

    http_server = tornado.httpserver.HTTPServer(application)
    http_server.xheaders = args.enable_xheaders
    http_server.listen(args.port)
    tornado.ioloop.IOLoop.instance().start()