def _get_guest_config(self, instance, network_info, image_meta, disk_info, rescue=None, block_device_info=None, context=None):
    "Get config data for parameters.\n\n        :param rescue: optional dictionary that should contain the key\n            'ramdisk_id' if a ramdisk is needed for the rescue image and\n            'kernel_id' if a kernel is needed for the rescue image.\n        "
    flavor = objects.Flavor.get_by_id(nova_context.get_admin_context(read_deleted='yes'), instance['instance_type_id'])
    inst_path = libvirt_utils.get_instance_path(instance)
    disk_mapping = disk_info['mapping']
    img_meta_prop = (image_meta.get('properties', {}) if image_meta else {})
    CONSOLE = 'console=tty0 console=ttyS0'
    guest = vconfig.LibvirtConfigGuest()
    guest.virt_type = CONF.libvirt.virt_type
    guest.name = instance['name']
    guest.uuid = instance['uuid']
    guest.memory = (flavor.memory_mb * units.Ki)
    guest.vcpus = flavor.vcpus
    guest.cpuset = hardware.get_vcpu_pin_set()
    guest.metadata.append(self._get_guest_config_meta(context, instance, flavor))
    cputuning = ['shares', 'period', 'quota']
    for name in cputuning:
        key = ('quota:cpu_' + name)
        if (key in flavor.extra_specs):
            if (guest.cputune is None):
                guest.cputune = vconfig.LibvirtConfigGuestCPUTune()
            setattr(guest.cputune, name, int(flavor.extra_specs[key]))
    guest.cpu = self._get_guest_cpu_config(flavor, image_meta)
    if ('root' in disk_mapping):
        root_device_name = block_device.prepend_dev(disk_mapping['root']['dev'])
    else:
        root_device_name = None
    if root_device_name:
        instance.root_device_name = root_device_name
        instance.save()
    guest.os_type = vm_mode.get_from_instance(instance)
    if (guest.os_type is None):
        if (CONF.libvirt.virt_type == 'lxc'):
            guest.os_type = vm_mode.EXE
        elif (CONF.libvirt.virt_type == 'uml'):
            guest.os_type = vm_mode.UML
        elif (CONF.libvirt.virt_type == 'xen'):
            guest.os_type = vm_mode.XEN
        else:
            guest.os_type = vm_mode.HVM
    if ((CONF.libvirt.virt_type == 'xen') and (guest.os_type == vm_mode.HVM)):
        guest.os_loader = CONF.libvirt.xen_hvmloader_path
    if (CONF.libvirt.virt_type in ('kvm', 'qemu')):
        caps = self._get_host_capabilities()
        if (caps.host.cpu.arch in ('i686', 'x86_64')):
            guest.sysinfo = self._get_guest_config_sysinfo(instance)
            guest.os_smbios = vconfig.LibvirtConfigGuestSMBIOS()
        if ((image_meta is not None) and image_meta.get('properties') and (image_meta['properties'].get('hw_machine_type') is not None)):
            guest.os_mach_type = image_meta['properties']['hw_machine_type']
        else:
            if (caps.host.cpu.arch == 'armv7l'):
                guest.os_mach_type = 'vexpress-a15'
            if (caps.host.cpu.arch == 'aarch64'):
                guest.os_mach_type = 'virt'
    if (CONF.libvirt.virt_type == 'lxc'):
        guest.os_init_path = '/sbin/init'
        guest.os_cmdline = CONSOLE
    elif (CONF.libvirt.virt_type == 'uml'):
        guest.os_kernel = '/usr/bin/linux'
        guest.os_root = root_device_name
    elif rescue:
        if rescue.get('kernel_id'):
            guest.os_kernel = os.path.join(inst_path, 'kernel.rescue')
            if (CONF.libvirt.virt_type == 'xen'):
                guest.os_cmdline = ('ro root=%s' % root_device_name)
            else:
                guest.os_cmdline = ('root=%s %s' % (root_device_name, CONSOLE))
                if (CONF.libvirt.virt_type == 'qemu'):
                    guest.os_cmdline += ' no_timer_check'
        if rescue.get('ramdisk_id'):
            guest.os_initrd = os.path.join(inst_path, 'ramdisk.rescue')
    elif instance['kernel_id']:
        guest.os_kernel = os.path.join(inst_path, 'kernel')
        if (CONF.libvirt.virt_type == 'xen'):
            guest.os_cmdline = ('ro root=%s' % root_device_name)
        else:
            guest.os_cmdline = ('root=%s %s' % (root_device_name, CONSOLE))
            if (CONF.libvirt.virt_type == 'qemu'):
                guest.os_cmdline += ' no_timer_check'
        if instance['ramdisk_id']:
            guest.os_initrd = os.path.join(inst_path, 'ramdisk')
        if image_meta:
            img_props = image_meta.get('properties', {})
            if img_props.get('os_command_line'):
                guest.os_cmdline = img_props.get('os_command_line')
    else:
        guest.os_boot_dev = blockinfo.get_boot_order(disk_info)
    if ((CONF.libvirt.virt_type != 'lxc') and (CONF.libvirt.virt_type != 'uml')):
        guest.acpi = True
        guest.apic = True
    clk = vconfig.LibvirtConfigGuestClock()
    if (instance['os_type'] == 'windows'):
        LOG.info(_LI('Configuring timezone for windows instance to localtime'), instance=instance)
        clk.offset = 'localtime'
    else:
        clk.offset = 'utc'
    guest.set_clock(clk)
    if (CONF.libvirt.virt_type == 'kvm'):
        tmpit = vconfig.LibvirtConfigGuestTimer()
        tmpit.name = 'pit'
        tmpit.tickpolicy = 'delay'
        tmrtc = vconfig.LibvirtConfigGuestTimer()
        tmrtc.name = 'rtc'
        tmrtc.tickpolicy = 'catchup'
        clk.add_timer(tmpit)
        clk.add_timer(tmrtc)
        arch = libvirt_utils.get_arch(image_meta)
        if (arch in ('i686', 'x86_64')):
            tmhpet = vconfig.LibvirtConfigGuestTimer()
            tmhpet.name = 'hpet'
            tmhpet.present = False
            clk.add_timer(tmhpet)
    for config in self._get_guest_storage_config(instance, image_meta, disk_info, rescue, block_device_info, flavor):
        guest.add_device(config)
    for vif in network_info:
        config = self.vif_driver.get_config(instance, vif, image_meta, flavor, CONF.libvirt.virt_type)
        guest.add_device(config)
    if ((CONF.libvirt.virt_type == 'qemu') or (CONF.libvirt.virt_type == 'kvm')):
        consolelog = vconfig.LibvirtConfigGuestSerial()
        consolelog.type = 'file'
        consolelog.source_path = self._get_console_log_path(instance)
        guest.add_device(consolelog)
        consolepty = vconfig.LibvirtConfigGuestSerial()
    else:
        consolepty = vconfig.LibvirtConfigGuestConsole()
    consolepty.type = 'pty'
    guest.add_device(consolepty)
    need_usb_tablet = False
    if CONF.vnc_enabled:
        need_usb_tablet = CONF.libvirt.use_usb_tablet
    elif (CONF.spice.enabled and (not CONF.spice.agent_enabled)):
        need_usb_tablet = CONF.libvirt.use_usb_tablet
    if (need_usb_tablet and (guest.os_type == vm_mode.HVM)):
        tablet = vconfig.LibvirtConfigGuestInput()
        tablet.type = 'tablet'
        tablet.bus = 'usb'
        guest.add_device(tablet)
    if (CONF.spice.enabled and CONF.spice.agent_enabled and (CONF.libvirt.virt_type not in ('lxc', 'uml', 'xen'))):
        channel = vconfig.LibvirtConfigGuestChannel()
        channel.target_name = 'com.redhat.spice.0'
        guest.add_device(channel)
    add_video_driver = False
    if (CONF.vnc_enabled and (CONF.libvirt.virt_type not in ('lxc', 'uml'))):
        graphics = vconfig.LibvirtConfigGuestGraphics()
        graphics.type = 'vnc'
        graphics.keymap = CONF.vnc_keymap
        graphics.listen = CONF.vncserver_listen
        guest.add_device(graphics)
        add_video_driver = True
    if (CONF.spice.enabled and (CONF.libvirt.virt_type not in ('lxc', 'uml', 'xen'))):
        graphics = vconfig.LibvirtConfigGuestGraphics()
        graphics.type = 'spice'
        graphics.keymap = CONF.spice.keymap
        graphics.listen = CONF.spice.server_listen
        guest.add_device(graphics)
        add_video_driver = True
    if add_video_driver:
        VALID_VIDEO_DEVICES = ('vga', 'cirrus', 'vmvga', 'xen', 'qxl')
        video = vconfig.LibvirtConfigGuestVideo()
        arch = libvirt_utils.get_arch(image_meta)
        if (guest.os_type == vm_mode.XEN):
            video.type = 'xen'
        elif (arch in ('ppc', 'ppc64')):
            video.type = 'vga'
        elif CONF.spice.enabled:
            video.type = 'qxl'
        if img_meta_prop.get('hw_video_model'):
            video.type = img_meta_prop.get('hw_video_model')
            if (video.type not in VALID_VIDEO_DEVICES):
                raise exception.InvalidVideoMode(model=video.type)
        video_ram = int(img_meta_prop.get('hw_video_ram', 0))
        max_vram = int(flavor.extra_specs.get('hw_video:ram_max_mb', 0))
        if (video_ram > max_vram):
            raise exception.RequestedVRamTooHigh(req_vram=video_ram, max_vram=max_vram)
        if (max_vram and video_ram):
            video.vram = video_ram
        guest.add_device(video)
    if (CONF.libvirt.virt_type in ('qemu', 'kvm')):
        qga_enabled = False
        hw_qga = img_meta_prop.get('hw_qemu_guest_agent', 'no')
        if (hw_qga.lower() == 'yes'):
            LOG.debug('Qemu guest agent is enabled through image metadata', instance=instance)
            qga_enabled = True
        if qga_enabled:
            qga = vconfig.LibvirtConfigGuestChannel()
            qga.type = 'unix'
            qga.target_name = 'org.qemu.guest_agent.0'
            qga.source_path = ('/var/lib/libvirt/qemu/%s.%s.sock' % ('org.qemu.guest_agent.0', instance['name']))
            guest.add_device(qga)
        if ((img_meta_prop.get('hw_rng_model') == 'virtio') and (flavor.extra_specs.get('hw_rng:allowed', '').lower() == 'true')):
            rng_device = vconfig.LibvirtConfigGuestRng()
            rate_bytes = flavor.extra_specs.get('hw_rng:rate_bytes', 0)
            period = flavor.extra_specs.get('hw_rng:rate_period', 0)
            if rate_bytes:
                rng_device.rate_bytes = int(rate_bytes)
                rng_device.rate_period = int(period)
            if (CONF.libvirt.rng_dev_path and (not os.path.exists(CONF.libvirt.rng_dev_path))):
                raise exception.RngDeviceNotExist(path=CONF.libvirt.rng_dev_path)
            rng_device.backend = CONF.libvirt.rng_dev_path
            guest.add_device(rng_device)
    if (CONF.libvirt.virt_type in ('xen', 'qemu', 'kvm')):
        for pci_dev in pci_manager.get_instance_pci_devs(instance):
            guest.add_device(self._get_guest_pci_device(pci_dev))
    elif (len(pci_manager.get_instance_pci_devs(instance)) > 0):
        raise exception.PciDeviceUnsupportedHypervisor(type=CONF.libvirt.virt_type)
    watchdog_action = flavor.extra_specs.get('hw_watchdog_action', 'disabled')
    if ((image_meta is not None) and image_meta.get('properties', {}).get('hw_watchdog_action')):
        watchdog_action = image_meta['properties']['hw_watchdog_action']
    if (watchdog_action != 'disabled'):
        if watchdog_actions.is_valid_watchdog_action(watchdog_action):
            bark = vconfig.LibvirtConfigGuestWatchdog()
            bark.action = watchdog_action
            guest.add_device(bark)
        else:
            raise exception.InvalidWatchdogAction(action=watchdog_action)
    return guest
