示例#1
0
def _make_guest(conn=None, os_variant=None):
    if not conn:
        conn = utils.URIs.open_testdriver_cached()

    g = virtinst.Guest(conn)
    g.name = "TestGuest"
    g.currentMemory = int(200 * 1024)
    g.memory = int(400 * 1024)

    if os_variant:
        g.set_os_name(os_variant)

    # File disk
    d = virtinst.DeviceDisk(conn)
    d.path = "/dev/default-pool/new-test-suite.img"
    if d.wants_storage_creation():
        parent_pool = d.get_parent_pool()
        vol_install = virtinst.DeviceDisk.build_vol_install(conn,
            os.path.basename(d.path), parent_pool, .0000001, True)
        d.set_vol_install(vol_install)
    d.validate()
    g.add_device(d)

    # Block disk
    d = virtinst.DeviceDisk(conn)
    d.path = "/dev/disk-pool/diskvol1"
    d.validate()
    g.add_device(d)

    # Network device
    dev = virtinst.DeviceInterface(conn)
    g.add_device(dev)

    return g
示例#2
0
def parse_netdev_entry(conn, ifaces, fullkey, value):
    """
    Parse a particular key/value for a network.  Throws ValueError.
    """
    ignore, ignore, inst, key = re.split("^(ethernet)([0-9]+).", fullkey)
    lvalue = value.lower()

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

    net = None
    for checkiface in ifaces:
        if getattr(checkiface, "vmx_inst") == inst:
            net = checkiface
            break
    if not net:
        net = virtinst.DeviceInterface(conn)
        setattr(net, "vmx_inst", inst)
        net.set_default_source()
        ifaces.append(net)

    if key == "virtualdev":
        # "vlance", "vmxnet", "e1000"
        if lvalue in ["e1000"]:
            net.model = lvalue
    if key == "addresstype" and lvalue == "generated":
        # Autogenerate a MAC address, the default
        pass
    if key == "address":
        # we ignore .generatedAddress for auto mode
        net.macaddr = lvalue
    return net, inst
示例#3
0
def testAddRemoveDevices():
    conn = utils.URIs.open_testdefault_cached()
    guest, outfile = _get_test_content(conn, "add-devices")

    # Basic removal of existing device
    rmdev = guest.devices.disk[2]
    guest.remove_device(rmdev)

    # Basic device add
    d = virtinst.DeviceWatchdog(conn)
    d.set_defaults(guest)
    guest.add_device(d)

    # Test adding device with child properties (address value)
    adddev = virtinst.DeviceInterface(conn)
    adddev.type = "network"
    adddev.source = "default"
    adddev.macaddr = "1A:2A:3A:4A:5A:6A"
    adddev.address.type = "spapr-vio"
    adddev.set_defaults(guest)

    # Test adding and removing the same device
    guest.add_device(adddev)
    guest.remove_device(adddev)
    guest.add_device(adddev)

    # Test adding device built from parsed XML
    guest.add_device(
        virtinst.DeviceSound(conn, parsexml="""<sound model='pcspk'/>"""))

    _alter_compare(conn, guest.get_xml(), outfile)
示例#4
0
    def validate_network(self, macaddr, model=None):
        nettype, devname, mode, portgroup = self.get_network_selection()
        if nettype is None:
            return None

        net = None

        # Make sure VirtualNetwork is running
        netobj = None
        if nettype == virtinst.DeviceInterface.TYPE_VIRTUAL:
            for net in self.conn.list_nets():
                if net.get_name() == devname:
                    netobj = net
                    break

        if netobj and not netobj.is_active():
            res = self.err.yes_no(
                _("Virtual Network is not active."),
                _("Virtual Network '%s' is not active. "
                  "Would you like to start the network "
                  "now?") % devname)
            if not res:
                return False

            # Try to start the network
            try:
                netobj.start()
                logging.info("Started network '%s'", devname)
            except Exception as e:
                return self.err.show_err(
                    _("Could not start virtual network "
                      "'%s': %s") % (devname, str(e)))

        # Create network device
        try:
            net = virtinst.DeviceInterface(self.conn.get_backend())
            net.type = nettype
            net.source = devname
            net.macaddr = macaddr
            net.model = model
            net.source_mode = mode
            net.portgroup = portgroup
            if net.model == "spapr-vlan":
                net.address.set_addrstr("spapr-vio")

            if net.type == "direct":
                (vport_type, vport_managerid, vport_typeid, vport_idver,
                 vport_instid) = self.get_vport()

                net.virtualport.type = vport_type or None
                net.virtualport.managerid = vport_managerid or None
                net.virtualport.typeid = vport_typeid or None
                net.virtualport.typeidversion = vport_idver or None
                net.virtualport.instanceid = vport_instid or None

            net.validate()
        except Exception as e:
            return self.err.val_err(_("Error with network parameters."), e)
        return net
