def _create_vm(self, context, instance, vdis, network_info):
        """Create VM instance."""
        instance_name = instance.name
        vm_ref = VMHelper.lookup(self._session, instance_name)
        if vm_ref is not None:
            raise exception.InstanceExists(name=instance_name)

        #ensure enough free memory is available
        if not VMHelper.ensure_free_mem(self._session, instance):
            raise exception.InsufficientFreeMemory(uuid=instance.uuid)

        disk_image_type = VMHelper.determine_disk_image_type(instance, context)
        kernel = None
        ramdisk = None
        try:
            if instance.kernel_id:
                kernel = VMHelper.fetch_image(context, self._session,
                        instance, instance.kernel_id, instance.user_id,
                        instance.project_id, ImageType.KERNEL)[0]
            if instance.ramdisk_id:
                ramdisk = VMHelper.fetch_image(context, self._session,
                        instance, instance.ramdisk_id, instance.user_id,
                        instance.project_id, ImageType.RAMDISK)[0]

            # NOTE(jk0): Since vdi_type may contain either 'os' or 'swap', we
            # need to ensure that the 'swap' VDI is not chosen as the mount
            # point for file injection.
            first_vdi_ref = None
            for vdi in vdis:
                if vdi.get('vdi_type') != 'swap':
                    # Create the VM ref and attach the first disk
                    first_vdi_ref = self._session.call_xenapi(
                            'VDI.get_by_uuid', vdi['vdi_uuid'])

            vm_mode = instance.vm_mode and instance.vm_mode.lower()
            if vm_mode == 'pv':
                use_pv_kernel = True
            elif vm_mode in ('hv', 'hvm'):
                use_pv_kernel = False
                vm_mode = 'hvm'  # Normalize
            else:
                use_pv_kernel = VMHelper.determine_is_pv(self._session,
                        instance.id, first_vdi_ref, disk_image_type,
                        instance.os_type)
                vm_mode = use_pv_kernel and 'pv' or 'hvm'

            if instance.vm_mode != vm_mode:
                # Update database with normalized (or determined) value
                db.instance_update(nova_context.get_admin_context(),
                                   instance['id'], {'vm_mode': vm_mode})
            vm_ref = VMHelper.create_vm(self._session, instance,
                    kernel and kernel.get('file', None) or None,
                    ramdisk and ramdisk.get('file', None) or None,
                    use_pv_kernel)
        except (self.XenAPI.Failure, OSError, IOError) as vm_create_error:
            # Collect VDI/file resources to clean up;
            # These resources will be removed by _handle_spawn_error.
            LOG.exception(_("instance %s: Failed to spawn - " +
                            "Unable to create VM"),
                          instance.id, exc_info=sys.exc_info())
            last_arg = None
            resources = []

            if vm_create_error.args:
                last_arg = vm_create_error.args[-1]
            if isinstance(last_arg, list):
                resources = last_arg
            else:
                vm_create_error.args = vm_create_error.args + (resources,)

            if kernel:
                resources.append(kernel)
            if ramdisk:
                resources.append(ramdisk)

            raise vm_create_error

        # Add disks to VM
        self._attach_disks(instance, disk_image_type, vm_ref, first_vdi_ref,
            vdis)

        # Alter the image before VM start for network injection.
        if FLAGS.flat_injected:
            VMHelper.preconfigure_instance(self._session, instance,
                                           first_vdi_ref, network_info)

        self.create_vifs(vm_ref, instance, network_info)
        self.inject_network_info(instance, network_info, vm_ref)
        self.inject_hostname(instance, vm_ref, instance['hostname'])

        return vm_ref
dataset/ETHPy150Open nii-cloud/dodai-compute/nova/virt/xenapi/vmops.py/VMOps._create_vm/original