当前位置: 首页>>代码示例>>Python>>正文


Python virsh.attach_device函数代码示例

本文整理汇总了Python中virttest.virsh.attach_device函数的典型用法代码示例。如果您正苦于以下问题:Python attach_device函数的具体用法?Python attach_device怎么用?Python attach_device使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了attach_device函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: hotplug_device

 def hotplug_device(hotplug_type, char_dev, index=1, id=0):
     if hotplug_type == "qmp":
         char_add_opt = "chardev-add "
         dev_add_opt = "device_add virtserialport,chardev="
         if char_dev == "file":
             char_add_opt += ("file,path=%s/file%s,id=file%s"
                              % (tmp_dir, index, index))
             dev_add_opt += ("file%s,name=file%s,bus=virtio-serial0.0,id=file%s"
                             % (index, index, index))
         elif char_dev == "socket":
             char_add_opt += ("socket,path=%s/socket%s,server,nowait,id=socket%s"
                              % (tmp_dir, index, index))
             dev_add_opt += ("socket%s,name=socket%s,bus=virtio-serial0.0,id=socket%s"
                             % (index, index, index))
         elif char_dev == "pty":
             char_add_opt += "pty,path=/dev/pts/%s,id=pty%s" % (id, index)
             dev_add_opt += ("pty%s,name=pty%s,bus=virtio-serial0.0,id=pty%s"
                             % (index, index, index))
         virsh.qemu_monitor_command(vm_name, char_add_opt, "--hmp")
         virsh.qemu_monitor_command(vm_name, dev_add_opt, "--hmp")
     elif hotplug_type == "attach":
         xml_file = "%s/xml_%s%s" % (tmp_dir, char_dev, index)
         if char_dev in ["file", "socket"]:
             prepare_channel_xml(xml_file, char_dev, index)
         elif char_dev == "pty":
             prepare_channel_xml(xml_file, char_dev, index, id)
         virsh.attach_device(vm_name, xml_file, flagstr="--live")
开发者ID:will-Do,项目名称:tp-libvirt_v2v,代码行数:27,代码来源:libvirt_bench_serial_hotplug.py

示例2: test_win_fibre_group

def test_win_fibre_group(vm, params):
    """
    Try to attach device in iommu group to vm.

    1.Get original available disks before attaching.
    2.Attaching hostdev in iommu group to vm.
    3.Start vm and check it.
    4.Check added disk in vm.
    """
    pci_id = params.get("fibre_pci_id", "FIBRE:PCI.EXAMPLE")
    device_type = "Fibre"
    if pci_id.count("EXAMPLE"):
        raise error.TestNAError("Invalid pci device id.")

    # Login vm to get disks before attaching pci device.
    if vm.is_dead():
        vm.start()
    before_disks = get_windows_disks(vm)
    logging.debug("Disks before:%s",
                  before_disks)
    vm.destroy()

    xmlfile = utlv.create_hostdev_xml(pci_id)
    prepare_devices(pci_id, device_type)
    try:
        virsh.attach_device(domain_opt=vm.name, file_opt=xmlfile,
                            flagstr="--config", debug=True,
                            ignore_status=False)
        vm.start()
    except (error.CmdError, virt_vm.VMStartError), detail:
        cleanup_devices(pci_id, device_type)
        raise error.TestFail("New device does not work well: %s" % detail)
开发者ID:Chenditang,项目名称:tp-libvirt,代码行数:32,代码来源:vfio.py

示例3: test_nic_fibre_group

def test_nic_fibre_group(vm, params):
    """
    Try to attach nic and fibre device at same time in iommu group to vm.

    1.Get original available interfaces&disks before attaching.
    2.Attaching hostdev in iommu group to vm.
    3.Start vm and check it.
    4.Check added interface&disk in vm.
    """
    nic_pci_id = params.get("nic_pci_id", "ETH:PCI.EXAMPLE")
    fibre_pci_id = params.get("fibre_pci_id", "FIBRE:PCI.EXAMPLE")
    if nic_pci_id.count("EXAMPLE"):
        raise error.TestNAError("Invalid Ethernet pci device id.")
    if fibre_pci_id.count("EXAMPLE"):
        raise error.TestNAError("Invalid Fibre pci device id.")
    disk_check = "yes" == params.get("fibre_pci_disk_check", "no")

    nic_ip = params.get("nic_pci_ip")
    nic_mask = params.get("nic_pci_mask", "255.255.0.0")
    nic_gateway = params.get("nic_pci_gateway")

    # Login vm to get interfaces before attaching pci device.
    if vm.is_dead():
        vm.start()
    before_pci_nics = vm.get_pci_devices("Ethernet")
    before_interfaces = vm.get_interfaces()
    before_pci_fibres = vm.get_pci_devices("Fibre")
    before_disks = vm.get_disks()
    logging.debug("Ethernet PCI devices before:%s",
                  before_pci_nics)
    logging.debug("Ethernet interfaces before:%s",
                  before_interfaces)
    logging.debug("Fibre PCI devices before:%s",
                  before_pci_fibres)
    logging.debug("Disks before:%s",
                  before_disks)
    vm.destroy()

    prepare_devices(nic_pci_id, "Ethernet")
    prepare_devices(fibre_pci_id, "Fibre")
    try:
        nicxmlfile = utlv.create_hostdev_xml(nic_pci_id)
        virsh.attach_device(domain_opt=vm.name, file_opt=nicxmlfile,
                            flagstr="--config", debug=True,
                            ignore_status=False)
        fibrexmlfile = utlv.create_hostdev_xml(fibre_pci_id)
        virsh.attach_device(domain_opt=vm.name, file_opt=fibrexmlfile,
                            flagstr="--config", debug=True,
                            ignore_status=False)
        vm.start()
    except (error.CmdError, virt_vm.VMStartError), detail:
        cleanup_devices(nic_pci_id, "Ethernet")
        cleanup_devices(fibre_pci_id, "Fibre")
        raise error.TestFail("New device does not work well: %s" % detail)
开发者ID:Chenditang,项目名称:tp-libvirt,代码行数:54,代码来源:vfio.py

示例4: hotplug_device

 def hotplug_device(type, char_dev, id=0):
     tmp_file = os.path.join(tmp_dir, char_dev)
     if type == "qmp":
         char_add_opt = "chardev-add "
         dev_add_opt = "device_add virtserialport,chardev="
         if char_dev == "file":
             char_add_opt += "file,path=%s,id=file" % tmp_file
             dev_add_opt += "file,name=file,bus=virtio-serial0.0,id=file"
         elif char_dev == "socket":
             char_add_opt += "socket,path=%s,server,nowait,id=socket" % tmp_file
             dev_add_opt += "socket,name=socket,bus=virtio-serial0.0,id=socket"
         elif char_dev == "pty":
             char_add_opt += "pty,path=/dev/pts/%s,id=pty" % id
             dev_add_opt += "pty,name=pty,bus=virtio-serial0.0,id=pty"
         result = virsh.qemu_monitor_command(vm_name, char_add_opt, "--hmp")
         if result.exit_status:
             raise error.TestError("Failed to add chardev %s to %s. Result:\n %s" % (char_dev, vm_name, result))
         result = virsh.qemu_monitor_command(vm_name, dev_add_opt, "--hmp")
         if result.exit_status:
             raise error.TestError("Failed to add device %s to %s. Result:\n %s" % (char_dev, vm_name, result))
     elif type == "attach":
         xml_file = os.path.join(tmp_dir, "xml_%s" % char_dev)
         if char_dev in ["file", "socket"]:
             prepare_channel_xml(xml_file, char_dev)
         elif char_dev == "pty":
             prepare_channel_xml(xml_file, char_dev, id)
         result = virsh.attach_device(vm_name, xml_file)
         # serial device was introduced by the following commit,
         # http://libvirt.org/git/?
         # p=libvirt.git;a=commit;h=b63ea467617e3cbee4282ab2e5e780b4119cef3d
         if "unknown device type" in result.stderr:
             raise error.TestNAError("Failed to attach %s to %s. Result:\n %s" % (char_dev, vm_name, result))
     return result
开发者ID:chloerh,项目名称:tp-libvirt,代码行数:33,代码来源:hotplug_serial.py

示例5: hotplug_device

 def hotplug_device(type, char_dev, id=0):
     tmp_file = os.path.join(tmp_dir, char_dev)
     if type == "qmp":
         char_add_opt = "chardev-add "
         dev_add_opt = "device_add virtserialport,chardev="
         if char_dev == "file":
             char_add_opt += "file,path=%s,id=file" % tmp_file
             dev_add_opt += "file,name=file,bus=virtio-serial0.0,id=file"
         elif char_dev == "socket":
             char_add_opt += "socket,path=%s,server,nowait,id=socket" % tmp_file
             dev_add_opt += "socket,name=socket,bus=virtio-serial0.0,id=socket"
         elif char_dev == "pty":
             char_add_opt += ("pty,path=/dev/pts/%s,id=pty" % id)
             dev_add_opt += "pty,name=pty,bus=virtio-serial0.0,id=pty"
         result = virsh.qemu_monitor_command(vm_name, char_add_opt, "--hmp")
         if result.exit_status:
             raise error.TestError('Failed to add chardev %s to %s. Result:\n %s'
                                   % (char_dev, vm_name, result))
         result = virsh.qemu_monitor_command(vm_name, dev_add_opt, "--hmp")
         if result.exit_status:
             raise error.TestError('Failed to add device %s to %s. Result:\n %s'
                                   % (char_dev, vm_name, result))
     elif type == "attach":
         xml_file = os.path.join(tmp_dir, "xml_%s" % char_dev)
         if char_dev in ["file", "socket"]:
             prepare_channel_xml(xml_file, char_dev)
         elif char_dev == "pty":
             prepare_channel_xml(xml_file, char_dev, id)
         result = virsh.attach_device(vm_name, xml_file)
     return result
开发者ID:Antique,项目名称:tp-libvirt,代码行数:30,代码来源:hotplug_serial.py