示例#5
0
    def build_device(self, macaddr, model=None):
        nettype, devname, mode = self.get_network_selection()

        net = virtinst.DeviceInterface(self.conn.get_backend())
        net.type = nettype
        net.source = devname
        net.macaddr = macaddr
        net.model = model
        net.source_mode = mode

        return net
示例#6
0
    def testDefaultBridge(self):
        # Test our handling of the default bridge routines
        from virtinst.devices import interface as deviceinterface
        origfunc = getattr(deviceinterface, "_default_bridge")
        try:

            def newbridge(ignore_conn):
                return "bzz0"

            setattr(deviceinterface, "_default_bridge", newbridge)

            dev1 = virtinst.DeviceInterface(self.conn)
            dev1.macaddr = "22:22:33:44:55:66"

            dev2 = virtinst.DeviceInterface(self.conn,
                                            parsexml=dev1.get_xml_config())
            dev2.source = None
            dev2.source = "foobr0"
            dev2.macaddr = "22:22:33:44:55:67"

            dev3 = virtinst.DeviceInterface(self.conn,
                                            parsexml=dev1.get_xml_config())
            dev3.source = None
            dev3.macaddr = "22:22:33:44:55:68"

            utils.diff_compare(
                dev1.get_xml_config(), None, "<interface type=\"bridge\">\n"
                "  <source bridge=\"bzz0\"/>\n"
                "  <mac address=\"22:22:33:44:55:66\"/>\n"
                "</interface>\n")
            utils.diff_compare(
                dev2.get_xml_config(), None, "<interface type=\"bridge\">\n"
                "  <source bridge=\"foobr0\"/>\n"
                "  <mac address=\"22:22:33:44:55:67\"/>\n"
                "</interface>\n")
            utils.diff_compare(
                dev3.get_xml_config(), None, "<interface type=\"bridge\">\n"
                "  <mac address=\"22:22:33:44:55:68\"/>\n"
                "</interface>\n")
        finally:
            setattr(deviceinterface, "_default_bridge", origfunc)
示例#7
0
    def build_device(self, macaddr, model=None):
        nettype, devname, mode, portgroup = self.get_network_selection()

        net = virtinst.DeviceInterface(self.conn.get_backend())
        net.type = nettype
        net.source = devname
        net.macaddr = macaddr
        net.model = model
        net.source_mode = mode
        net.portgroup = portgroup
        if net.model == "spapr-vlan":
            net.address.set_addrstr("spapr-vio")

        if net.type == "direct":
            (vport_type, vport_managerid, vport_typeid, vport_idver,
             vport_instid) = self.get_vport()

            net.virtualport.type = vport_type or None
            net.virtualport.managerid = vport_managerid or None
            net.virtualport.typeid = vport_typeid or None
            net.virtualport.typeidversion = vport_idver or None
            net.virtualport.instanceid = vport_instid or None

        return net
示例#8
0
def _make_guest(conn=None, os_variant=None):
    if not conn:
        conn = utils.URIs.open_testdriver_cached()

    g = virtinst.Guest(conn)
    g.type = "kvm"
    g.name = "TestGuest"
    g.memory = int(200 * 1024)
    g.maxmemory = int(400 * 1024)
    g.uuid = "12345678-1234-1234-1234-123456789012"
    gdev = virtinst.DeviceGraphics(conn)
    gdev.type = "vnc"
    gdev.keymap = "ja"
    g.add_device(gdev)
    g.features.pae = False
    g.vcpus = 5

    g.emulator = "/usr/lib/xen/bin/qemu-dm"
    g.os.arch = "i686"
    g.os.os_type = "hvm"

    if os_variant:
        g.set_os_name(os_variant)

    # Floppy disk
    path = "/dev/default-pool/testvol1.img"
    d = DeviceDisk(conn)
    d.path = path
    d.device = d.DEVICE_FLOPPY
    d.validate()
    g.add_device(d)

    # File disk
    path = "/dev/default-pool/new-test-suite.img"
    d = virtinst.DeviceDisk(conn)
    d.path = path

    if d.wants_storage_creation():
        parent_pool = d.get_parent_pool()
        vol_install = virtinst.DeviceDisk.build_vol_install(conn,
            os.path.basename(path), parent_pool, .0000001, True)
        d.set_vol_install(vol_install)

    d.validate()
    g.add_device(d)

    # Block disk
    path = "/dev/disk-pool/diskvol1"
    d = virtinst.DeviceDisk(conn)
    d.path = path
    d.validate()
    g.add_device(d)

    # Network device
    dev = virtinst.DeviceInterface(conn)
    dev.macaddr = "22:22:33:44:55:66"
    dev.type = virtinst.DeviceInterface.TYPE_VIRTUAL
    dev.source = "default"
    g.add_device(dev)

    return g
