def _export_disk(self, instance):
        LOG.debug("Trying to get the root virtual hard driver.")
        current_disk = pathutils.get_root_disk_path(instance)
        if not current_disk:
            raise exception.VBoxException("Cannot get the root disk.")

        disk_info = vhdutils.disk_info(current_disk)
        root_disk_uuid = disk_info[constants.VHD_PARENT_UUID]
        root_disk = vhdutils.disk_info(root_disk_uuid)

        # The root virtual disk is a base disk
        root_disk_path = root_disk[constants.VHD_PATH]
        export_path = os.path.join(
            pathutils.export_dir(instance, constants.PATH_CREATE),
            os.path.basename(root_disk_path))

        LOG.debug("Trying to export the virtual disk to %(export_path)s.",
                  {"export_path": export_path})
        existing = constants.VHD_PARENT_UUID in root_disk
        if existing:
            # The root virtual disk is linked to another disk
            base_info = vhdutils.disk_info(
                root_disk[constants.VHD_PARENT_UUID])
            base_disk_path = base_info[constants.VHD_PATH]
            try:
                LOG.debug("Cloning base disk for the root disk.")
                self._vbox_manage.clone_hd(
                    vhd_path=base_disk_path,
                    new_vdh_path=export_path,
                    disk_format=root_disk[constants.VHD_IMAGE_TYPE])
            except exception.VBoxException:
                with excutils.save_and_reraise_exception():
                    self._clenup_disk(export_path)

        try:
            LOG.debug("Cloning VHD image %(base)s to target: %(target)s", {
                'base': root_disk_path,
                'target': export_path
            })

            # Note(alexandrucoman): If the target disk already exists
            # only the portion of the source medium which fits into the
            # destination medium is copied.
            # This means if the destination medium is smaller than the
            # source only a part of it is copied, and if the destination
            # medium is larger than the source the remaining part of the
            # destination medium is unchanged.
            self._vbox_manage.clone_hd(root_disk_path,
                                       export_path,
                                       existing=existing)
        except exception.VBoxException:
            with excutils.save_and_reraise_exception():
                self._clenup_disk(export_path)

        return export_path
    def _export_disk(self, instance):
        LOG.debug("Trying to get the root virtual hard driver.")
        current_disk = pathutils.get_root_disk_path(instance)
        if not current_disk:
            raise exception.VBoxException("Cannot get the root disk.")

        disk_info = vhdutils.disk_info(current_disk)
        root_disk_uuid = disk_info[constants.VHD_PARENT_UUID]
        root_disk = vhdutils.disk_info(root_disk_uuid)

        # The root virtual disk is a base disk
        root_disk_path = root_disk[constants.VHD_PATH]
        export_path = os.path.join(
            pathutils.export_dir(instance, constants.PATH_CREATE),
            os.path.basename(root_disk_path))

        LOG.debug("Trying to export the virtual disk to %(export_path)s.",
                  {"export_path": export_path})
        existing = constants.VHD_PARENT_UUID in root_disk
        if existing:
            # The root virtual disk is linked to another disk
            base_info = vhdutils.disk_info(
                root_disk[constants.VHD_PARENT_UUID])
            base_disk_path = base_info[constants.VHD_PATH]
            try:
                LOG.debug("Cloning base disk for the root disk.")
                self._vbox_manage.clone_hd(
                    vhd_path=base_disk_path,
                    new_vdh_path=export_path,
                    disk_format=root_disk[constants.VHD_IMAGE_TYPE])
            except exception.VBoxException:
                with excutils.save_and_reraise_exception():
                    self._clenup_disk(export_path)

        try:
            LOG.debug(
                "Cloning VHD image %(base)s to target: %(target)s",
                {'base': root_disk_path, 'target': export_path})

            # Note(alexandrucoman): If the target disk already exists
            # only the portion of the source medium which fits into the
            # destination medium is copied.
            # This means if the destination medium is smaller than the
            # source only a part of it is copied, and if the destination
            # medium is larger than the source the remaining part of the
            # destination medium is unchanged.
            self._vbox_manage.clone_hd(root_disk_path, export_path,
                                       existing=existing)
        except exception.VBoxException:
            with excutils.save_and_reraise_exception():
                self._clenup_disk(export_path)

        return export_path
