⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xendconfig.py

📁 xen 3.2.2 源码
💻 PY
📖 第 1 页 / 共 5 页
字号:
                    # because we create one automatically in                    # XendDomainInfo._update_consoles                    raise XendConfigError('Creating vt100 consoles via '                                          'Xen API is unsupported')            return dev_uuid        # no valid device to add        return ''    def phantom_device_add(self, dev_type, cfg_xenapi = None,                   target = None):        """Add a phantom tap device configuration in XenAPI struct format.        """        if target == None:            target = self                if dev_type not in XendDevices.valid_devices() and \           dev_type not in XendDevices.pseudo_devices():                    raise XendConfigError("XendConfig: %s not a valid device type" %                            dev_type)        if cfg_xenapi == None:            raise XendConfigError("XendConfig: device_add requires some "                                  "config.")        if cfg_xenapi:            log.debug("XendConfig.phantom_device_add: %s" % str(cfg_xenapi))         if cfg_xenapi:            dev_info = {}                        if dev_type in ('vbd', 'tap'):                if dev_type == 'vbd':                    dev_info['uname'] = cfg_xenapi.get('image', '')                    dev_info['dev'] = '%s:disk' % cfg_xenapi.get('device')                elif dev_type == 'tap':                    if cfg_xenapi.get('image').find('tap:') == -1:                        dev_info['uname'] = 'tap:qcow:%s' % cfg_xenapi.get('image')                    dev_info['dev'] =  '/dev/%s' % cfg_xenapi.get('device')                    dev_info['uname'] = cfg_xenapi.get('image')                dev_info['mode'] = cfg_xenapi.get('mode')                dev_info['backend'] = '0'                dev_uuid = cfg_xenapi.get('uuid', uuid.createString())                dev_info['uuid'] = dev_uuid                self['devices'][dev_uuid] = (dev_type, dev_info)                self['vbd_refs'].append(dev_uuid)                return dev_uuid        return ''    def console_add(self, protocol, location, other_config = {}):        dev_uuid = uuid.createString()        if protocol == 'vt100':            dev_info = {                'uuid': dev_uuid,                'protocol': protocol,                'location': location,                'other_config': other_config,            }            if 'devices' not in self:                self['devices'] = {}                        self['devices'][dev_uuid] = ('console', dev_info)            self['console_refs'].append(dev_uuid)            return dev_info        return {}    def console_update(self, console_uuid, key, value):        for dev_uuid, (dev_type, dev_info) in self['devices'].items():            if dev_uuid == console_uuid:                dev_info[key] = value                # collapse other_config into dev_info for things                # such as vncpasswd, vncunused, etc.                if key == 'other_config':                    for k in XENAPI_CONSOLE_OTHER_CFG:                        if k in dev_info and k not in value:                            del dev_info[k]                    dev_info.update(value)                break    def console_get_all(self, protocol):        if protocol == 'vt100':            consoles = [dinfo for dtype, dinfo in self['devices'].values()                        if dtype == 'console']            return [c for c in consoles if c.get('protocol') == protocol]        elif protocol == 'rfb':            vfbs = [dinfo for dtype, dinfo in self['devices'].values()                   if dtype == 'vfb']            # move all non-console key values to other_config before            # returning console config            valid_keys = ['uuid', 'location']            for vfb in vfbs:                other_config = {}                for key, val in vfb.items():                    if key not in valid_keys:                        other_config[key] = vfb[key]                    del vfb[key]                vfb['other_config'] = other_config                vfb['protocol'] = 'rfb'                                    return vfbs        else:            return []    def device_update(self, dev_uuid, cfg_sxp = [], cfg_xenapi = {}):        """Update an existing device with the new configuration.        @rtype: boolean        @return: Returns True if succesfully found and updated a device conf        """        if dev_uuid in self['devices'] and cfg_sxp:            if sxp.child0(cfg_sxp) == 'device':                            config = sxp.child0(cfg_sxp)            else:                config = cfg_sxp            dev_type, dev_info = self['devices'][dev_uuid]            for opt_val in config[1:]:                try:                    opt, val = opt_val                    dev_info[opt] = val                except (TypeError, ValueError):                    pass # no value for this config option            self['devices'][dev_uuid] = (dev_type, dev_info)            return True                elif dev_uuid in self['devices'] and cfg_xenapi:            dev_type, dev_info = self['devices'][dev_uuid]            for key, val in cfg_xenapi.items():                dev_info[key] = val            self['devices'][dev_uuid] = (dev_type, dev_info)        return False    def device_sxpr(self, dev_uuid = None, dev_type = None, dev_info = None, target = None):        """Get Device SXPR by either giving the device UUID or (type, config).        @rtype: list of lists        @return: device config sxpr        """        sxpr = []        if target == None:            target = self        if dev_uuid != None and dev_uuid in target['devices']:            dev_type, dev_info = target['devices'][dev_uuid]        if dev_type == None or dev_info == None:            raise XendConfigError("Required either UUID or device type and "                                  "configuration dictionary.")                    sxpr.append(dev_type)        if dev_type in ('console', 'vfb'):            config = [(opt, val) for opt, val in dev_info.items()                      if opt != 'other_config']        else:            config = [(opt, val) for opt, val in dev_info.items()]                    sxpr += config        return sxpr    def ordered_device_refs(self, target = None):        result = []        if target == None:            target = self        # vkbd devices *must* be before vfb devices, otherwise        # there is a race condition when setting up devices        # where the daemon spawned for the vfb may write stuff        # into xenstore vkbd backend, before DevController has        # setup permissions on the vkbd backend path. This race        # results in domain creation failing with 'device already        # connected' messages        result.extend([u for u in target['devices'].keys() if target['devices'][u][0] == 'vkbd'])        result.extend(target.get('console_refs', []) +                      target.get('vbd_refs', []) +                      target.get('vif_refs', []) +                      target.get('vtpm_refs', []))        result.extend([u for u in target['devices'].keys() if u not in result])        return result    def all_devices_sxpr(self, target = None):        """Returns the SXPR for all devices in the current configuration."""        sxprs = []        pci_devs = []        if target == None:            target = self        if 'devices' not in target:            return sxprs                ordered_refs = self.ordered_device_refs(target = target)        for dev_uuid in ordered_refs:            dev_type, dev_info = target['devices'][dev_uuid]            if dev_type == 'pci': # special case for pci devices                sxpr = ['pci', ['uuid', dev_info['uuid']]]                for pci_dev_info in dev_info['devs']:                    pci_dev_sxpr = ['dev']                    for opt, val in pci_dev_info.items():                        pci_dev_sxpr.append([opt, val])                    sxpr.append(pci_dev_sxpr)                sxprs.append((dev_type, sxpr))            else:                sxpr = self.device_sxpr(dev_type = dev_type,                                        dev_info = dev_info,                                        target   = target)                sxprs.append((dev_type, sxpr))        return sxprs    def image_sxpr(self):        """Returns a backwards compatible image SXP expression that is        used in xenstore's /vm/<uuid>/image value and xm list."""        image = [self.image_type()]        if self.has_key('PV_kernel'):            image.append(['kernel', self['PV_kernel']])        if self.has_key('PV_ramdisk') and self['PV_ramdisk']:            image.append(['ramdisk', self['PV_ramdisk']])        if self.has_key('PV_args') and self['PV_args']:            image.append(['args', self['PV_args']])        for key in XENAPI_PLATFORM_CFG:            if key in self['platform']:                image.append([key, self['platform'][key]])        if 'notes' in self:            image.append(self.notes_sxp(self['notes']))        return image    def update_with_image_sxp(self, image_sxp, bootloader = False):        # Convert Legacy "image" config to Xen API PV_*        # configuration        log.debug("update_with_image_sxp(%s)" % scrub_password(image_sxp))        # user-specified args must come last: previous releases did this and        # some domU kernels rely upon the ordering.        kernel_args = sxp.child_value(image_sxp, 'args', '')        # attempt to extract extra arguments from SXP config        arg_ip = sxp.child_value(image_sxp, 'ip')        if arg_ip and not re.search(r'ip=[^ ]+', kernel_args):            kernel_args = 'ip=%s ' % arg_ip + kernel_args        arg_root = sxp.child_value(image_sxp, 'root')        if arg_root and not re.search(r'root=', kernel_args):            kernel_args = 'root=%s ' % arg_root + kernel_args        if bootloader:            self['_temp_using_bootloader'] = '1'            self['_temp_kernel'] = sxp.child_value(image_sxp, 'kernel','')            self['_temp_ramdisk'] = sxp.child_value(image_sxp, 'ramdisk','')            self['_temp_args'] = kernel_args        else:            self['PV_kernel'] = sxp.child_value(image_sxp, 'kernel','')            self['PV_ramdisk'] = sxp.child_value(image_sxp, 'ramdisk','')            self['PV_args'] = kernel_args        for key in XENAPI_PLATFORM_CFG:            val = sxp.child_value(image_sxp, key, None)            if val is not None and val != '':                self['platform'][key] = val        notes = sxp.children(image_sxp, 'notes')        if notes:            self['notes'] = self.notes_from_sxp(notes[0])        self._hvm_boot_params_from_sxp(image_sxp)    def set_notes(self, notes):        'Add parsed elfnotes to image'        self['notes'] = notes    def get_notes(self):        try:            return self['notes'] or {}        except KeyError:            return {}    def notes_from_sxp(self, nsxp):        notes = {}        for note in sxp.children(nsxp):            notes[note[0]] = note[1]        return notes    def notes_sxp(self, notes):        nsxp = ['notes']        for k, v in notes.iteritems():            nsxp.append([k, str(v)])        return nsxp            def _hvm_boot_params_from_sxp(self, image_sxp):        boot = sxp.child_value(image_sxp, 'boot', None)        if boot is not None:            self['HVM_boot_policy'] = 'BIOS order'            self['HVM_boot_params'] = { 'order' : boot }    def is_hvm(self):        return self['HVM_boot_policy'] != ''    def image_type(self):        stored_type = self['platform'].get('image_type')        return stored_type or (self.is_hvm() and 'hvm' or 'linux')

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -