示例#1
0
    def to_vm_xml(self, vm_name, vm_uuid, **kwargs):
        params = dict(self.info)
        params['name'] = vm_name
        params['uuid'] = vm_uuid
        params['networks'] = self._get_networks_xml()
        params['input_output'] = self._get_input_output_xml()
        params['qemu-namespace'] = ''
        params['cdroms'] = ''
        params['qemu-stream-cmdline'] = ''
        params['disks'] = self._get_disks_xml(vm_uuid)
        params['serial'] = get_serial_xml(params)

        graphics = dict(self.info['graphics'])
        graphics.update(kwargs.get('graphics', {}))
        params['graphics'] = get_graphics_xml(graphics)

        libvirt_stream_protocols = kwargs.get('libvirt_stream_protocols', [])
        cdrom_xml = self._get_cdrom_xml(libvirt_stream_protocols)

        # Add information of CD-ROM device only if template have info about it.
        if cdrom_xml is not None:
            if not urlparse.urlparse(self.info.get('cdrom', "")).scheme in \
                    libvirt_stream_protocols and \
                    params.get('iso_stream', False):
                params['qemu-stream-cmdline'] = cdrom_xml
            else:
                params['cdroms'] = cdrom_xml

        # Set the boot order of VM
        # TODO: need modify this when boot order edition feature came upstream.
        params['boot_order'] = get_bootorder_xml()

        # Setting maximum number of memory slots
        slots = str(self.info['mem_dev_slots'])

        # Rearrange memory parameters
        memory = self.info['memory'].get('current')
        maxmemory = self.info['memory'].get('maxmemory')
        if maxmemory < memory:
            raise OperationFailed("KCHVM0041E",
                                  {'maxmem': str(maxmemory)})

        params['memory'] = self.info['memory'].get('current')
        params['max_memory'] = ""
        # if there is not support to memory hotplug in Libvirt or qemu, we
        # cannot add the tag maxMemory
        if memory != maxmemory and kwargs.get('mem_hotplug_support', True):
            maxmem_xml = "<maxMemory slots='%s' unit='MiB'>%s</maxMemory>"
            params['max_memory'] = maxmem_xml % (slots, maxmemory)

        # set a hard limit using max_memory + 1GiB
        params['hard_limit'] = maxmemory + 1024

        # vcpu element
        cpus = params['cpu_info']['vcpus']
        maxvcpus = params['cpu_info']['maxvcpus']
        params['vcpus_xml'] = "<vcpu current='%d'>%d</vcpu>" % (cpus, maxvcpus)

        # cpu_info element
        params['cpu_info_xml'] = self._get_cpu_xml()

        xml = """
        <domain type='%(domain)s'>
          %(qemu-stream-cmdline)s
          <name>%(name)s</name>
          <uuid>%(uuid)s</uuid>
          <memtune>
            <hard_limit unit='MiB'>%(hard_limit)s</hard_limit>
          </memtune>
          %(max_memory)s
          <memory unit='MiB'>%(memory)s</memory>
          %(vcpus_xml)s
          %(cpu_info_xml)s
          <os>
            <type arch='%(arch)s'>hvm</type>
            %(boot_order)s
          </os>
          <features>
            <acpi/>
            <apic/>
            <pae/>
          </features>
          <clock offset='utc'/>
          <on_poweroff>destroy</on_poweroff>
          <on_reboot>restart</on_reboot>
          <on_crash>restart</on_crash>
          <devices>
            %(disks)s
            %(cdroms)s
            %(networks)s
            %(graphics)s
            %(input_output)s
            %(serial)s
            <memballoon model='virtio' />
          </devices>
        </domain>
        """ % params

        return xml