Exemple #3
0
    def _check_disk(self, disk_file, base_disk):
        try:
            vhdutils.check_disk_uuid(disk_file)
            disk_info = vhdutils.disk_info(disk_file)
            parent_uuid = disk_info[constants.VHD_PARENT_UUID]
            if not parent_uuid:
                return
        except vbox_exc.VBoxException:
            parent_uuid = None

        registered_hdds = vhdutils.get_hard_disks()
        if parent_uuid not in registered_hdds:
            parent_info = vhdutils.disk_info(base_disk)
            self._vbox_manage.set_disk_parent_uuid(
                disk_file, parent_info[constants.VHD_UUID])
    def _check_disk(self, disk_file, base_disk):
        try:
            vhdutils.check_disk_uuid(disk_file)
            disk_info = vhdutils.disk_info(disk_file)
            parent_uuid = disk_info[constants.VHD_PARENT_UUID]
            if not parent_uuid:
                return
        except vbox_exc.VBoxException:
            parent_uuid = None

        registered_hdds = vhdutils.get_hard_disks()
        if parent_uuid not in registered_hdds:
            parent_info = vhdutils.disk_info(base_disk)
            self._vbox_manage.set_disk_parent_uuid(
                disk_file, parent_info[constants.VHD_UUID])
def _fetch_image(context, instance, image_path):
    disk_path = None
    try:
        images.fetch(context, instance.image_ref, image_path,
                     instance.user_id, instance.project_id)
        # Avoid conflicts
        vhdutils.check_disk_uuid(image_path)

        disk_info = vhdutils.disk_info(image_path)
        disk_format = disk_info[constants.VHD_IMAGE_TYPE]
        disk_path = image_path + "." + disk_format.lower()

        manage.VBoxManage.clone_hd(image_path, disk_path,
                                   disk_format=disk_format)
        manage.VBoxManage.close_medium(constants.MEDIUM_DISK, image_path,
                                       delete=True)

    except (vbox_exc.VBoxException, exception.NovaException):
        with excutils.save_and_reraise_exception():
            for path in (image_path, disk_path):
                if path and os.path.exists(path):
                    manage.VBoxManage.close_medium(constants.MEDIUM_DISK,
                                                   path)
                    pathutils.delete_path(path)

    return disk_path
Exemple #6
0
def _fetch_image(context, instance, image_path):
    disk_path = None
    try:
        images.fetch(context, instance.image_ref, image_path, instance.user_id,
                     instance.project_id)
        # Avoid conflicts
        vhdutils.check_disk_uuid(image_path)

        disk_info = vhdutils.disk_info(image_path)
        disk_format = disk_info[constants.VHD_IMAGE_TYPE]
        disk_path = image_path + "." + disk_format.lower()

        manage.VBoxManage.clone_hd(image_path,
                                   disk_path,
                                   disk_format=disk_format)
        manage.VBoxManage.close_medium(constants.MEDIUM_DISK,
                                       image_path,
                                       delete=True)

    except (vbox_exc.VBoxException, exception.NovaException):
        with excutils.save_and_reraise_exception():
            for path in (image_path, disk_path):
                if path and os.path.exists(path):
                    manage.VBoxManage.close_medium(constants.MEDIUM_DISK, path)
                    pathutils.delete_path(path)

    return disk_path
 def _resize_disk(self, instance, new_size, disk_file):
     if not new_size:
         return
     disk_info = vhdutils.disk_info(disk_file)
     current_size = disk_info[constants.VHD_CAPACITY] / units.Mi
     if vhdutils.is_resize_required(disk_file, current_size,
                                    new_size, instance):
         self._vbox_manage.modify_hd(
             disk_file, constants.FIELD_HD_RESIZE_MB, new_size)
Exemple #8
0
 def _resize_disk(self, instance, new_size, disk_file):
     if not new_size:
         return
     disk_info = vhdutils.disk_info(disk_file)
     current_size = disk_info[constants.VHD_CAPACITY] / units.Mi
     if vhdutils.is_resize_required(disk_file, current_size, new_size,
                                    instance):
         self._vbox_manage.modify_hd(disk_file,
                                     constants.FIELD_HD_RESIZE_MB, new_size)
