예제 #1
0
파일: vmx.py 프로젝트: cardoe/virt-manager
def parse_disk_entry(vm, fullkey, value):
    """
    Parse a particular key/value for a disk.  FIXME: this should be a
    lot smarter.
    """

    # skip bus values, e.g. 'scsi0.present = "TRUE"'
    if re.match(r"^(scsi|ide)[0-9]+[^:]", fullkey):
        return

    ignore, bus, bus_nr, inst, key = re.split(r"^(scsi|ide)([0-9]+):([0-9]+)\.",
        fullkey)

    lvalue = value.lower()

    if key == "present" and lvalue == "false":
        return

    # Does anyone else think it's scary that we're still doing things
    # like this?
    if bus == "ide":
        inst = int(bus_nr) * 2 + (int(inst) % 2)
    elif bus == "scsi":
        inst = int(bus_nr) * 16 + (int(inst) % 16)


    devid = (bus, inst)
    if not vm.disks.get(devid):
        vm.disks[devid] = diskcfg.disk(bus=bus,
            typ=diskcfg.DISK_TYPE_DISK)
    disk = vm.disks[devid]

    if key == "devicetype":
        if lvalue == "atapi-cdrom" or lvalue == "cdrom-raw":
            disk.type = diskcfg.DISK_TYPE_CDROM
        elif lvalue == "cdrom-image":
            disk.type = diskcfg.DISK_TYPE_ISO

    if key == "filename":
        disk.path = value
        disk.format = diskcfg.DISK_FORMAT_RAW
        if lvalue.endswith(".vmdk"):
            disk.format = diskcfg.DISK_FORMAT_VMDK
            # See if the filename is actually a VMDK descriptor file
            parse_vmdk(disk, disk.path)
예제 #2
0
def _parse_hw_section(vm, nodes, file_refs, disk_section):
    vm.nr_vcpus = 0
    disk_buses = {}

    for device_node in nodes:
        if device_node.name != "Item":
            continue

        devtype = None
        for item_node in node_list(device_node):
            if item_node.name == "ResourceType":
                devtype = item_node.content

        if devtype == DEVICE_CPU:
            cpus = get_child_content(device_node, "VirtualQuantity")
            if cpus:
                vm.nr_vcpus += int(cpus)

        elif devtype == DEVICE_MEMORY:
            mem = get_child_content(device_node, "VirtualQuantity")
            alloc_str = get_child_content(device_node, "AllocationUnits")
            if mem:
                vm.memory = convert_alloc_val(alloc_str, mem)

        elif devtype == DEVICE_ETHERNET:
            net_model = get_child_content(device_node, "ResourceSubType")
            if net_model:
                net_model = net_model.lower()
            netdev = netdevcfg.netdev(driver=net_model)
            vm.netdevs[len(vm.netdevs)] = netdev

        elif devtype == DEVICE_IDE_BUS:
            instance_id = get_child_content(device_node, "InstanceID")
            disk_buses[instance_id] = "ide"

        elif devtype == DEVICE_SCSI_BUS:
            instance_id = get_child_content(device_node, "InstanceID")
            disk_buses[instance_id] = "scsi"

        elif devtype in [DEVICE_DISK]:
            bus_id = get_child_content(device_node, "Parent")
            path = get_child_content(device_node, "HostResource")

            dev_num = int(get_child_content(device_node, "AddressOnParent"))

            if bus_id and bus_id not in disk_buses:
                raise ValueError(_("Didn't find parent bus for disk '%s'" %
                                 path))

            bus = (bus_id and disk_buses[bus_id]) or "ide"

            fmt = diskcfg.DISK_FORMAT_RAW

            if path:
                ref = None
                fmt = diskcfg.DISK_FORMAT_VMDK

                if path.startswith("ovf:/disk/"):
                    disk_ref = path[len("ovf:/disk/"):]
                    if disk_ref not in disk_section:
                        raise ValueError(_("Unknown reference id '%s' "
                                           "for path %s.") % (path, ref))

                    ref, fmt = disk_section[disk_ref]

                elif path.startswith("ovf:/file/"):
                    ref = path[len("ovf:/file/"):]

                else:
                    raise ValueError(_("Unknown storage path type %s." % path))

                if not ref:
                    # XXX: This means allocate the disk.
                    pass

                if ref not in file_refs:
                    raise ValueError(_("Unknown reference id '%s' "
                                       "for path %s.") % (path, ref))

                path = file_refs[ref]

            disk = diskcfg.disk(path=path, fmt=fmt, bus=bus,
                                typ=diskcfg.DISK_TYPE_DISK)

            vm.disks[(bus, dev_num)] = disk

        else:
            desc = get_child_content(device_node, "Description")
            logging.debug("Unhandled device type=%s desc=%s", devtype, desc)