示例#2
0
    def to_vm_xml(self, vm_name, vm_uuid, **kwargs):
        params = dict(self.info)
        params['name'] = vm_name
        params['uuid'] = vm_uuid
        params['networks'] = self._get_networks_xml()
        params['input_output'] = self._get_input_output_xml()
        params['qemu-namespace'] = ''
        params['cdroms'] = ''
        params['qemu-stream-cmdline'] = ''
        params['disks'] = self._get_disks_xml(vm_uuid)
        params['serial'] = get_serial_xml(params)

        graphics = dict(self.info['graphics'])
        graphics.update(kwargs.get('graphics', {}))
        params['graphics'] = get_graphics_xml(graphics)

        libvirt_stream_protocols = kwargs.get('libvirt_stream_protocols', [])
        cdrom_xml = self._get_cdrom_xml(libvirt_stream_protocols)

        if not urlparse.urlparse(self.info.get('cdrom', "")).scheme in \
                libvirt_stream_protocols and \
                params.get('iso_stream', False):
            params['qemu-stream-cmdline'] = cdrom_xml
        else:
            params['cdroms'] = cdrom_xml

        # Setting maximum number of slots to avoid errors when hotplug memory
        # Number of slots are the numbers of chunks of 1GB that fit inside
        # the max_memory of the host minus memory assigned to the VM. It
        # cannot have more than 32 slots in Power.
        memory = self.info['memory'].get('current')
        maxmemory = self.info['memory'].get('maxmemory')

        slots = (maxmemory - memory) >> 10
        if slots < 0:
            raise OperationFailed("KCHVM0041E", {'maxmem': str(maxmemory)})
        elif slots == 0:
            slots = 1
        elif slots > 32:
            distro, _, _ = platform.linux_distribution()
            if distro == "IBM_PowerKVM":
                slots = 32

        # Rearrange memory parameters
        params['memory'] = self.info['memory'].get('current')
        params['max_memory'] = ""
        # if there is not support to memory hotplug in Libvirt or qemu, we
        # cannot add the tag maxMemory
        if memory != maxmemory and kwargs.get('mem_hotplug_support', True):
            maxmem_xml = "<maxMemory slots='%s' unit='MiB'>%s</maxMemory>"
            params['max_memory'] = maxmem_xml % (slots, maxmemory)

        # set a hard limit using max_memory + 1GiB
        params['hard_limit'] = maxmemory + 1024

        # vcpu element
        cpus = params['cpu_info']['vcpus']
        maxvcpus = params['cpu_info']['maxvcpus']
        params['vcpus_xml'] = "<vcpu current='%d'>%d</vcpu>" % (cpus, maxvcpus)

        # cpu_info element
        params['cpu_info_xml'] = self._get_cpu_xml()

        xml = """
        <domain type='%(domain)s'>
          %(qemu-stream-cmdline)s
          <name>%(name)s</name>
          <uuid>%(uuid)s</uuid>
          <memtune>
            <hard_limit unit='MiB'>%(hard_limit)s</hard_limit>
          </memtune>
          %(max_memory)s
          <memory unit='MiB'>%(memory)s</memory>
          %(vcpus_xml)s
          %(cpu_info_xml)s
          <os>
            <type arch='%(arch)s'>hvm</type>
            <boot dev='hd'/>
            <boot dev='cdrom'/>
          </os>
          <features>
            <acpi/>
            <apic/>
            <pae/>
          </features>
          <clock offset='utc'/>
          <on_poweroff>destroy</on_poweroff>
          <on_reboot>restart</on_reboot>
          <on_crash>restart</on_crash>
          <devices>
            %(disks)s
            %(cdroms)s
            %(networks)s
            %(graphics)s
            %(input_output)s
            %(serial)s
            <memballoon model='virtio' />
          </devices>
        </domain>
        """ % params

        return xml