Get config data for parameters.

:param rescue: optional dictionary that should contain the key
    'ramdisk_id' if a ramdisk is needed for the rescue image and
    'kernel_id' if a kernel is needed for the rescue image.
def _get_guest_config(self, instance, network_info, image_meta, disk_info, rescue=None, block_device_info=None, context=None):
    "Get config data for parameters.\n\n        :param rescue: optional dictionary that should contain the key\n            'ramdisk_id' if a ramdisk is needed for the rescue image and\n            'kernel_id' if a kernel is needed for the rescue image.\n        "
    flavor = objects.Flavor.get_by_id(nova_context.get_admin_context(read_deleted='yes'), instance['instance_type_id'])
    inst_path = libvirt_utils.get_instance_path(instance)
    disk_mapping = disk_info['mapping']
    img_meta_prop = (image_meta.get('properties', {}) if image_meta else {})
    CONSOLE = 'console=tty0 console=ttyS0'
    guest = vconfig.LibvirtConfigGuest()
    guest.virt_type = CONF.libvirt.virt_type
    guest.name = instance['name']
    guest.uuid = instance['uuid']
    guest.memory = (flavor.memory_mb * units.Ki)
    guest.vcpus = flavor.vcpus
    guest.cpuset = hardware.get_vcpu_pin_set()
    guest.metadata.append(self._get_guest_config_meta(context, instance, flavor))
    cputuning = ['shares', 'period', 'quota']
    for name in cputuning:
        key = ('quota:cpu_' + name)
        if (key in flavor.extra_specs):
            if (guest.cputune is None):
                guest.cputune = vconfig.LibvirtConfigGuestCPUTune()
            setattr(guest.cputune, name, int(flavor.extra_specs[key]))
    guest.cpu = self._get_guest_cpu_config(flavor, image_meta)
    if ('root' in disk_mapping):
        root_device_name = block_device.prepend_dev(disk_mapping['root']['dev'])
    else:
        root_device_name = None
    if root_device_name:
        instance.root_device_name = root_device_name
        instance.save()
    guest.os_type = vm_mode.get_from_instance(instance)
    if (guest.os_type is None):
        if (CONF.libvirt.virt_type == 'lxc'):
            guest.os_type = vm_mode.EXE
        elif (CONF.libvirt.virt_type == 'uml'):
            guest.os_type = vm_mode.UML
        elif (CONF.libvirt.virt_type == 'xen'):
            guest.os_type = vm_mode.XEN
        else:
            guest.os_type = vm_mode.HVM
    if ((CONF.libvirt.virt_type == 'xen') and (guest.os_type == vm_mode.HVM)):
        guest.os_loader = CONF.libvirt.xen_hvmloader_path
    if (CONF.libvirt.virt_type in ('kvm', 'qemu')):
        caps = self._get_host_capabilities()
        if (caps.host.cpu.arch in ('i686', 'x86_64')):
            guest.sysinfo = self._get_guest_config_sysinfo(instance)
            guest.os_smbios = vconfig.LibvirtConfigGuestSMBIOS()
        if ((image_meta is not None) and image_meta.get('properties') and (image_meta['properties'].get('hw_machine_type') is not None)):
            guest.os_mach_type = image_meta['properties']['hw_machine_type']
        else:
            if (caps.host.cpu.arch == 'armv7l'):
                guest.os_mach_type = 'vexpress-a15'
            if (caps.host.cpu.arch == 'aarch64'):
                guest.os_mach_type = 'virt'
    if (CONF.libvirt.virt_type == 'lxc'):
        guest.os_init_path = '/sbin/init'
        guest.os_cmdline = CONSOLE
    elif (CONF.libvirt.virt_type == 'uml'):
        guest.os_kernel = '/usr/bin/linux'
        guest.os_root = root_device_name
    elif rescue:
        if rescue.get('kernel_id'):
            guest.os_kernel = os.path.join(inst_path, 'kernel.rescue')
            if (CONF.libvirt.virt_type == 'xen'):
                guest.os_cmdline = ('ro root=%s' % root_device_name)
            else:
                guest.os_cmdline = ('root=%s %s' % (root_device_name, CONSOLE))
                if (CONF.libvirt.virt_type == 'qemu'):
                    guest.os_cmdline += ' no_timer_check'
        if rescue.get('ramdisk_id'):
            guest.os_initrd = os.path.join(inst_path, 'ramdisk.rescue')
    elif instance['kernel_id']:
        guest.os_kernel = os.path.join(inst_path, 'kernel')
        if (CONF.libvirt.virt_type == 'xen'):
            guest.os_cmdline = ('ro root=%s' % root_device_name)
        else:
            guest.os_cmdline = ('root=%s %s' % (root_device_name, CONSOLE))
            if (CONF.libvirt.virt_type == 'qemu'):
                guest.os_cmdline += ' no_timer_check'
        if instance['ramdisk_id']:
            guest.os_initrd = os.path.join(inst_path, 'ramdisk')
        if image_meta:
            img_props = image_meta.get('properties', {})
            if img_props.get('os_command_line'):
                guest.os_cmdline = img_props.get('os_command_line')
    else:
        guest.os_boot_dev = blockinfo.get_boot_order(disk_info)
    if ((CONF.libvirt.virt_type != 'lxc') and (CONF.libvirt.virt_type != 'uml')):
        guest.acpi = True
        guest.apic = True
    clk = vconfig.LibvirtConfigGuestClock()
    if (instance['os_type'] == 'windows'):
        LOG.info(_LI('Configuring timezone for windows instance to localtime'), instance=instance)
        clk.offset = 'localtime'
    else:
        clk.offset = 'utc'
    guest.set_clock(clk)
    if (CONF.libvirt.virt_type == 'kvm'):
        tmpit = vconfig.LibvirtConfigGuestTimer()
        tmpit.name = 'pit'
        tmpit.tickpolicy = 'delay'
        tmrtc = vconfig.LibvirtConfigGuestTimer()
        tmrtc.name = 'rtc'
        tmrtc.tickpolicy = 'catchup'
        clk.add_timer(tmpit)
        clk.add_timer(tmrtc)
        arch = libvirt_utils.get_arch(image_meta)
        if (arch in ('i686', 'x86_64')):
            tmhpet = vconfig.LibvirtConfigGuestTimer()
            tmhpet.name = 'hpet'
            tmhpet.present = False
            clk.add_timer(tmhpet)
    for config in self._get_guest_storage_config(instance, image_meta, disk_info, rescue, block_device_info, flavor):
        guest.add_device(config)
    for vif in network_info:
        config = self.vif_driver.get_config(instance, vif, image_meta, flavor, CONF.libvirt.virt_type)
        guest.add_device(config)
    if ((CONF.libvirt.virt_type == 'qemu') or (CONF.libvirt.virt_type == 'kvm')):
        consolelog = vconfig.LibvirtConfigGuestSerial()
        consolelog.type = 'file'
        consolelog.source_path = self._get_console_log_path(instance)
        guest.add_device(consolelog)
        consolepty = vconfig.LibvirtConfigGuestSerial()
    else:
        consolepty = vconfig.LibvirtConfigGuestConsole()
    consolepty.type = 'pty'
    guest.add_device(consolepty)
    need_usb_tablet = False
    if CONF.vnc_enabled:
        need_usb_tablet = CONF.libvirt.use_usb_tablet
    elif (CONF.spice.enabled and (not CONF.spice.agent_enabled)):
        need_usb_tablet = CONF.libvirt.use_usb_tablet
    if (need_usb_tablet and (guest.os_type == vm_mode.HVM)):
        tablet = vconfig.LibvirtConfigGuestInput()
        tablet.type = 'tablet'
        tablet.bus = 'usb'
        guest.add_device(tablet)
    if (CONF.spice.enabled and CONF.spice.agent_enabled and (CONF.libvirt.virt_type not in ('lxc', 'uml', 'xen'))):
        channel = vconfig.LibvirtConfigGuestChannel()
        channel.target_name = 'com.redhat.spice.0'
        guest.add_device(channel)
    add_video_driver = False
    if (CONF.vnc_enabled and (CONF.libvirt.virt_type not in ('lxc', 'uml'))):
        graphics = vconfig.LibvirtConfigGuestGraphics()
        graphics.type = 'vnc'
        graphics.keymap = CONF.vnc_keymap
        graphics.listen = CONF.vncserver_listen
        guest.add_device(graphics)
        add_video_driver = True
    if (CONF.spice.enabled and (CONF.libvirt.virt_type not in ('lxc', 'uml', 'xen'))):
        graphics = vconfig.LibvirtConfigGuestGraphics()
        graphics.type = 'spice'
        graphics.keymap = CONF.spice.keymap
        graphics.listen = CONF.spice.server_listen
        guest.add_device(graphics)
        add_video_driver = True
    if add_video_driver:
        VALID_VIDEO_DEVICES = ('vga', 'cirrus', 'vmvga', 'xen', 'qxl')
        video = vconfig.LibvirtConfigGuestVideo()
        arch = libvirt_utils.get_arch(image_meta)
        if (guest.os_type == vm_mode.XEN):
            video.type = 'xen'
        elif (arch in ('ppc', 'ppc64')):
            video.type = 'vga'
        elif CONF.spice.enabled:
            video.type = 'qxl'
        if img_meta_prop.get('hw_video_model'):
            video.type = img_meta_prop.get('hw_video_model')
            if (video.type not in VALID_VIDEO_DEVICES):
                raise exception.InvalidVideoMode(model=video.type)
        video_ram = int(img_meta_prop.get('hw_video_ram', 0))
        max_vram = int(flavor.extra_specs.get('hw_video:ram_max_mb', 0))
        if (video_ram > max_vram):
            raise exception.RequestedVRamTooHigh(req_vram=video_ram, max_vram=max_vram)
        if (max_vram and video_ram):
            video.vram = video_ram
        guest.add_device(video)
    if (CONF.libvirt.virt_type in ('qemu', 'kvm')):
        qga_enabled = False
        hw_qga = img_meta_prop.get('hw_qemu_guest_agent', 'no')
        if (hw_qga.lower() == 'yes'):
            LOG.debug('Qemu guest agent is enabled through image metadata', instance=instance)
            qga_enabled = True
        if qga_enabled:
            qga = vconfig.LibvirtConfigGuestChannel()
            qga.type = 'unix'
            qga.target_name = 'org.qemu.guest_agent.0'
            qga.source_path = ('/var/lib/libvirt/qemu/%s.%s.sock' % ('org.qemu.guest_agent.0', instance['name']))
            guest.add_device(qga)
        if ((img_meta_prop.get('hw_rng_model') == 'virtio') and (flavor.extra_specs.get('hw_rng:allowed', ).lower() == 'true')):
            rng_device = vconfig.LibvirtConfigGuestRng()
            rate_bytes = flavor.extra_specs.get('hw_rng:rate_bytes', 0)
            period = flavor.extra_specs.get('hw_rng:rate_period', 0)
            if rate_bytes:
                rng_device.rate_bytes = int(rate_bytes)
                rng_device.rate_period = int(period)
            if (CONF.libvirt.rng_dev_path and (not os.path.exists(CONF.libvirt.rng_dev_path))):
                raise exception.RngDeviceNotExist(path=CONF.libvirt.rng_dev_path)
            rng_device.backend = CONF.libvirt.rng_dev_path
            guest.add_device(rng_device)
    if (CONF.libvirt.virt_type in ('xen', 'qemu', 'kvm')):
        for pci_dev in pci_manager.get_instance_pci_devs(instance):
            guest.add_device(self._get_guest_pci_device(pci_dev))
    elif (len(pci_manager.get_instance_pci_devs(instance)) > 0):
        raise exception.PciDeviceUnsupportedHypervisor(type=CONF.libvirt.virt_type)
    watchdog_action = flavor.extra_specs.get('hw_watchdog_action', 'disabled')
    if ((image_meta is not None) and image_meta.get('properties', {}).get('hw_watchdog_action')):
        watchdog_action = image_meta['properties']['hw_watchdog_action']
    if (watchdog_action != 'disabled'):
        if watchdog_actions.is_valid_watchdog_action(watchdog_action):
            bark = vconfig.LibvirtConfigGuestWatchdog()
            bark.action = watchdog_action
            guest.add_device(bark)
        else:
            raise exception.InvalidWatchdogAction(action=watchdog_action)
    return guest