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

📄 xendconfig.py

📁 xen 3.2.2 源码
💻 PY
📖 第 1 页 / 共 5 页
字号:
        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:                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')                    # Ignore a null cdrom definition string                    if o_dev_uname is None:                        continue                     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 = {}            # Parsing the device SXP's. In most cases, the SXP looks            # like this:            #            # [device, [vif, [mac, xx:xx:xx:xx:xx:xx], [ip 1.3.4.5]]]            #            # However, for PCI devices it looks like this:            #            # [device, [pci, [dev, [domain, 0], [bus, 0], [slot, 1]]]]            #            # It seems the reasoning for this difference is because            # pciif.py needs all the PCI device configurations at            # the same time when creating the devices.            #            # To further complicate matters, Xen 2.0 configuration format            # uses the following for pci device configuration:            #            # [device, [pci, [domain, 0], [bus, 0], [dev, 1], [func, 2]]]            if dev_type == 'pci':                pci_devs_uuid = sxp.child_value(config, 'uuid',                                                uuid.createString())                pci_devs = []                for pci_dev in sxp.children(config, 'dev'):                    pci_dev_info = {}                    for opt_val in pci_dev[1:]:                        try:                            opt, val = opt_val                            pci_dev_info[opt] = val                        except TypeError:                            pass                    pci_devs.append(pci_dev_info)                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')                                dev_uuid = cfg_xenapi.get('uuid', None)                if not dev_uuid:                    dev_uuid = uuid.createString()                dev_info['uuid'] = dev_uuid                target['devices'][dev_uuid] = (dev_type, dev_info)                target['vif_refs'].append(dev_uuid)                        elif dev_type in ('vbd', 'tap'):                dev_info['type'] = cfg_xenapi.get('type', 'Disk')                if dev_info['type'] == 'CD':                    old_vbd_type = 'cdrom'                else:                    old_vbd_type = 'disk'                                    dev_info['uname'] = cfg_xenapi.get('image', '')                dev_info['dev'] = '%s:%s' % (cfg_xenapi.get('device'),                                             old_vbd_type)                dev_info['bootable'] = int(cfg_xenapi.get('bootable', 0))                dev_info['driver'] = cfg_xenapi.get('driver', '')                dev_info['VDI'] = cfg_xenapi.get('VDI', '')                                    if cfg_xenapi.get('mode') == 'RW':                    dev_info['mode'] = 'w'                else:                    dev_info['mode'] = 'r'                dev_uuid = cfg_xenapi.get('uuid', None)                if not dev_uuid:                    dev_uuid = uuid.createString()                dev_info['uuid'] = dev_uuid                target['devices'][dev_uuid] = (dev_type, dev_info)                target['vbd_refs'].append(dev_uuid)                            elif dev_type == 'vtpm':                if cfg_xenapi.get('type'):                    dev_info['type'] = cfg_xenapi.get('type')                dev_uuid = cfg_xenapi.get('uuid', None)                if not dev_uuid:                    dev_uuid = uuid.createString()                dev_info['uuid'] = dev_uuid                dev_info['other_config'] = cfg_xenapi.get('other_config', {})                target['devices'][dev_uuid] = (dev_type, dev_info)                target['vtpm_refs'].append(dev_uuid)            elif dev_type == 'console':                dev_uuid = cfg_xenapi.get('uuid', None)                if not dev_uuid:                    dev_uuid = uuid.createString()                dev_info['uuid'] = dev_uuid                dev_info['protocol'] = cfg_xenapi.get('protocol', 'rfb')                dev_info['other_config'] = cfg_xenapi.get('other_config', {})                if dev_info['protocol'] == 'rfb':                    # collapse other config into devinfo for things                    # such as vncpasswd, vncunused, etc.                                        dev_info.update(cfg_xenapi.get('other_config', {}))                    dev_info['type'] = 'vnc'                                            target['devices'][dev_uuid] = ('vfb', dev_info)                    target['console_refs'].append(dev_uuid)                    # Finally, if we are a pvfb, we need to make a vkbd                    # as well that is not really exposed to Xen API                    vkbd_uuid = uuid.createString()                    target['devices'][vkbd_uuid] = ('vkbd', {})                                    elif dev_info['protocol'] == 'vt100':                    # if someone tries to create a VT100 console                    # via the Xen API, we'll have to ignore it

⌨️ 快捷键说明

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