示例#3
0
    def to_vm_xml(self, vm_name, vm_uuid, **kwargs):
        params = dict(self.info)
        params['name'] = vm_name
        params['uuid'] = vm_uuid
        params['networks'] = self._get_networks_xml()
        params['interfaces'] = self._get_interfaces_xml()
        params['input_output'] = self._get_input_output_xml()
        params['qemu-namespace'] = ''
        params['cdroms'] = ''
        params['qemu-stream-cmdline'] = ''
        params['disks'] = self._get_disks_xml(vm_uuid)
        params['serial'] = get_serial_xml(params)
        params['title'] = kwargs.get('title', '')
        params['description'] = kwargs.get('description', '')
        graphics = dict(self.info['graphics'])
        graphics.update(kwargs.get('graphics', {}))
        # Graphics is not supported on s390x, this check will
        # not add graphics tag in domain xml.
        if params.get('arch') != 's390x':
            params['graphics'] = get_graphics_xml(graphics)

        libvirt_stream_protocols = kwargs.get('libvirt_stream_protocols', [])
        cdrom_xml = self._get_cdrom_xml(libvirt_stream_protocols)

        # Add information of CD-ROM device only if template have info about it.
        if cdrom_xml is not None:
            if not urlparse.urlparse(self.info.get('cdrom', "")).scheme in \
                    libvirt_stream_protocols and \
                    params.get('iso_stream', False):
                params['qemu-stream-cmdline'] = cdrom_xml
            else:
                params['cdroms'] = cdrom_xml

        # Set the boot order of VM
        # TODO: need modify this when boot order edition feature came upstream.
        if cdrom_xml and params.get('arch') == 's390x':
            params['boot_order'] = get_bootorder_xml(
                ['cdrom', 'hd', 'network'])
        else:
            params['boot_order'] = get_bootorder_xml()

        # Setting maximum number of memory slots
        slots = str(self.info['mem_dev_slots'])

        # Rearrange memory parameters
        memory = self.info['memory'].get('current')
        maxmemory = self.info['memory'].get('maxmemory')
        if maxmemory < memory:
            raise OperationFailed("KCHVM0041E", {'maxmem': str(maxmemory)})

        params['memory'] = self.info['memory'].get('current')
        params['max_memory'] = ""
        # if there is not support to memory hotplug in Libvirt or qemu, we
        # cannot add the tag maxMemory
        if memory != maxmemory and kwargs.get('mem_hotplug_support', True):
            maxmem_xml = "<maxMemory slots='%s' unit='MiB'>%s</maxMemory>"
            params['max_memory'] = maxmem_xml % (slots, maxmemory)

        # set a hard limit using max_memory + 1GiB
        params['hard_limit'] = maxmemory + 1024

        # vcpu element
        cpus = params['cpu_info']['vcpus']
        maxvcpus = params['cpu_info']['maxvcpus']
        params['vcpus_xml'] = "<vcpu current='%d'>%d</vcpu>" % (cpus, maxvcpus)

        # cpu_info element
        params['cpu_info_xml'] = self._get_cpu_xml()

        # usb controller
        params['usb_controller'] = self._get_usb_controller()

        xml = """
        <domain type='%(domain)s'>
          %(qemu-stream-cmdline)s
          <name>%(name)s</name>
          <title>%(title)s</title>
          <description>%(description)s</description>
          <uuid>%(uuid)s</uuid>
          <memtune>
            <hard_limit unit='MiB'>%(hard_limit)s</hard_limit>
          </memtune>
          %(max_memory)s
          <memory unit='MiB'>%(memory)s</memory>
          %(vcpus_xml)s
          %(cpu_info_xml)s
          <os>
            <type arch='%(arch)s'>hvm</type>
            %(boot_order)s
          </os>
          <features>
            <acpi/>
            <apic/>
            <pae/>
          </features>
          <clock offset='utc'/>
          <on_poweroff>destroy</on_poweroff>
          <on_reboot>restart</on_reboot>
          <on_crash>restart</on_crash>
          <devices>
            %(disks)s
            %(cdroms)s
            %(networks)s
            %(interfaces)s
            %(graphics)s
            %(input_output)s
            %(usb_controller)s
            %(serial)s
            <memballoon model='virtio' />
          </devices>
        </domain>
        """ % params

        return xml
示例#4
0
    def to_vm_xml(self, vm_name, vm_uuid, **kwargs):
        params = dict(self.info)
        params['name'] = vm_name
        params['uuid'] = vm_uuid
        params['networks'] = self._get_networks_xml()
        params['input_output'] = self._get_input_output_xml()
        params['qemu-namespace'] = ''
        params['cdroms'] = ''
        params['qemu-stream-cmdline'] = ''
        params['cpu_info'] = self._get_cpu_xml()
        params['disks'] = self._get_disks_xml(vm_uuid)

        graphics = dict(self.info['graphics'])
        graphics.update(kwargs.get('graphics', {}))
        params['graphics'] = get_graphics_xml(graphics)

        libvirt_stream_protocols = kwargs.get('libvirt_stream_protocols', [])
        cdrom_xml = self._get_cdrom_xml(libvirt_stream_protocols)

        if not urlparse.urlparse(self.info.get('cdrom', "")).scheme in \
                libvirt_stream_protocols and \
                params.get('iso_stream', False):
            params['qemu-stream-cmdline'] = cdrom_xml
        else:
            params['cdroms'] = cdrom_xml

        # Setting maximum number of slots to avoid errors when hotplug memory
        # Number of slots are the numbers of chunks of 1GB that fit inside
        # the max_memory of the host minus memory assigned to the VM
        params['slots'] = (
            (params['max_memory'] >> 10) - params['memory']) >> 10
        if params['slots'] < 0:
            raise OperationFailed("KCHVM0041E")
        elif params['slots'] == 0:
            params['slots'] = 1

        xml = """
        <domain type='%(domain)s'>
          %(qemu-stream-cmdline)s
          <name>%(name)s</name>
          <uuid>%(uuid)s</uuid>
          <maxMemory slots='%(slots)s' unit='KiB'>%(max_memory)s</maxMemory>
          <memory unit='MiB'>%(memory)s</memory>
          <vcpu>%(cpus)s</vcpu>
          %(cpu_info)s
          <os>
            <type arch='%(arch)s'>hvm</type>
            <boot dev='hd'/>
            <boot dev='cdrom'/>
          </os>
          <features>
            <acpi/>
            <apic/>
            <pae/>
          </features>
          <clock offset='utc'/>
          <on_poweroff>destroy</on_poweroff>
          <on_reboot>restart</on_reboot>
          <on_crash>restart</on_crash>
          <devices>
            %(disks)s
            %(cdroms)s
            %(networks)s
            %(graphics)s
            %(input_output)s
            <memballoon model='virtio' />
          </devices>
        </domain>
        """ % params

        # Adding PPC console configuration
        if params['arch'] in ['ppc', 'ppc64']:
            ppc_console = """<memballoon model='virtio' />
            <console type='pty'>
              <target type='serial' port='1'/>
              <address type='spapr-vio' reg='0x30001000'/>
            </console>"""
            xml = xml.replace("<memballoon model='virtio' />", ppc_console)

        return xml
示例#5
0
    def to_vm_xml(self, vm_name, vm_uuid, **kwargs):
        params = dict(self.info)
        params['name'] = vm_name
        params['uuid'] = vm_uuid
        params['networks'] = self._get_networks_xml()
        params['input_output'] = self._get_input_output_xml()
        params['qemu-namespace'] = ''
        params['cdroms'] = ''
        params['qemu-stream-cmdline'] = ''
        params['cpu_info'] = self._get_cpu_xml()
        params['disks'] = self._get_disks_xml(vm_uuid)
        params['serial'] = get_serial_xml(params)

        graphics = dict(self.info['graphics'])
        graphics.update(kwargs.get('graphics', {}))
        params['graphics'] = get_graphics_xml(graphics)

        libvirt_stream_protocols = kwargs.get('libvirt_stream_protocols', [])
        cdrom_xml = self._get_cdrom_xml(libvirt_stream_protocols)

        if not urlparse.urlparse(self.info.get('cdrom', "")).scheme in \
                libvirt_stream_protocols and \
                params.get('iso_stream', False):
            params['qemu-stream-cmdline'] = cdrom_xml
        else:
            params['cdroms'] = cdrom_xml

        # Setting maximum number of slots to avoid errors when hotplug memory
        # Number of slots are the numbers of chunks of 1GB that fit inside
        # the max_memory of the host minus memory assigned to the VM. It
        # cannot have more than 32 slots in Power.
        params['slots'] = (
            (params['max_memory'] >> 10) - params['memory']) >> 10
        if params['slots'] < 0:
            raise OperationFailed("KCHVM0041E")
        elif params['slots'] == 0:
            params['slots'] = 1
        elif params['slots'] > 32:
            distro, _, _ = platform.linux_distribution()
            if distro == "IBM_PowerKVM":
                params['slots'] = 32

        xml = """
        <domain type='%(domain)s'>
          %(qemu-stream-cmdline)s
          <name>%(name)s</name>
          <uuid>%(uuid)s</uuid>
          <maxMemory slots='%(slots)s' unit='KiB'>%(max_memory)s</maxMemory>
          <memory unit='MiB'>%(memory)s</memory>
          <vcpu>%(cpus)s</vcpu>
          %(cpu_info)s
          <os>
            <type arch='%(arch)s'>hvm</type>
            <boot dev='hd'/>
            <boot dev='cdrom'/>
          </os>
          <features>
            <acpi/>
            <apic/>
            <pae/>
          </features>
          <clock offset='utc'/>
          <on_poweroff>destroy</on_poweroff>
          <on_reboot>restart</on_reboot>
          <on_crash>restart</on_crash>
          <devices>
            %(disks)s
            %(cdroms)s
            %(networks)s
            %(graphics)s
            %(input_output)s
            %(serial)s
            <memballoon model='virtio' />
          </devices>
        </domain>
        """ % params

        return xml
示例#6
0
    def to_vm_xml(self, vm_name, vm_uuid, **kwargs):
        params = dict(self.info)
        params['name'] = vm_name
        params['uuid'] = vm_uuid
        params['networks'] = self._get_networks_xml()
        params['input_output'] = self._get_input_output_xml()
        params['qemu-namespace'] = ''
        params['cdroms'] = ''
        params['qemu-stream-cmdline'] = ''
        params['disks'] = self._get_disks_xml(vm_uuid)
        params['serial'] = get_serial_xml(params)

        graphics = dict(self.info['graphics'])
        graphics.update(kwargs.get('graphics', {}))
        params['graphics'] = get_graphics_xml(graphics)

        libvirt_stream_protocols = kwargs.get('libvirt_stream_protocols', [])
        cdrom_xml = self._get_cdrom_xml(libvirt_stream_protocols)

        if not urlparse.urlparse(self.info.get('cdrom', "")).scheme in \
                libvirt_stream_protocols and \
                params.get('iso_stream', False):
            params['qemu-stream-cmdline'] = cdrom_xml
        else:
            params['cdroms'] = cdrom_xml

        # max memory
        params['max_memory'] = self._get_max_memory(params['memory'])

        # Setting maximum number of slots to avoid errors when hotplug memory
        # Number of slots are the numbers of chunks of 1GB that fit inside
        # the max_memory of the host minus memory assigned to the VM. It
        # cannot have more than 32 slots in Power.
        params['slots'] = ((params['max_memory'] >> 10) -
                           params['memory']) >> 10
        if params['slots'] < 0:
            raise OperationFailed("KCHVM0041E",
                                  {'maxmem': str(params['max_memory'] >> 10)})
        elif params['slots'] == 0:
            params['slots'] = 1
        elif params['slots'] > 32:
            distro, _, _ = platform.linux_distribution()
            if distro == "IBM_PowerKVM":
                params['slots'] = 32

        # set a hard limit using max_memory + 1GiB
        params['hard_limit'] = params['max_memory'] + (1024 << 10)

        # vcpu element
        cpus = params['cpu_info']['vcpus']
        maxvcpus = params['cpu_info']['maxvcpus']
        params['vcpus_xml'] = "<vcpu current='%d'>%d</vcpu>" % (cpus, maxvcpus)

        # cpu_info element
        params['cpu_info_xml'] = self._get_cpu_xml()

        xml = """
        <domain type='%(domain)s'>
          %(qemu-stream-cmdline)s
          <name>%(name)s</name>
          <uuid>%(uuid)s</uuid>
          <memtune>
            <hard_limit unit='KiB'>%(hard_limit)s</hard_limit>
          </memtune>
          <maxMemory slots='%(slots)s' unit='KiB'>%(max_memory)s</maxMemory>
          <memory unit='MiB'>%(memory)s</memory>
          %(vcpus_xml)s
          %(cpu_info_xml)s
          <os>
            <type arch='%(arch)s'>hvm</type>
            <boot dev='hd'/>
            <boot dev='cdrom'/>
          </os>
          <features>
            <acpi/>
            <apic/>
            <pae/>
          </features>
          <clock offset='utc'/>
          <on_poweroff>destroy</on_poweroff>
          <on_reboot>restart</on_reboot>
          <on_crash>restart</on_crash>
          <devices>
            %(disks)s
            %(cdroms)s
            %(networks)s
            %(graphics)s
            %(input_output)s
            %(serial)s
            <memballoon model='virtio' />
          </devices>
        </domain>
        """ % params

        return xml