示例#9
0
文件: ovf.py 项目: wsszh/virt-manager
def _import_file(conn, input_file):
    """
    Parse the OVF file and generate a virtinst.Guest object from it
    """
    root = xml.etree.ElementTree.parse(input_file).getroot()
    vsnode = _find(root, "./ovf:VirtualSystem")
    vhnode = _find(vsnode, "./ovf:VirtualHardwareSection")

    # General info
    name = _text(vsnode.find("./ovf:Name", OVF_NAMESPACES))
    desc = _text(
        vsnode.find("./ovf:AnnotationSection/ovf:Annotation", OVF_NAMESPACES))
    if not desc:
        desc = _text(vsnode.find("./ovf:Description", OVF_NAMESPACES))

    vhxpath = "./ovf:Item[rasd:ResourceType='%s']"
    vcpus = _text(
        _find(vhnode, (vhxpath % DEVICE_CPU) + "/rasd:VirtualQuantity"))
    mem = _text(
        _find(vhnode, (vhxpath % DEVICE_MEMORY) + "/rasd:VirtualQuantity"))
    alloc_mem = _text(
        _find(vhnode, (vhxpath % DEVICE_MEMORY) + "/rasd:AllocationUnits"))

    # Sections that we handle
    # NetworkSection is ignored, since I don't have an example of
    # a valid section in the wild.
    parsed_sections = [
        "References", "DiskSection", "NetworkSection", "VirtualSystem"
    ]

    # Check for unhandled 'required' sections
    for env_node in root.findall("./"):
        if any([p for p in parsed_sections if p in env_node.tag]):
            continue

        logging.debug("Unhandled XML section '%s'", env_node.tag)

        if not _convert_bool_val(env_node.attrib.get("required")):
            continue
        raise Exception(
            _("OVF section '%s' is listed as "
              "required, but parser doesn't know "
              "how to handle it.") % env_node.name)

    disk_buses = {}
    for node in _findall(vhnode, vhxpath % DEVICE_IDE_BUS):
        instance_id = _text(_find(node, "rasd:InstanceID"))
        disk_buses[instance_id] = "ide"
    for node in _findall(vhnode, vhxpath % DEVICE_SCSI_BUS):
        instance_id = _text(_find(node, "rasd:InstanceID"))
        disk_buses[instance_id] = "scsi"

    ifaces = []
    for node in _findall(vhnode, vhxpath % DEVICE_ETHERNET):
        iface = virtinst.DeviceInterface(conn)
        # Just ignore 'source' info for now and choose the default
        net_model = _text(_find(node, "rasd:ResourceSubType"))
        if net_model and not net_model.isdigit():
            iface.model = net_model.lower()
        iface.set_default_source()
        ifaces.append(iface)

    disks = []
    for node in _findall(vhnode, vhxpath % DEVICE_DISK):
        bus_id = _text(_find(node, "rasd:Parent"))
        path = _text(_find(node, "rasd:HostResource"))

        bus = disk_buses.get(bus_id, "ide")
        fmt = "raw"

        if path:
            path = _lookup_disk_path(root, path)
            fmt = "vmdk"

        disk = virtinst.DeviceDisk(conn)
        disk.path = path
        disk.driver_type = fmt
        disk.bus = bus
        disk.device = "disk"
        disks.append(disk)

    # Generate the Guest
    guest = conn.caps.lookup_virtinst_guest()
    guest.installer = virtinst.ImportInstaller(conn)

    if not name:
        name = os.path.basename(input_file)

    guest.name = name.replace(" ", "_")
    guest.description = desc or None
    if vcpus:
        guest.vcpus = int(vcpus)

    if mem:
        guest.memory = _convert_alloc_val(alloc_mem, mem) * 1024

    for dev in ifaces + disks:
        guest.add_device(dev)

    return guest