示例6: hotplug_device

 def hotplug_device(type, char_dev, id=0):
     tmp_file = "/tmp/%s" % char_dev
     if type == "qmp":
         char_add_opt = "chardev-add "
         dev_add_opt = "device_add virtserialport,chardev="
         if char_dev == "file":
             char_add_opt += "file,path=/tmp/file,id=file"
             dev_add_opt += "file,name=file,bus=virtio-serial0.0,id=file"
         elif char_dev == "socket":
             char_add_opt += "socket,path=/tmp/socket,server,nowait,id=socket"
             dev_add_opt += "socket,name=socket,bus=virtio-serial0.0,id=socket"
         elif char_dev == "pty":
             char_add_opt += ("pty,path=/dev/pts/%s,id=pty" % id)
             dev_add_opt += "pty,name=pty,bus=virtio-serial0.0,id=pty"
         result = virsh.qemu_monitor_command(vm_name, char_add_opt, "--hmp")
         if result.exit_status:
             raise error.TestError('Failed to add chardev %s to %s. Result:\n %s'
                                   % (char_dev, vm_name, result))
         result = virsh.qemu_monitor_command(vm_name, dev_add_opt, "--hmp")
         if result.exit_status:
             raise error.TestError('Failed to add device %s to %s. Result:\n %s'
                                   % (char_dev, vm_name, result))
     elif type == "attach":
         if char_dev in ["file", "socket"]:
             xml_info = create_channel_xml(vm_name, char_dev)
         elif char_dev == "pty":
             xml_info = create_channel_xml(vm_name, char_dev, id)
         f = open(xml_file, "w")
         f.write(xml_info)
         f.close()
         if os.path.exists(tmp_file):
             os.chmod(tmp_file, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
         result = virsh.attach_device(vm_name, xml_file)
     return result
开发者ID:zcyes,项目名称:tp-libvirt,代码行数:34,代码来源:hotplug_serial.py

示例7: attach_additional_device

def attach_additional_device(vm_name, disksize, targetdev, params):
    """
    Create a disk with disksize, then attach it to given vm.

    @param vm: Libvirt VM name.
    @param disksize: size of attached disk
    @param targetdev: target of disk device
    """
    logging.info("Attaching disk...")
    disk_path = os.path.join(data_dir.get_tmp_dir(), targetdev)
    cmd = "qemu-img create %s %s" % (disk_path, disksize)
    status, output = commands.getstatusoutput(cmd)
    if status:
        return (False, output)

    # Update params for source file
    params['source_file'] = disk_path
    params['target_dev'] = targetdev

    # Create a file of device
    xmlfile = create_disk_xml(params)

    # To confirm attached device do not exist.
    virsh.detach_disk(vm_name, targetdev, extra="--config")

    return virsh.attach_device(domain_opt=vm_name, file_opt=xmlfile,
                               flagstr="--config", debug=True)
开发者ID:CongLi,项目名称:tp-libvirt,代码行数:27,代码来源:multifunction.py

示例8: guest_config

    def guest_config(vm, ip_addr):
        """
        Add a new nic to guest and set a static ip address

        :param vm: Configured guest
        :param ip_addr: Set ip address
        """
        # Attach an interface device
        # Use attach-device, not attach-interface, because attach-interface
        # doesn't support 'direct'
        interface_class = vm_xml.VMXML.get_device_class('interface')
        interface = interface_class(type_name="direct")
        interface.source = dict(dev=str(eth_card_no), mode=str(iface_mode))
        interface.model = "virtio"
        interface.xmltreefile.write()
        if vm.is_alive():
            vm.destroy(gracefully=False)
        virsh.attach_device(vm.name, interface.xml, flagstr="--config")
        os.remove(interface.xml)
        vmxml = vm_xml.VMXML.new_from_inactive_dumpxml(vm.name)
        new_nic = vmxml.get_devices(device_type="interface")[-1]

        # Modify new interface's IP
        vm.start()
        session = vm.wait_for_login()
        eth_name = utils_net.get_linux_ifname(session, new_nic.mac_address)
        eth_config_detail_list = ['DEVICE=%s' % eth_name,
                                  'HWADDR=%s' % new_nic.mac_address,
                                  'ONBOOT=yes',
                                  'BOOTPROTO=static',
                                  'IPADDR=%s' % ip_addr]
        remote_file = remote.RemoteFile(vm.get_address(), 'scp', 'root',
                                        params.get('password'), 22,
                                        eth_config_file)
        remote_file.truncate()
        remote_file.add(eth_config_detail_list, linesep='\n')
        try:
            # Attached interface maybe already active
            session.cmd("ifdown %s" % eth_name)
        except aexpect.ShellCmdError:
            raise error.TestFail("ifdown %s failed." % eth_name)

        try:
            session.cmd("ifup %s" % eth_name)
        except aexpect.ShellCmdError:
            raise error.TestFail("ifup %s failed." % eth_name)
        return session
开发者ID:noxdafox,项目名称:tp-libvirt,代码行数:47,代码来源:macvtap.py

示例9: test_win_fibre_group

def test_win_fibre_group(test, vm, params):
    """
    Try to attach device in iommu group to vm.

    1.Get original available disks before attaching.
    2.Attaching hostdev in iommu group to vm.
    3.Start vm and check it.
    4.Check added disk in vm.
    """
    pci_id = params.get("fibre_pci_id", "FIBRE:PCI.EXAMPLE")
    device_type = "Fibre"
    if pci_id.count("EXAMPLE"):
        test.cancel("Invalid pci device id.")

    # Login vm to get disks before attaching pci device.
    if vm.is_dead():
        vm.start()
    before_disks = get_windows_disks(vm)
    logging.debug("Disks before:%s",
                  before_disks)
    vm.destroy()

    xmlfile = utlv.create_hostdev_xml(pci_id)
    prepare_devices(test, pci_id, device_type)
    try:
        virsh.attach_device(domain_opt=vm.name, file_opt=xmlfile,
                            flagstr="--config", debug=True,
                            ignore_status=False)
        vm.start()
    except (process.CmdError, virt_vm.VMStartError) as detail:
        cleanup_devices(pci_id, device_type)
        test.fail("New device does not work well: %s" % detail)

    # Get devices in vm again after attaching
    after_disks = get_windows_disks(vm)
    logging.debug("Disks after:%s",
                  after_disks)
    new_disk = "".join(list(set(before_disks) ^ set(after_disks)))
    try:
        if not new_disk:
            test.fail("Cannot find attached host device in vm.")
        # TODO: Support to configure windows partition
    finally:
        if vm.is_alive():
            vm.destroy()
        cleanup_devices(pci_id, device_type)
开发者ID:balamuruhans,项目名称:tp-libvirt,代码行数:46,代码来源:vfio.py

示例10: test_nic_single

def test_nic_single(vm, params):
    """
    Try to attach device in iommu group to vm with adding only this
    device to iommu group.

    1.Get original available interfaces before attaching.
    2.Attaching hostdev in iommu group to vm.
    3.Start vm and check it.
    4.Check added interface in vm.
    """
    pci_id = params.get("nic_pci_id", "ETH:PCI.EXAMPLE")
    if pci_id.count("EXAMPLE"):
        raise error.TestNAError("Invalid pci device id.")

    device_type = "Ethernet"
    nic_ip = params.get("nic_pci_ip")
    nic_mask = params.get("nic_pci_mask", "255.255.0.0")
    nic_gateway = params.get("nic_pci_gateway")

    # Login vm to get interfaces before attaching pci device.
    if vm.is_dead():
        vm.start()
    before_pci_nics = vm.get_pci_devices("Ethernet")
    before_interfaces = vm.get_interfaces()
    logging.debug("Ethernet PCI devices before:%s",
                  before_pci_nics)
    logging.debug("Ethernet interfaces before:%s",
                  before_interfaces)
    vm.destroy()

    xmlfile = utlv.create_hostdev_xml(pci_id)

    # Add only this device to corresponding iommu group
    prepare_devices(pci_id, device_type, only=True)
    try:
        virsh.attach_device(domain_opt=vm.name, file_opt=xmlfile,
                            flagstr="--config", debug=True,
                            ignore_status=False)
        vm.start()
        # Start successfully, but not expected.
        vm.destroy(gracefully=False)
        cleanup_devices(pci_id, device_type)
        raise error.TestFail("Start vm succesfully after attaching single "
                             "device to iommu group.Not expected.")
    except (error.CmdError, virt_vm.VMStartError), detail:
        logging.debug("Expected:New device does not work well: %s" % detail)
开发者ID:Antique,项目名称:tp-libvirt,代码行数:46,代码来源:vfio.py

示例11: attach_channel_xml

    def attach_channel_xml():
        """
        Create channel xml and attach it to guest configuration
        """
        # Check if pty channel exists already
        for elem in new_xml.devices.by_device_tag('channel'):
            if elem.type_name == channel_type_name:
                logging.debug("{0} channel already exists in guest. "
                              "No need to add new one".format(channel_type_name))
                return

        params = {'channel_type_name': channel_type_name,
                  'target_type': target_type,
                  'target_name': target_name}
        channel_xml = libvirt.create_channel_xml(params)
        virsh.attach_device(domain_opt=vm_name, file_opt=channel_xml.xml,
                            flagstr="--config", ignore_status=False)
        logging.debug("New VMXML with channel:\n%s", virsh.dumpxml(vm_name))
开发者ID:balamuruhans,项目名称:tp-libvirt,代码行数:18,代码来源:migrate_options_shared.py

示例12: test_nic_group

def test_nic_group(vm, params):
    """
    Try to attach device in iommu group to vm.

    1.Get original available interfaces before attaching.
    2.Attaching hostdev in iommu group to vm.
    3.Start vm and check it.
    4.Check added interface in vm.
    """
    pci_id = params.get("nic_pci_id", "ETH:PCI.EXAMPLE")
    if pci_id.count("EXAMPLE"):
        raise error.TestNAError("Invalid pci device id.")

    device_type = "Ethernet"
    nic_ip = params.get("nic_pci_ip")
    nic_mask = params.get("nic_pci_mask", "255.255.0.0")
    nic_gateway = params.get("nic_pci_gateway")

    # Login vm to get interfaces before attaching pci device.
    if vm.is_dead():
        vm.start()
    before_pci_nics = vm.get_pci_devices("Ethernet")
    before_interfaces = vm.get_interfaces()
    logging.debug("Ethernet PCI devices before:%s",
                  before_pci_nics)
    logging.debug("Ethernet interfaces before:%s",
                  before_interfaces)
    vm.destroy()

    boot_order = int(params.get("boot_order", 0))
    prepare_devices(pci_id, device_type)
    try:
        if boot_order:
            utlv.alter_boot_order(vm.name, pci_id, boot_order)
        else:
            xmlfile = utlv.create_hostdev_xml(pci_id)
            virsh.attach_device(domain_opt=vm.name, file_opt=xmlfile,
                                flagstr="--config", debug=True,
                                ignore_status=False)
        logging.debug("VMXML with disk boot:\n%s", virsh.dumpxml(vm.name))
        vm.start()
    except (error.CmdError, virt_vm.VMStartError), detail:
        cleanup_devices(pci_id, device_type)
        raise error.TestFail("New device does not work well: %s" % detail)
开发者ID:Chenditang,项目名称:tp-libvirt,代码行数:44,代码来源:vfio.py

示例13: test_fibre_group

def test_fibre_group(vm, params):
    """
    Try to attach device in iommu group to vm.

    1.Get original available disks before attaching.
    2.Attaching hostdev in iommu group to vm.
    3.Start vm and check it.
    4.Check added disk in vm.
    """
    pci_id = params.get("fibre_pci_id", "FIBRE:PCI.EXAMPLE")
    device_type = "Fibre"
    if pci_id.count("EXAMPLE"):
        raise error.TestNAError("Invalid pci device id.")
    disk_check = "yes" == params.get("fibre_pci_disk_check", "no")

    # Login vm to get disks before attaching pci device.
    if vm.is_dead():
        vm.start()
    before_pci_fibres = vm.get_pci_devices("Fibre")
    before_disks = vm.get_disks()
    logging.debug("Fibre PCI devices before:%s",
                  before_pci_fibres)
    logging.debug("Disks before:%s",
                  before_disks)
    vm.destroy()

    boot_order = int(params.get("boot_order", 0))
    prepare_devices(pci_id, device_type)
    try:
        if boot_order:
            utlv.alter_boot_order(vm.name, pci_id, boot_order)
        else:
            xmlfile = utlv.create_hostdev_xml(pci_id)
            virsh.attach_device(domain_opt=vm.name, file_opt=xmlfile,
                                flagstr="--config", debug=True,
                                ignore_status=False)
        logging.debug("VMXML with disk boot:\n%s", virsh.dumpxml(vm.name))
        vm.start()
    except (process.CmdError, virt_vm.VMStartError), detail:
        cleanup_devices(pci_id, device_type)
        raise error.TestFail("New device does not work well: %s" % detail)
开发者ID:bssrikanth,项目名称:tp-libvirt,代码行数:41,代码来源:vfio.py

示例14: device_hotplug

 def device_hotplug():
     if not libvirt_version.version_compare(3, 10, 0):
         detach_device(pci_devs, pci_ids)
     # attach the device in hotplug mode
     result = virsh.attach_device(vm_name, dev.xml,
                                  flagstr="--live", debug=True)
     if result.exit_status:
         test.error(result.stdout.strip())
     else:
         logging.debug(result.stdout.strip())
     if not utils_misc.wait_for(check_attach_pci, timeout):
         test.fail("timeout value is not sufficient")
开发者ID:balamuruhans,项目名称:tp-libvirt,代码行数:12,代码来源:libvirt_pci_passthrough_hotplug.py

示例15: add_device

 def add_device(dev_xml, at_error=False):
     """
     Add memory device by attachment or modify domain xml.
     """
     if attach_device:
         ret = virsh.attach_device(vm_name, dev_xml.xml, flagstr=attach_option)
         libvirt.check_exit_status(ret, at_error)
     else:
         vmxml = vm_xml.VMXML.new_from_inactive_dumpxml(vm.name)
         if numa_cells:
             del vmxml.max_mem
             del vmxml.current_mem
         vmxml.add_device(dev_xml)
         vmxml.sync()
开发者ID:uni-peter-zheng,项目名称:tp-libvirt,代码行数:14,代码来源:libvirt_mem.py


注:本文中的virttest.virsh.attach_device函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。