Exemple #9
0
    def _migrate_disk(self, disk_file, destination, root_disk=False):
        disk_info = vhdutils.disk_info(disk_file)
        disk_format = disk_info[constants.VHD_IMAGE_TYPE]

        if root_disk:
            dest_file = os.path.join(destination,
                                     "root." + disk_format.lower())
        else:
            dest_file = os.path.join(destination, os.path.basename(disk_file))

        if not disk_info[constants.VHD_PARENT_UUID]:
            self._vbox_manage.clone_hd(disk_file, dest_file, disk_format)
        else:
            shutil.copy(disk_file, dest_file)
    def _migrate_disk(self, disk_file, destination, root_disk=False):
        disk_info = vhdutils.disk_info(disk_file)
        disk_format = disk_info[constants.VHD_IMAGE_TYPE]

        if root_disk:
            dest_file = os.path.join(destination,
                                     "root." + disk_format.lower())
        else:
            dest_file = os.path.join(destination,
                                     os.path.basename(disk_file))

        if not disk_info[constants.VHD_PARENT_UUID]:
            self._vbox_manage.clone_hd(disk_file, dest_file, disk_format)
        else:
            shutil.copy(disk_file, dest_file)
    def create_root_disk(self, context, instance):
        base_vhd_path = imagecache.get_cached_image(context, instance)
        base_info = vhdutils.disk_info(base_vhd_path)
        root_vhd_path = pathutils.root_disk_path(
            instance, disk_format=base_info[constants.VHD_IMAGE_TYPE])

        if CONF.use_cow_images:
            LOG.debug(
                "Creating differencing VHD. Parent: %(parent)s, "
                "Target: %(target)s", {
                    'parent': base_vhd_path,
                    'target': root_vhd_path
                },
                instance=instance)
            self._vbox_manage.create_hd(
                filename=root_vhd_path,
                # disk_format=base_info[constants.VHD_IMAGE_TYPE],
                variant=constants.VARIANT_STANDARD,
                parent=base_vhd_path)

        else:
            LOG.debug(
                "Cloning VHD image %(base)s to target: %(target)s", {
                    'base': base_vhd_path,
                    'target': root_vhd_path
                },
                instance=instance)
            self._vbox_manage.clone_hd(
                vhd_path=base_vhd_path,
                new_vdh_path=root_vhd_path,
                disk_format=base_info[constants.VHD_IMAGE_TYPE],
                variant=constants.VARIANT_STANDARD)

        # Resize image if is necesary
        disk_format = vhdutils.get_image_type(root_vhd_path)
        if instance.root_gb and disk_format in (constants.DISK_FORMAT_VDI,
                                                constants.DISK_FORMAT_VHD):
            base_vhd_size = base_info[constants.VHD_CAPACITY] / units.Mi
            root_vhd_size = instance.root_gb * units.Ki
            if vhdutils.is_resize_required(
                    disk_path=root_vhd_path,
                    old_size=base_vhd_size,
                    new_size=root_vhd_size,
                    instance=instance):
                self._vbox_manage.modify_hd(
                    root_vhd_path, constants.FIELD_HD_RESIZE_MB, root_vhd_size)

        return root_vhd_path
Exemple #12
0
    def create_root_disk(self, context, instance):
        base_vhd_path = imagecache.get_cached_image(context, instance)
        base_info = vhdutils.disk_info(base_vhd_path)
        root_vhd_path = pathutils.root_disk_path(
            instance, disk_format=base_info[constants.VHD_IMAGE_TYPE])

        if CONF.use_cow_images:
            LOG.debug(
                "Creating differencing VHD. Parent: %(parent)s, "
                "Target: %(target)s", {
                    'parent': base_vhd_path,
                    'target': root_vhd_path
                },
                instance=instance)
            self._vbox_manage.create_hd(
                filename=root_vhd_path,
                # disk_format=base_info[constants.VHD_IMAGE_TYPE],
                variant=constants.VARIANT_STANDARD,
                parent=base_vhd_path)

        else:
            LOG.debug("Cloning VHD image %(base)s to target: %(target)s", {
                'base': base_vhd_path,
                'target': root_vhd_path
            },
                      instance=instance)
            self._vbox_manage.clone_hd(
                vhd_path=base_vhd_path,
                new_vdh_path=root_vhd_path,
                disk_format=base_info[constants.VHD_IMAGE_TYPE],
                variant=constants.VARIANT_STANDARD)

        # Resize image if is necesary
        disk_format = vhdutils.get_image_type(root_vhd_path)
        if instance.root_gb and disk_format in (constants.DISK_FORMAT_VDI,
                                                constants.DISK_FORMAT_VHD):
            base_vhd_size = base_info[constants.VHD_CAPACITY] / units.Mi
            root_vhd_size = instance.root_gb * units.Ki
            if vhdutils.is_resize_required(disk_path=root_vhd_path,
                                           old_size=base_vhd_size,
                                           new_size=root_vhd_size,
                                           instance=instance):
                self._vbox_manage.modify_hd(root_vhd_path,
                                            constants.FIELD_HD_RESIZE_MB,
                                            root_vhd_size)

        return root_vhd_path