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

📄 xenapi_create.py

📁 xen虚拟机源代码安装包
💻 PY
📖 第 1 页 / 共 3 页
字号:
                })        else:            hvm = vm.getElementsByTagName("hvm")[0]            vm_record.update({                "HVM_boot_policy":                    get_child_node_attribute(vm, "hvm", "boot_policy"),                "HVM_boot_params":                    get_child_nodes_as_dict(hvm, "boot_param", "key", "value")                })        try:            vm_ref = server.xenapi.VM.create(vm_record)        except:            traceback.print_exc()            sys.exit(-1)        try:            # Now create vbds            vbds = vm.getElementsByTagName("vbd")            self.create_vbds(vm_ref, vbds, vdis)            # Now create vifs            vifs = vm.getElementsByTagName("vif")            self.create_vifs(vm_ref, vifs, networks)            # Now create vtpms            vtpms = vm.getElementsByTagName("vtpm")            self.create_vtpms(vm_ref, vtpms)            # Now create consoles            consoles = vm.getElementsByTagName("console")            self.create_consoles(vm_ref, consoles)            # Now create pcis            pcis = vm.getElementsByTagName("pci")            self.create_pcis(vm_ref, pcis)            return vm_ref        except:            server.xenapi.VM.destroy(vm_ref)            raise            def create_vbds(self, vm_ref, vbds, vdis):        log(DEBUG, "create_vbds")        return map(lambda vbd: self.create_vbd(vm_ref, vbd, vdis), vbds)    def create_vbd(self, vm_ref, vbd, vdis):        log(DEBUG, "create_vbd")        vbd_record = {            "VM":                vm_ref,            "VDI":                vdis[vbd.attributes["vdi"].value],            "device":                vbd.attributes["device"].value,            "bootable":                vbd.attributes["bootable"].value == "1",            "mode":                vbd.attributes["mode"].value,            "type":                vbd.attributes["type"].value,            "qos_algorithm_type":                vbd.attributes["qos_algorithm_type"].value,            "qos_algorithm_params":                get_child_nodes_as_dict(vbd,                  "qos_algorithm_param", "key", "value")            }        return server.xenapi.VBD.create(vbd_record)    def create_vifs(self, vm_ref, vifs, networks):        log(DEBUG, "create_vifs")        return map(lambda vif: self.create_vif(vm_ref, vif, networks), vifs)    def create_vif(self, vm_ref, vif, networks):        log(DEBUG, "create_vif")        if 'network' in vif.attributes.keys():            network_name = vif.attributes['network'].value            if network_name in networks.keys():                network_uuid = networks[network_name]            else:                networks = dict([(record['name_label'], ref)                                 for ref, record in                                 server.xenapi.network.get_all_records().items()])                if network_name in networks.keys():                    network_uuid = networks[network_name]                else:                    raise OptionError("Network %s doesn't exist"                                  % vif.attributes["network"].value)        else:            network_uuid = self._get_network_ref()        vif_record = {            "device":                vif.attributes["device"].value,            "network":                network_uuid,            "VM":                vm_ref,            "MAC":                vif.attributes["mac"].value,            "MTU":                vif.attributes["mtu"].value,            "qos_algorithm_type":                vif.attributes["qos_algorithm_type"].value,            "qos_algorithm_params":                get_child_nodes_as_dict(vif,                    "qos_algorithm_param", "key", "value"),            "security_label":                vif.attributes["security_label"].value        }        return server.xenapi.VIF.create(vif_record)    _network_refs = []    def _get_network_ref(self):        try:            return self._network_refs.pop(0)        except IndexError:            self._network_refs = server.xenapi.network.get_all()            return self._network_refs.pop(0)    def create_vtpms(self, vm_ref, vtpms):        if len(vtpms) > 1:            vtpms = [ vtpms[0] ]        log(DEBUG, "create_vtpms")        return map(lambda vtpm: self.create_vtpm(vm_ref, vtpm), vtpms)    def create_vtpm(self, vm_ref, vtpm):        vtpm_record = {            "VM":                vm_ref,            "backend":                vtpm.attributes["backend"].value        }        return server.xenapi.VTPM.create(vtpm_record)    def create_consoles(self, vm_ref, consoles):        log(DEBUG, "create_consoles")        return map(lambda console: self.create_console(vm_ref, console),                   consoles)    def create_console(self, vm_ref, console):        log(DEBUG, "create_consoles")        console_record = {            "VM":                vm_ref,            "protocol":                console.attributes["protocol"].value,            "other_config":                get_child_nodes_as_dict(console,                  "other_config", "key", "value")            }        return server.xenapi.console.create(console_record)    def create_pcis(self, vm_ref, pcis):        log(DEBUG, "create_pcis")        return map(lambda pci: self.create_pci(vm_ref, pci), pcis)    def create_pci(self, vm_ref, pci):        log(DEBUG, "create_pci")        domain = int(pci.attributes["domain"].value, 16)        bus = int(pci.attributes["bus"].value, 16)        slot = int(pci.attributes["slot"].value, 16)        func = int(pci.attributes["func"].value, 16)        name = "%04x:%02x:%02x.%01x" % (domain, bus, slot, func)        target_ref = None        for ppci_ref in server.xenapi.PPCI.get_all():            if name == server.xenapi.PPCI.get_name(ppci_ref):                target_ref = ppci_ref                break        if target_ref is None:            log(DEBUG, "create_pci: pci device not found")            return None        dpci_record = {            "VM":                vm_ref,            "PPCI":                target_ref,            "hotplug_slot":                int(pci.attributes["func"].value, 16)        }        return server.xenapi.DPCI.create(dpci_record)def get_child_by_name(exp, childname, default = None):    try:        return [child for child in sxp.children(exp)                if child[0] == childname][0][1]    except:        return default# Convert old sxp into new xmlclass sxp2xml:    def convert_sxp_to_xml(self, config, transient=False):               devices = [child for child in sxp.children(config)                   if len(child) > 0 and child[0] == "device"]                           vbds_sxp = map(lambda x: x[1], [device for device in devices                                        if device[1][0] in ("vbd", "tap")])        vifs_sxp = map(lambda x: x[1], [device for device in devices                                        if device[1][0] == "vif"])        vtpms_sxp = map(lambda x: x[1], [device for device in devices                                         if device[1][0] == "vtpm"])        vfbs_sxp = map(lambda x: x[1], [device for device in devices                                        if device[1][0] == "vfb"])        pcis_sxp = map(lambda x: x[1], [device for device in devices                                        if device[1][0] == "pci"])        # Create XML Document                impl = getDOMImplementation()        document = impl.createDocument(None, "xm", None)        # Lets make the VM tag..        vm = document.createElement("vm")        # Some string compatibility        actions_after_shutdown \            = get_child_by_name(config, "on_poweroff", "destroy")        actions_after_reboot \            = get_child_by_name(config, "on_reboot", "restart")        actions_after_crash \            = get_child_by_name(config, "on_crash", "restart")        def conv_chk(val, vals):            val.replace("-", "_")            if val not in vals:                raise "Invalid value: " + val            else:                return val        actions_after_shutdown = conv_chk(actions_after_shutdown,\                                          XEN_API_ON_NORMAL_EXIT)        actions_after_reboot   = conv_chk(actions_after_reboot, \                                          XEN_API_ON_NORMAL_EXIT)        actions_after_crash    = conv_chk(actions_after_crash, \                                          XEN_API_ON_CRASH_BEHAVIOUR)        # Flesh out tag attributes                    vm.attributes["is_a_template"] = "false"        vm.attributes["auto_power_on"] = "false"        vm.attributes["actions_after_shutdown"] \            = actions_after_shutdown                      vm.attributes["actions_after_reboot"] \            = actions_after_reboot        vm.attributes["actions_after_crash"] \            = actions_after_crash        vm.attributes["PCI_bus"] = ""        vm.attributes["vcpus_max"] \            = str(get_child_by_name(config, "vcpus", 1))        vm.attributes["vcpus_at_startup"] \            = str(get_child_by_name(config, "vcpus", 1))        sec_data = get_child_by_name(config, "security")        if sec_data:            try :                vm.attributes['security_label'] = \                                    security.set_security_label(sec_data[0][1][1],sec_data[0][2][1])            except Exception, e:                raise "Invalid security data format: %s" % str(sec_data)        # Make the name tag        vm.appendChild(self.make_name_tag(            get_child_by_name(config, "name"), document))        # Make version tag        version = document.createElement("version")        version.appendChild(document.createTextNode("0"))        vm.appendChild(version)                # Make pv or hvm tag        image = get_child_by_name(config, "image")        if image[0] == "linux":            pv = document.createElement("pv")            pv.attributes["kernel"] \                = get_child_by_name(image, "kernel", "")            pv.attributes["bootloader"] \                = get_child_by_name(config, "bootloader", "")            pv.attributes["ramdisk"] \                = get_child_by_name(image, "ramdisk", "")            pv.attributes["args"] \                = "root=" + get_child_by_name(image, "root", "") \                + " " + get_child_by_name(image, "args", "")            pv.attributes["bootloader_args"] \                = get_child_by_name(config, "bootloader_args","")            vm.appendChild(pv)        elif image[0] == "hvm":            hvm = document.createElement("hvm")            hvm.attributes["boot_policy"] = "BIOS order"            boot_order = document.createElement("boot_param")            boot_order.attributes["key"] = "order"            boot_order.attributes["value"] \                = get_child_by_name(image, "boot", "abcd")            hvm.appendChild

⌨️ 快捷键说明

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