예제 #3
0
class virtimage_parser(formats.parser):
    """
    Support for virt-install's image format (see virt-image man page).
    """
    name = "virt-image"
    suffix = ".virt-image.xml"
    can_import = True
    can_export = True
    can_identify = True

    @staticmethod
    def identify_file(input_file):
        """
        Return True if the given file is of this format.
        """
        try:
            ImageParser.parse_file(input_file)
        except ImageParser.ParserException:
            return False
        return True

    @staticmethod
    def import_file(input_file):
        """
        Import a configuration file.  Raises if the file couldn't be
        opened, or parsing otherwise failed.
        """
        vm = vmcfg.vm()
        try:
            config  = ImageParser.parse_file(input_file)
        except Exception, e:
            raise ValueError(_("Couldn't import file '%s': %s") %
                             (input_file, e))

        domain = config.domain
        boot = domain.boots[0]

        if not config.name:
            raise ValueError(_("No Name defined in '%s'") % input_file)
        vm.name = config.name
        vm.memory = int(config.domain.memory / 1024)
        if config.descr:
            vm.description = config.descr
        vm.nr_vcpus = config.domain.vcpu

        bus = "ide"
        nr_disk = 0

        for d in boot.drives:
            disk = d.disk
            fmt = None
            if disk.format == ImageParser.Disk.FORMAT_RAW:
                fmt = diskcfg.DISK_FORMAT_RAW
            elif disk.format == ImageParser.Disk.FORMAT_VMDK:
                fmt = diskcfg.DISK_FORMAT_VMDK

            if fmt is None:
                raise ValueError(_("Unable to determine disk format"))
            devid = (bus, nr_disk)
            vm.disks[devid] = diskcfg.disk(bus = bus,
                type = diskcfg.DISK_TYPE_DISK)
            vm.disks[devid].format = fmt
            vm.disks[devid].path = disk.file
            nr_disk = nr_disk + 1

        nics = domain.interface
        nic_idx = 0
        while nic_idx in range(0, nics):
            vm.netdevs[nic_idx] = netdevcfg.netdev(type = netdevcfg.NETDEV_TYPE_UNKNOWN)
            nic_idx = nic_idx + 1
            """  Eventually need to add support for mac addresses if given"""
        vm.validate()
        return vm
예제 #4
0
def _parse_hw_section(vm, nodes, file_refs, disk_section):
    vm.nr_vcpus = 0
    disk_buses = {}

    for device_node in nodes:
        if device_node.name != "Item":
            continue

        devtype = None
        for item_node in node_list(device_node):
            if item_node.name == "ResourceType":
                devtype = item_node.content

        if devtype == DEVICE_CPU:
            cpus = get_child_content(device_node, "VirtualQuantity")
            if cpus:
                vm.nr_vcpus += int(cpus)

        elif devtype == DEVICE_MEMORY:
            mem = get_child_content(device_node, "VirtualQuantity")
            alloc_str = get_child_content(device_node, "AllocationUnits")
            if mem:
                vm.memory = convert_alloc_val(alloc_str, mem)

        elif devtype == DEVICE_ETHERNET:
            net_model = get_child_content(device_node, "ResourceSubType")
            if net_model:
                net_model = net_model.lower()
            netdev = netdevcfg.netdev(driver=net_model)
            vm.netdevs[len(vm.netdevs)] = netdev

        elif devtype == DEVICE_IDE_BUS:
            instance_id = get_child_content(device_node, "InstanceID")
            disk_buses[instance_id] = "ide"

        elif devtype == DEVICE_SCSI_BUS:
            instance_id = get_child_content(device_node, "InstanceID")
            disk_buses[instance_id] = "scsi"

        elif devtype in [DEVICE_DISK]:
            bus_id = get_child_content(device_node, "Parent")
            path = get_child_content(device_node, "HostResource")

            dev_num = int(get_child_content(device_node, "AddressOnParent"))

            if bus_id and bus_id not in disk_buses:
                raise ValueError(
                    _("Didn't find parent bus for disk '%s'" % path))

            bus = (bus_id and disk_buses[bus_id]) or "ide"

            fmt = diskcfg.DISK_FORMAT_RAW

            if path:
                ref = None
                fmt = diskcfg.DISK_FORMAT_VMDK

                if path.startswith("ovf:/disk/"):
                    disk_ref = path[len("ovf:/disk/"):]
                    if disk_ref not in disk_section:
                        raise ValueError(
                            _("Unknown reference id '%s' "
                              "for path %s.") % (path, ref))

                    ref, fmt = disk_section[disk_ref]

                elif path.startswith("ovf:/file/"):
                    ref = path[len("ovf:/file/"):]

                else:
                    raise ValueError(_("Unknown storage path type %s." % path))

                if not ref:
                    # XXX: This means allocate the disk.
                    pass

                if ref not in file_refs:
                    raise ValueError(
                        _("Unknown reference id '%s' "
                          "for path %s.") % (path, ref))

                path = file_refs[ref]

            disk = diskcfg.disk(path=path,
                                format=fmt,
                                bus=bus,
                                type=diskcfg.DISK_TYPE_DISK)

            vm.disks[(bus, dev_num)] = disk

        else:
            desc = get_child_content(device_node, "Description")
            logging.debug("Unhandled device type=%s desc=%s", devtype, desc)