示例#7
0
    def to_vm_xml(self, vm_name, vm_uuid, **kwargs):
        params = dict(self.info)
        params['name'] = vm_name
        params['uuid'] = vm_uuid
        params['networks'] = self._get_networks_xml()
        params['input_output'] = self._get_input_output_xml()
        params['qemu-namespace'] = ''
        params['cdroms'] = ''
        params['qemu-stream-cmdline'] = ''
        params['disks'] = self._get_disks_xml(vm_uuid)
        params['serial'] = get_serial_xml(params)

        graphics = dict(self.info['graphics'])
        graphics.update(kwargs.get('graphics', {}))
        params['graphics'] = get_graphics_xml(graphics)

        libvirt_stream_protocols = kwargs.get('libvirt_stream_protocols', [])
        cdrom_xml = self._get_cdrom_xml(libvirt_stream_protocols)

        max_vcpus = kwargs.get('max_vcpus', 1)

        if not urlparse.urlparse(self.info.get('cdrom', "")).scheme in \
                libvirt_stream_protocols and \
                params.get('iso_stream', False):
            params['qemu-stream-cmdline'] = cdrom_xml
        else:
            params['cdroms'] = cdrom_xml

        # In order to avoid problems with live migration, setting maxMemory of
        # the VM, which will be lesser value between:
        # [ 1TB,  (Template Memory * 4),  Host Physical Memory.
        tmp_max_mem = (params['memory'] << 10) * 4
        if tmp_max_mem < params['max_memory']:
            params['max_memory'] = tmp_max_mem

        # Setting maximum number of slots to avoid errors when hotplug memory
        # Number of slots are the numbers of chunks of 1GB that fit inside
        # the max_memory of the host minus memory assigned to the VM. It
        # cannot have more than 32 slots in Power.
        params['slots'] = ((params['max_memory'] >> 10) -
                           params['memory']) >> 10
        if params['slots'] < 0:
            raise OperationFailed("KCHVM0041E")
        elif params['slots'] == 0:
            params['slots'] = 1
        elif params['slots'] > 32:
            distro, _, _ = platform.linux_distribution()
            if distro == "IBM_PowerKVM":
                params['slots'] = 32

        # set a hard limit using max_memory + 1GiB
        params['hard_limit'] = params['max_memory'] + (1024 << 10)

        cpu_topo = self.info.get('cpu_info').get('topology')
        if (cpu_topo is not None):
            sockets = int(max_vcpus / (cpu_topo['cores'] *
                          cpu_topo['threads']))
            self.info['cpu_info']['topology']['sockets'] = sockets

            # Reduce maxvcpu to fit number of sockets if necessary
            total_max_vcpu = sockets * cpu_topo['cores'] * cpu_topo['threads']
            if total_max_vcpu != max_vcpus:
                max_vcpus = total_max_vcpu

            params['vcpus'] = "<vcpu current='%s'>%d</vcpu>" % \
                              (params['cpus'], max_vcpus)
        else:
            params['vcpus'] = "<vcpu current='%s'>%d</vcpu>" % \
                              (params['cpus'], max_vcpus)
        params['cpu_info'] = self._get_cpu_xml()

        xml = """
        <domain type='%(domain)s'>
          %(qemu-stream-cmdline)s
          <name>%(name)s</name>
          <uuid>%(uuid)s</uuid>
          <memtune>
            <hard_limit unit='KiB'>%(hard_limit)s</hard_limit>
          </memtune>
          <maxMemory slots='%(slots)s' unit='KiB'>%(max_memory)s</maxMemory>
          <memory unit='MiB'>%(memory)s</memory>
          %(vcpus)s
          %(cpu_info)s
          <os>
            <type arch='%(arch)s'>hvm</type>
            <boot dev='hd'/>
            <boot dev='cdrom'/>
          </os>
          <features>
            <acpi/>
            <apic/>
            <pae/>
          </features>
          <clock offset='utc'/>
          <on_poweroff>destroy</on_poweroff>
          <on_reboot>restart</on_reboot>
          <on_crash>restart</on_crash>
          <devices>
            %(disks)s
            %(cdroms)s
            %(networks)s
            %(graphics)s
            %(input_output)s
            %(serial)s
            <memballoon model='virtio' />
          </devices>
        </domain>
        """ % params

        return xml
