📄 xendconfig.py
字号:
old_state = sxp.child_value(sxp_cfg, 'state') if old_state: for i in range(len(CONFIG_OLD_DOM_STATES)): cfg[CONFIG_OLD_DOM_STATES[i]] = int(old_state[i] != '-') return cfg def _sxp_to_xapi(self, sxp_cfg): """Read in an SXP Configuration object and populate at much of the Xen API with valid values. """ log.debug('_sxp_to_xapi(%s)' % scrub_password(sxp_cfg)) cfg = self._parse_sxp(sxp_cfg) for key, typ in XENAPI_CFG_TYPES.items(): val = cfg.get(key) if val is not None: self[key] = typ(val) # Convert parameters that can be directly mapped from # the Legacy Config to Xen API Config for apikey, cfgkey in XENAPI_CFG_TO_LEGACY_CFG.items(): try: type_conv = XENAPI_CFG_TYPES.get(apikey) if callable(type_conv): self[apikey] = type_conv(cfg[cfgkey]) else: log.warn("Unconverted key: " + apikey) self[apikey] = cfg[cfgkey] except KeyError: pass # Lets try and handle memory correctly MiB = 1024 * 1024 if "memory" in cfg: self["memory_static_min"] = 0 self["memory_static_max"] = int(cfg["memory"]) * MiB self["memory_dynamic_min"] = int(cfg["memory"]) * MiB self["memory_dynamic_max"] = int(cfg["memory"]) * MiB if "maxmem" in cfg: self["memory_static_max"] = int(cfg["maxmem"]) * MiB self._memory_sanity_check() def update_with(n, o): if not self.get(n): self[n] = cfg.get(o, '') update_with('PV_bootloader', 'bootloader') update_with('PV_bootloader_args', 'bootloader_args') image_sxp = sxp.child_value(sxp_cfg, 'image', []) if image_sxp: self.update_with_image_sxp(image_sxp) # Convert Legacy HVM parameters to Xen API configuration for key in XENAPI_PLATFORM_CFG: if key in cfg: self['platform'][key] = cfg[key] # set device references in the configuration self['devices'] = cfg.get('devices', {}) self['console_refs'] = cfg.get('console_refs', []) self['vif_refs'] = cfg.get('vif_refs', []) self['vbd_refs'] = cfg.get('vbd_refs', []) self['vtpm_refs'] = cfg.get('vtpm_refs', []) # coalesce hvm vnc frame buffer with vfb config if self.is_hvm() and int(self['platform'].get('vnc', 0)) != 0: # add vfb device if it isn't there already if not self.has_rfb(): dev_config = ['vfb'] dev_config.append(['type', 'vnc']) # copy VNC related params from platform config to vfb dev conf for key in ['vncpasswd', 'vncunused', 'vncdisplay', 'vnclisten']: if key in self['platform']: dev_config.append([key, self['platform'][key]]) self.device_add('vfb', cfg_sxp = dev_config) def has_rfb(self): for console_uuid in self['console_refs']: if self['devices'][console_uuid][1].get('protocol') == 'rfb': return True if self['devices'][console_uuid][0] == 'vfb': return True return False def _sxp_to_xapi_unsupported(self, sxp_cfg): """Read in an SXP configuration object and populate values are that not related directly supported in the Xen API. """ log.debug('_sxp_to_xapi_unsupported(%s)' % scrub_password(sxp_cfg)) # Parse and convert parameters used to configure # the image (as well as HVM images) image_sxp = sxp.child_value(sxp_cfg, 'image', []) if image_sxp: image_type = sxp.name(image_sxp) if image_type != 'hvm' and image_type != 'linux': self['platform']['image_type'] = image_type 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) # extract backend value backend = [] for c in sxp.children(sxp_cfg, 'backend'): backend.append(sxp.name(sxp.child0(c))) if backend: self['backend'] = backend # Parse and convert other Non Xen API parameters. def _set_cfg_if_exists(sxp_arg): val = sxp.child_value(sxp_cfg, sxp_arg) if val != None: if LEGACY_CFG_TYPES.get(sxp_arg): self[sxp_arg] = LEGACY_CFG_TYPES[sxp_arg](val) else: self[sxp_arg] = val _set_cfg_if_exists('shadow_memory') _set_cfg_if_exists('features') _set_cfg_if_exists('on_xend_stop') _set_cfg_if_exists('on_xend_start') _set_cfg_if_exists('vcpu_avail') # Parse and store runtime configuration _set_cfg_if_exists('start_time') _set_cfg_if_exists('cpu_time') _set_cfg_if_exists('shutdown_reason') _set_cfg_if_exists('up_time') _set_cfg_if_exists('status') # TODO, deprecated def _get_old_state_string(self): """Returns the old xm state string. @rtype: string @return: old state string """ state_string = '' for state_name in CONFIG_OLD_DOM_STATES: on_off = self.get(state_name, 0) if on_off: state_string += state_name[0] else: state_string += '-' return state_string def update_config(self, dominfo): """Update configuration with the output from xc.domain_getinfo(). @param dominfo: Domain information via xc.domain_getinfo() @type dominfo: dict """ self._dominfo_to_xapi(dominfo) self.validate() def update_with_xenapi_config(self, xapi): """Update configuration with a Xen API VM struct @param xapi: Xen API VM Struct @type xapi: dict """ log.debug('update_with_xenapi_config: %s' % scrub_password(xapi)) for key, val in xapi.items(): type_conv = XENAPI_CFG_TYPES.get(key) if type_conv is None: key = key.lower() type_conv = XENAPI_CFG_TYPES.get(key) if callable(type_conv): self[key] = type_conv(val) else: self[key] = val self['vcpus_params']['weight'] = \ int(self['vcpus_params'].get('weight', 256)) self['vcpus_params']['cap'] = int(self['vcpus_params'].get('cap', 0)) def to_sxp(self, domain = None, ignore_devices = False, ignore = [], legacy_only = True): """ Get SXP representation of this config object. Incompat: removed store_mfn, console_mfn @keyword domain: (optional) XendDomainInfo to get extra information from such as domid and running devices. @type domain: XendDomainInfo @keyword ignore: (optional) list of 'keys' that we do not want to export. @type ignore: list of strings @rtype: list of list (SXP representation) """ sxpr = ['domain'] # TODO: domid/dom is the same thing but called differently # depending if it is from xenstore or sxpr. if domain.getDomid() is not None: sxpr.append(['domid', domain.getDomid()]) if not legacy_only: for name, typ in XENAPI_CFG_TYPES.items(): if name in self and self[name] not in (None, []): if typ == dict: s = self[name].items() elif typ == list: s = self[name] else: s = str(self[name]) sxpr.append([name, s]) for xenapi, legacy in XENAPI_CFG_TO_LEGACY_CFG.items(): if legacy in ('cpus'): # skip this continue if self.has_key(xenapi) and self[xenapi] not in (None, []): if type(self[xenapi]) == bool: # convert booleans to ints before making an sxp item sxpr.append([legacy, int(self[xenapi])]) else: sxpr.append([legacy, self[xenapi]]) MiB = 1024*1024 sxpr.append(["maxmem", int(self["memory_static_max"])/MiB]) sxpr.append(["memory", int(self["memory_dynamic_max"])/MiB]) for legacy in LEGACY_UNSUPPORTED_BY_XENAPI_CFG: if legacy in ('domid', 'uuid', 'cpus'): # skip these continue if self.has_key(legacy) and self[legacy] not in (None, []): sxpr.append([legacy, self[legacy]]) if self.has_key('security_label'): sxpr.append(['security_label', self['security_label']]) sxpr.append(['image', self.image_sxpr()]) sxpr.append(['status', domain._stateGet()]) if domain.getDomid() is not None: sxpr.append(['state', self._get_old_state_string()]) if domain: if domain.store_mfn: sxpr.append(['store_mfn', domain.store_mfn]) if domain.console_mfn: sxpr.append(['console_mfn', domain.console_mfn]) # Marshall devices (running or from configuration) if not ignore_devices: txn = xstransact() try: for cls in XendDevices.valid_devices(): found = False # figure if there is a dev controller is valid and running if domain and domain.getDomid() != None: try: controller = domain.getDeviceController(cls) configs = controller.configurations(txn) for config in configs: if sxp.name(config) in ('vbd', 'tap'): # The bootable flag is never written to the # store as part of the device config. dev_uuid = sxp.child_value(config, 'uuid') dev_type, dev_cfg = self['devices'][dev_uuid] is_bootable = dev_cfg.get('bootable', 0) config.append(['bootable', int(is_bootable)]) config.append(['VDI', dev_cfg.get('VDI', '')]) sxpr.append(['device', config]) found = True except: log.exception("dumping sxp from device controllers") pass # if we didn't find that device, check the existing config # for a device in the same class if not found: for dev_type, dev_info in self.all_devices_sxpr(): if dev_type == cls: sxpr.append(['device', dev_info]) txn.commit() except: txn.abort() raise return sxpr def _blkdev_name_to_number(self, dev): if 'ioemu:' in dev: _, dev = dev.split(':', 1) try: dev, _ = dev.split(':', 1)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -