Ejemplo n.º 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
Ejemplo n.º 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)

        # 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 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_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
Ejemplo n.º 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
Ejemplo n.º 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["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