示例#8
0
    def to_vm_xml(self, vm_name, vm_uuid, **kwargs):
        params = dict(self.info)
        params["name"] = vm_name
        params["uuid"] = vm_uuid
        params["networks"] = self._get_networks_xml()
        params["interfaces"] = self._get_interfaces_xml()
        params["input_output"] = self._get_input_output_xml()
        params["qemu-namespace"] = ""
        params["cdroms"] = ""
        params["qemu-stream-cmdline"] = ""
        params["disks"] = self._get_disks_xml(vm_uuid)
        params["serial"] = get_serial_xml(params)
        params["title"] = kwargs.get("title", "")
        params["description"] = kwargs.get("description", "")
        graphics = dict(self.info["graphics"])
        graphics.update(kwargs.get("graphics", {}))
        # Graphics is not supported on s390x, this check will
        # not add graphics tag in domain xml.
        if params.get("arch") != "s390x":
            params["graphics"] = get_graphics_xml(graphics)

        libvirt_stream_protocols = kwargs.get("libvirt_stream_protocols", [])
        cdrom_xml = self._get_cdrom_xml(libvirt_stream_protocols)

        # Add information of CD-ROM device only if template have info about it.
        if cdrom_xml is not None:
            if not urlparse.urlparse(self.info.get("cdrom", "")).scheme in libvirt_stream_protocols and params.get(
                "iso_stream", False
            ):
                params["qemu-stream-cmdline"] = cdrom_xml
            else:
                params["cdroms"] = cdrom_xml

        # Set the boot order of VM
        # TODO: need modify this when boot order edition feature came upstream.
        if cdrom_xml and params.get("arch") == "s390x":
            params["boot_order"] = get_bootorder_xml(["cdrom", "hd", "network"])
        else:
            params["boot_order"] = get_bootorder_xml()

        # Setting maximum number of memory slots
        slots = str(self.info["mem_dev_slots"])

        # Rearrange memory parameters
        memory = self.info["memory"].get("current")
        maxmemory = self.info["memory"].get("maxmemory")
        if maxmemory < memory:
            raise OperationFailed("KCHVM0041E", {"maxmem": str(maxmemory)})

        params["memory"] = self.info["memory"].get("current")
        params["max_memory"] = ""
        # if there is not support to memory hotplug in Libvirt or qemu, we
        # cannot add the tag maxMemory
        if memory != maxmemory and kwargs.get("mem_hotplug_support", True):
            maxmem_xml = "<maxMemory slots='%s' unit='MiB'>%s</maxMemory>"
            params["max_memory"] = maxmem_xml % (slots, maxmemory)

        # set a hard limit using max_memory + 1GiB
        params["hard_limit"] = maxmemory + 1024

        # vcpu element
        cpus = params["cpu_info"]["vcpus"]
        maxvcpus = params["cpu_info"]["maxvcpus"]
        params["vcpus_xml"] = "<vcpu current='%d'>%d</vcpu>" % (cpus, maxvcpus)

        # cpu_info element
        params["cpu_info_xml"] = self._get_cpu_xml()

        # usb controller
        params["usb_controller"] = self._get_usb_controller()

        xml = (
            """
        <domain type='%(domain)s'>
          %(qemu-stream-cmdline)s
          <name>%(name)s</name>
          <title>%(title)s</title>
          <description>%(description)s</description>
          <uuid>%(uuid)s</uuid>
          <memtune>
            <hard_limit unit='MiB'>%(hard_limit)s</hard_limit>
          </memtune>
          %(max_memory)s
          <memory unit='MiB'>%(memory)s</memory>
          %(vcpus_xml)s
          %(cpu_info_xml)s
          <os>
            <type arch='%(arch)s'>hvm</type>
            %(boot_order)s
          </os>
          <features>
            <acpi/>
            <apic/>
            <pae/>
          </features>
          <clock offset='utc'/>
          <on_poweroff>destroy</on_poweroff>
          <on_reboot>restart</on_reboot>
          <on_crash>restart</on_crash>
          <devices>
            %(disks)s
            %(cdroms)s
            %(networks)s
            %(interfaces)s
            %(graphics)s
            %(input_output)s
            %(usb_controller)s
            %(serial)s
            <memballoon model='virtio' />
          </devices>
        </domain>
        """
            % params
        )

        return xml
