Ejemplo n.º 1
0
    def create_disk_from_image(self, context, instance, image, disk_size,
                               image_type=disk_dvr.DiskType.BOOT):
        """Creates a disk and copies the specified image to it.

        :param context: nova context used to retrieve image from glance
        :param instance: instance to create the disk for.
        :param image: image dict used to locate the image in glance
        :param disk_size: The size of the disk to create in GB.  If smaller
                          than the image, it will be ignored (as the disk
                          must be at least as big as the image).  Must be an
                          int.
        :param image_type: the image type. See disk constants above.
        :return: The backing pypowervm storage object that was created.
        """
        LOG.info(_LI('Create disk.'))

        # Transfer the image
        stream = self._get_image_upload(context, image)
        vol_name = self._get_disk_name(image_type, instance, short=True)

        # Disk size to API is in bytes.  Input from method is in Gb
        disk_bytes = self._disk_gb_to_bytes(disk_size, floor=image['size'])

        # This method will create a new disk at our specified size.  It will
        # then put the image in the disk.  If the disk is bigger, user can
        # resize the disk, create a new partition, etc...
        # If the image is bigger than disk, API should make the disk big
        # enough to support the image (up to 1 Gb boundary).
        vdisk, f_wrap = tsk_stg.upload_new_vdisk(
            self.adapter, self._vios_uuid, self.vg_uuid, stream, vol_name,
            image['size'], d_size=disk_bytes)

        return vdisk
Ejemplo n.º 2
0
    def _get_or_upload_image(self, context, image_meta):
        """Return a cached image name

        Attempt to find a cached copy of the image. If there is no cached copy
        of the image, create one.

        :param context: nova context used to retrieve image from glance
        :param nova.objects.ImageMeta image_meta:
            The metadata of the image of the instance.
        :return: The name of the virtual disk containing the image
        """

        # Check for cached image
        with lockutils.lock(image_meta.id):
            vg_wrap = self._get_vg_wrap()
            cache_name = self.get_name_by_uuid(disk_dvr.DiskType.IMAGE,
                                               image_meta.id, short=True)
            image = [disk for disk in vg_wrap.virtual_disks
                     if disk.name == cache_name]
            if len(image) == 1:
                return image[0].udid

            image = tsk_stg.upload_new_vdisk(
                self.adapter, self._vios_uuid, self.vg_uuid,
                disk_dvr.IterableToFileAdapter(
                    IMAGE_API.download(context, image_meta.id)), cache_name,
                image_meta.size, d_size=image_meta.size,
                upload_type=tsk_stg.UploadType.IO_STREAM,
                file_format=image_meta.disk_format)[0]
            return image.udid
Ejemplo n.º 3
0
    def test_upload_new_vdisk_coordinated(self, mock_create_file):
        """Tests the uploads of a virtual disk using the coordinated path."""

        # Override adapter's traits to use the coordinated local API
        self.adptfx.set_traits(fx.LocalPVMTraits)

        # First need to load in the various test responses.
        vg_orig = tju.load_file(UPLOAD_VOL_GRP_ORIG, self.adpt)
        vg_post_crt = tju.load_file(UPLOAD_VOL_GRP_NEW_VDISK, self.adpt)

        self.adpt.read.return_value = vg_orig
        self.adpt.update_by_path.return_value = vg_post_crt
        mock_create_file.return_value = self._fake_meta()

        n_vdisk, f_wrap = ts.upload_new_vdisk(
            self.adpt, self.v_uuid, self.vg_uuid, None, 'test2', 50,
            d_size=25, sha_chksum='abc123')

        # Ensure the create file was called
        mock_create_file.assert_called_once_with(
            self.adpt, 'test2', vf.FileType.DISK_IMAGE_COORDINATED,
            self.v_uuid, f_size=50,
            tdev_udid='0300f8d6de00004b000000014a54555cd9.3',
            sha_chksum='abc123')

        # Ensure cleanup was called after the upload
        self.adpt.delete.assert_called_once_with(
            'File', service='web',
            root_id='6233b070-31cc-4b57-99bd-37f80e845de9')
        self.assertIsNone(f_wrap)
        self.assertIsNotNone(n_vdisk)
        self.assertIsInstance(n_vdisk, stor.VDisk)
Ejemplo n.º 4
0
    def create_disk_from_image(self, context, instance, image, disk_size,
                               image_type=disk_dvr.DiskType.BOOT):
        """Creates a disk and copies the specified image to it.

        :param context: nova context used to retrieve image from glance
        :param instance: instance to create the disk for.
        :param image: image dict used to locate the image in glance
        :param disk_size: The size of the disk to create in GB.  If smaller
                          than the image, it will be ignored (as the disk
                          must be at least as big as the image).  Must be an
                          int.
        :param image_type: the image type. See disk constants above.
        :return: The backing pypowervm storage object that was created.
        """
        LOG.info(_LI('Create disk.'))

        # Transfer the image
        stream = self._get_image_upload(context, image)
        vol_name = self._get_disk_name(image_type, instance, short=True)

        # Disk size to API is in bytes.  Input from method is in Gb
        disk_bytes = self._disk_gb_to_bytes(disk_size, floor=image['size'])

        # This method will create a new disk at our specified size.  It will
        # then put the image in the disk.  If the disk is bigger, user can
        # resize the disk, create a new partition, etc...
        # If the image is bigger than disk, API should make the disk big
        # enough to support the image (up to 1 Gb boundary).
        vdisk, f_wrap = tsk_stg.upload_new_vdisk(
            self.adapter, self._vios_uuid, self.vg_uuid, stream, vol_name,
            image['size'], d_size=disk_bytes)

        return vdisk
    def _upload_image(self, context, instance, image_meta):
        """Upload a new image.

        :param context: Nova context used to retrieve image from glance.
        :param image_meta: The metadata of the image of the instance.
        :return: The virtual disk containing the image.
        """

        img_name = self._get_disk_name(disk_dvr.DiskType.BOOT,
                                       instance,
                                       short=True)

        # TODO(esberglu) Add check for cached image when adding imagecache.

        return tsk_stg.upload_new_vdisk(
            self._adapter,
            self._vios_uuid,
            self.vg_uuid,
            disk_dvr.IterableToFileAdapter(
                IMAGE_API.download(context, image_meta.id)),
            img_name,
            image_meta.size,
            d_size=image_meta.size,
            upload_type=tsk_stg.UploadType.IO_STREAM,
            file_format=image_meta.disk_format)[0]
Ejemplo n.º 6
0
    def test_upload_new_vdisk_failure(self, mock_create_file):
        """Tests the failure path for uploading of the virtual disks."""

        # First need to load in the various test responses.
        vg_orig = tju.load_file(UPLOAD_VOL_GRP_ORIG, self.adpt)
        vg_post_crt = tju.load_file(UPLOAD_VOL_GRP_NEW_VDISK, self.adpt)

        self.adpt.read.return_value = vg_orig
        self.adpt.update_by_path.return_value = vg_post_crt
        mock_create_file.return_value = self._fake_meta()

        self.assertRaises(exc.Error, ts.upload_new_vdisk, self.adpt,
                          self.v_uuid, self.vg_uuid, None, 'test3', 50)

        # Test cleanup failure
        self.adpt.delete.side_effect = exc.Error('Something bad')
        f_wrap = ts.upload_new_vdisk(self.adpt,
                                     self.v_uuid,
                                     self.vg_uuid,
                                     None,
                                     'test2',
                                     50,
                                     sha_chksum='abc123')

        self.adpt.delete.assert_called_once_with(
            'File',
            service='web',
            root_id='6233b070-31cc-4b57-99bd-37f80e845de9')
        self.assertIsNotNone(f_wrap)
Ejemplo n.º 7
0
    def _upload_image(self, context, instance, image_meta):
        """Upload a new image.

        :param context: Nova context used to retrieve image from glance.
        :param image_meta: The metadata of the image of the instance.
        :return: The virtual disk containing the image.
        """

        img_name = self._get_disk_name(disk_dvr.DiskType.BOOT, instance,
                                       short=True)

        # TODO(esberglu) Add check for cached image when adding imagecache.

        return tsk_stg.upload_new_vdisk(
            self._adapter, self._vios_uuid, self.vg_uuid,
            disk_dvr.IterableToFileAdapter(
                IMAGE_API.download(context, image_meta.id)), img_name,
            image_meta.size, d_size=image_meta.size,
            upload_type=tsk_stg.UploadType.IO_STREAM,
            file_format=image_meta.disk_format)[0]
Ejemplo n.º 8
0
    def test_upload_new_vdisk_coordinated(self, mock_create_file):
        """Tests the uploads of a virtual disk using the coordinated path."""

        # Override adapter's traits to use the coordinated local API
        self.adptfx.set_traits(fx.LocalPVMTraits)

        # First need to load in the various test responses.
        vg_orig = tju.load_file(UPLOAD_VOL_GRP_ORIG, self.adpt)
        vg_post_crt = tju.load_file(UPLOAD_VOL_GRP_NEW_VDISK, self.adpt)

        self.adpt.read.return_value = vg_orig
        self.adpt.update_by_path.return_value = vg_post_crt
        mock_create_file.return_value = self._fake_meta()

        n_vdisk, f_wrap = ts.upload_new_vdisk(self.adpt,
                                              self.v_uuid,
                                              self.vg_uuid,
                                              None,
                                              'test2',
                                              50,
                                              d_size=25,
                                              sha_chksum='abc123')

        # Ensure the create file was called
        mock_create_file.assert_called_once_with(
            self.adpt,
            'test2',
            vf.FileType.DISK_IMAGE_COORDINATED,
            self.v_uuid,
            f_size=50,
            tdev_udid='0300f8d6de00004b000000014a54555cd9.3',
            sha_chksum='abc123')

        # Ensure cleanup was called after the upload
        self.adpt.delete.assert_called_once_with(
            'File',
            service='web',
            root_id='6233b070-31cc-4b57-99bd-37f80e845de9')
        self.assertIsNone(f_wrap)
        self.assertIsNotNone(n_vdisk)
        self.assertIsInstance(n_vdisk, stor.VDisk)
Ejemplo n.º 9
0
    def test_upload_new_vdisk_failure(self, mock_create_file):
        """Tests the failure path for uploading of the virtual disks."""

        # First need to load in the various test responses.
        vg_orig = tju.load_file(UPLOAD_VOL_GRP_ORIG, self.adpt)
        vg_post_crt = tju.load_file(UPLOAD_VOL_GRP_NEW_VDISK, self.adpt)

        self.adpt.read.return_value = vg_orig
        self.adpt.update_by_path.return_value = vg_post_crt
        mock_create_file.return_value = self._fake_meta()

        self.assertRaises(exc.Error, ts.upload_new_vdisk, self.adpt,
                          self.v_uuid, self.vg_uuid, None, 'test3', 50)

        # Test cleanup failure
        self.adpt.delete.side_effect = exc.Error('Something bad')
        f_wrap = ts.upload_new_vdisk(self.adpt, self.v_uuid, self.vg_uuid,
                                     None, 'test2', 50, sha_chksum='abc123')

        self.adpt.delete.assert_called_once_with(
            'File', service='web',
            root_id='6233b070-31cc-4b57-99bd-37f80e845de9')
        self.assertIsNotNone(f_wrap)