xendconfig.py
来自「xen虚拟机源代码安装包」· Python 代码 · 共 1,700 行 · 第 1/5 页
PY
1,700 行
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 if 'cpuid' in self: self.cpuid_to_sxp(sxpr, 'cpuid') if 'cpuid_check' in self: self.cpuid_to_sxp(sxpr, 'cpuid_check') log.debug(sxpr) return sxpr def _blkdev_name_to_number(self, dev): if 'ioemu:' in dev: _, dev = dev.split(':', 1) try: dev, _ = dev.split(':', 1) except ValueError: pass try: devid = int(dev) except ValueError: # devid is not a number but a string containing either device # name (e.g. xvda) or device_type/device_id (e.g. vbd/51728) dev2 = type(dev) is str and dev.split('/')[-1] or None if dev2 == None: log.debug("Could not check the device %s", dev) return None try: devid = int(dev2) except ValueError: (xenbus, devid) = blkdev_name_to_number(dev2) if devid == None: log.debug("The device %s is not device name", dev2) return None return devid def device_duplicate_check(self, dev_type, dev_info, defined_config): defined_devices_sxpr = self.all_devices_sxpr(target = defined_config) if dev_type == 'vbd' or dev_type == 'tap': dev_uname = dev_info.get('uname') blkdev_name = dev_info.get('dev') devid = self._blkdev_name_to_number(blkdev_name) if devid == None or dev_uname == None: return for o_dev_type, o_dev_info in defined_devices_sxpr: if o_dev_type == 'vbd' or o_dev_type == 'tap': blkdev_file = blkdev_uname_to_file(dev_uname) o_dev_uname = sxp.child_value(o_dev_info, 'uname') if o_dev_uname != None: o_blkdev_file = blkdev_uname_to_file(o_dev_uname) if blkdev_file == o_blkdev_file: raise XendConfigError('The file "%s" is already used' % blkdev_file) o_blkdev_name = sxp.child_value(o_dev_info, 'dev') o_devid = self._blkdev_name_to_number(o_blkdev_name) if o_devid != None and devid == o_devid: raise XendConfigError('The device "%s" is already defined' % blkdev_name) elif dev_type == 'vif': dev_mac = dev_info.get('mac') for o_dev_type, o_dev_info in defined_devices_sxpr: if dev_type == o_dev_type: if dev_mac.lower() == sxp.child_value(o_dev_info, 'mac').lower(): raise XendConfigError('The mac "%s" is already defined' % dev_mac) def device_add(self, dev_type, cfg_sxp = None, cfg_xenapi = None, target = None): """Add a device configuration in SXP format or XenAPI struct format. For SXP, it could be either: [device, [vbd, [uname ...]] or: [vbd, [uname ..]] @type cfg_sxp: list of lists (parsed sxp object) @param cfg_sxp: SXP configuration object @type cfg_xenapi: dict @param cfg_xenapi: A device configuration from Xen API (eg. vbd,vif) @param target: write device information to @type target: None or a dictionary @rtype: string @return: Assigned UUID of the device. """ if target == None: target = self if dev_type not in XendDevices.valid_devices(): raise XendConfigError("XendConfig: %s not a valid device type" % dev_type) if cfg_sxp == None and cfg_xenapi == None: raise XendConfigError("XendConfig: device_add requires some " "config.") #if cfg_sxp: # log.debug("XendConfig.device_add: %s" % str(cfg_sxp)) #if cfg_xenapi: # log.debug("XendConfig.device_add: %s" % str(cfg_xenapi)) if cfg_sxp: if sxp.child0(cfg_sxp) == 'device': config = sxp.child0(cfg_sxp) else: config = cfg_sxp dev_type = sxp.name(config) dev_info = {} if dev_type == 'pci' or dev_type == 'vscsi': pci_devs_uuid = sxp.child_value(config, 'uuid', uuid.createString()) pci_dict = self.pci_convert_sxp_to_dict(config) pci_devs = pci_dict['devs'] if dev_type != 'vscsi': # create XenAPI DPCI objects. for pci_dev in pci_devs: dpci_uuid = pci_dev.get('uuid') ppci_uuid = XendPPCI.get_by_sbdf(pci_dev['domain'], pci_dev['bus'], pci_dev['slot'], pci_dev['func']) if ppci_uuid is None: continue dpci_record = { 'VM': self['uuid'], 'PPCI': ppci_uuid, 'hotplug_slot': pci_dev.get('vslot', 0) } XendDPCI(dpci_uuid, dpci_record) target['devices'][pci_devs_uuid] = (dev_type, {'devs': pci_devs, 'uuid': pci_devs_uuid}) log.debug("XendConfig: reading device: %s" % pci_devs) return pci_devs_uuid for opt_val in config[1:]: try: opt, val = opt_val dev_info[opt] = val except (TypeError, ValueError): # unpack error pass if dev_type == 'vbd': dev_info['bootable'] = 0 if dev_info.get('dev', '').startswith('ioemu:'): dev_info['driver'] = 'ioemu' else: dev_info['driver'] = 'paravirtualised' if dev_type == 'tap': if dev_info['uname'].split(':')[1] not in blktap_disk_types: raise XendConfigError("tap:%s not a valid disk type" % dev_info['uname'].split(':')[1]) if dev_type == 'vif': if not dev_info.get('mac'): dev_info['mac'] = randomMAC() self.device_duplicate_check(dev_type, dev_info, target) if dev_type == 'vif': if dev_info.get('policy') and dev_info.get('label'): dev_info['security_label'] = "%s:%s:%s" % \ (xsconstants.ACM_POLICY_ID, dev_info['policy'],dev_info['label']) # create uuid if it doesn't exist dev_uuid = dev_info.get('uuid', None) if not dev_uuid: dev_uuid = uuid.createString() dev_info['uuid'] = dev_uuid # store dev references by uuid for certain device types target['devices'][dev_uuid] = (dev_type, dev_info) if dev_type in ('vif', 'vbd', 'vtpm'): param = '%s_refs' % dev_type if param not in target: target[param] = [] if dev_uuid not in target[param]: if dev_type == 'vbd': # Compat hack -- mark first disk bootable dev_info['bootable'] = int(not target[param]) target[param].append(dev_uuid) elif dev_type == 'tap': if 'vbd_refs' not in target: target['vbd_refs'] = [] if dev_uuid not in target['vbd_refs']: # Compat hack -- mark first disk bootable dev_info['bootable'] = int(not target['vbd_refs']) target['vbd_refs'].append(dev_uuid) elif dev_type == 'vfb': # Populate other config with aux data that is associated # with vfb other_config = {} for key in XENAPI_CONSOLE_OTHER_CFG: if key in dev_info: other_config[key] = dev_info[key] target['devices'][dev_uuid][1]['other_config'] = other_config if 'console_refs' not in target: target['console_refs'] = [] # Treat VFB devices as console devices so they are found # through Xen API if dev_uuid not in target['console_refs']: target['console_refs'].append(dev_uuid) elif dev_type == 'console': if 'console_refs' not in target: target['console_refs'] = [] if dev_uuid not in target['console_refs']: target['console_refs'].append(dev_uuid) log.debug("XendConfig: reading device: %s" % scrub_password(dev_info)) return dev_uuid if cfg_xenapi: dev_info = {} dev_uuid = '' if dev_type == 'vif': dev_info['mac'] = cfg_xenapi.get('MAC') if not dev_info['mac']: dev_info['mac'] = randomMAC() # vifname is the name on the guest, not dom0 # TODO: we don't have the ability to find that out or # change it from dom0 #if cfg_xenapi.get('device'): # don't add if blank # dev_info['vifname'] = cfg_xenapi.get('device') if cfg_xenapi.get('type'): dev_info['type'] = cfg_xenapi.get('type') if cfg_xenapi.get('name'): dev_info['name'] = cfg_xenapi.get('name') if cfg_xenapi.get('network'): network = XendAPIStore.get( cfg_xenapi.get('network'), 'network') dev_info['bridge'] = network.get_name_label() if cfg_xenapi.get('security_label'): dev_info['security_label'] = \ cfg_xenapi.get('security_label')
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?