示例#9
0
    def to_vm_xml(self, vm_name, vm_uuid, **kwargs):
        params = dict(self.info)
        params['name'] = vm_name
        params['uuid'] = vm_uuid
        params['networks'] = self._get_networks_xml()
        params['input_output'] = self._get_input_output_xml()
        params['qemu-namespace'] = ''
        params['cdroms'] = ''
        params['qemu-stream-cmdline'] = ''
        params['cpu_info'] = self._get_cpu_xml()
        params['disks'] = self._get_disks_xml(vm_uuid)

        graphics = dict(self.info['graphics'])
        graphics.update(kwargs.get('graphics', {}))
        params['graphics'] = get_graphics_xml(graphics)

        libvirt_stream_protocols = kwargs.get('libvirt_stream_protocols', [])
        cdrom_xml = self._get_cdrom_xml(libvirt_stream_protocols)

        if not urlparse.urlparse(self.info.get('cdrom', "")).scheme in \
                libvirt_stream_protocols and \
                params.get('iso_stream', False):
            params['qemu-stream-cmdline'] = cdrom_xml
        else:
            params['cdroms'] = cdrom_xml

        # Setting maximum number of slots to avoid errors when hotplug memory
        # Number of slots are the numbers of chunks of 1GB that fit inside
        # the max_memory of the host minus memory assigned to the VM
        params['slots'] = ((params['max_memory'] >> 10) -
                           params['memory']) >> 10
        if params['slots'] < 0:
            raise OperationFailed("KCHVM0041E")
        elif params['slots'] == 0:
            params['slots'] = 1

        xml = """
        <domain type='%(domain)s'>
          %(qemu-stream-cmdline)s
          <name>%(name)s</name>
          <uuid>%(uuid)s</uuid>
          <maxMemory slots='%(slots)s' unit='KiB'>%(max_memory)s</maxMemory>
          <memory unit='MiB'>%(memory)s</memory>
          <vcpu>%(cpus)s</vcpu>
          %(cpu_info)s
          <os>
            <type arch='%(arch)s'>hvm</type>
            <boot dev='hd'/>
            <boot dev='cdrom'/>
          </os>
          <features>
            <acpi/>
            <apic/>
            <pae/>
          </features>
          <clock offset='utc'/>
          <on_poweroff>destroy</on_poweroff>
          <on_reboot>restart</on_reboot>
          <on_crash>restart</on_crash>
          <devices>
            %(disks)s
            %(cdroms)s
            %(networks)s
            %(graphics)s
            %(input_output)s
            <memballoon model='virtio' />
          </devices>
        </domain>
        """ % params

        # Adding PPC console configuration
        if params['arch'] in ['ppc', 'ppc64']:
            ppc_console = """<memballoon model='virtio' />
            <console type='pty'>
              <target type='serial' port='1'/>
              <address type='spapr-vio' reg='0x30001000'/>
            </console>"""
            xml = xml.replace("<memballoon model='virtio' />", ppc_console)

        return xml