예제 #5
0
class virtimage_parser(formats.parser):
    """
    Support for virt-install's image format (see virt-image man page).
    """
    name = "virt-image"
    suffix = ".virt-image.xml"
    can_import = True
    can_export = True
    can_identify = True

    @staticmethod
    def identify_file(input_file):
        """
        Return True if the given file is of this format.
        """
        try:
            f = file(input_file, "r")
            output = f.read()
            f.close()

            ImageParser.parse(output, input_file)
        except ImageParser.ParserException:
            return False
        return True

    @staticmethod
    def import_file(input_file):
        """
        Import a configuration file.  Raises if the file couldn't be
        opened, or parsing otherwise failed.
        """
        vm = vmcfg.vm()
        try:
            f = file(input_file, "r")
            output = f.read()
            f.close()

            logging.debug("Importing virt-image XML:\n%s", output)
            config = ImageParser.parse(output, input_file)
        except Exception, e:
            raise ValueError(
                _("Couldn't import file '%s': %s") % (input_file, e))

        domain = config.domain
        boot = domain.boots[0]

        if not config.name:
            raise ValueError(_("No Name defined in '%s'") % input_file)
        vm.name = config.name
        vm.arch = boot.arch
        vm.memory = int(config.domain.memory / 1024)
        if config.descr:
            vm.description = config.descr
        vm.nr_vcpus = config.domain.vcpu

        bus = "ide"
        nr_disk = 0

        for d in boot.drives:
            disk = d.disk
            format_mappings = {
                ImageParser.Disk.FORMAT_RAW: diskcfg.DISK_FORMAT_RAW,
                ImageParser.Disk.FORMAT_VMDK: diskcfg.DISK_FORMAT_VMDK,
                ImageParser.Disk.FORMAT_QCOW: diskcfg.DISK_FORMAT_QCOW,
                ImageParser.Disk.FORMAT_QCOW2: diskcfg.DISK_FORMAT_QCOW2,
                ImageParser.Disk.FORMAT_VDI: diskcfg.DISK_FORMAT_VDI,
            }

            fmt = None
            if disk.format in format_mappings:
                fmt = format_mappings[disk.format]
            else:
                raise ValueError(_("Unknown disk format '%s'"), disk.format)

            devid = (bus, nr_disk)
            vm.disks[devid] = diskcfg.disk(bus=bus, typ=diskcfg.DISK_TYPE_DISK)
            vm.disks[devid].format = fmt
            vm.disks[devid].path = disk.file
            nr_disk = nr_disk + 1

        nics = domain.interface
        nic_idx = 0
        while nic_idx in range(0, nics):
            # XXX Eventually need to add support for mac addresses if given
            vm.netdevs[nic_idx] = netdevcfg.netdev(
                typ=netdevcfg.NETDEV_TYPE_UNKNOWN)
            nic_idx = nic_idx + 1
        vm.validate()
        return vm