예제 #1
0
def upload_volume(context, image_service, image_meta, volume_path):
    image_id = image_meta['id']
    if (image_meta['disk_format'] == 'raw'):
        LOG.debug("%s was raw, no need to convert to %s" %
                  (image_id, image_meta['disk_format']))
        with utils.temporary_chown(volume_path):
            with utils.file_open(volume_path) as image_file:
                image_service.update(context, image_id, {}, image_file)
        return

    if (FLAGS.image_conversion_dir and not
            os.path.exists(FLAGS.image_conversion_dir)):
        os.makedirs(FLAGS.image_conversion_dir)

    fd, tmp = tempfile.mkstemp(dir=FLAGS.image_conversion_dir)
    os.close(fd)
    with utils.remove_path_on_error(tmp):
        LOG.debug("%s was raw, converting to %s" %
                  (image_id, image_meta['disk_format']))
        convert_image(volume_path, tmp, image_meta['disk_format'])

        data = qemu_img_info(tmp)
        if data.file_format != image_meta['disk_format']:
            raise exception.ImageUnacceptable(
                image_id=image_id,
                reason=_("Converted to %(f1)s, but format is now %(f2)s") %
                {'f1': image_meta['disk_format'], 'f2': data.file_format})

        with utils.file_open(tmp) as image_file:
            image_service.update(context, image_id, {}, image_file)
        os.unlink(tmp)
예제 #2
0
def upload_volume(context, image_service, image_meta, volume_path):
    image_id = image_meta['id']
    if (image_meta['disk_format'] == 'raw'):
        LOG.debug("%s was raw, no need to convert to %s" %
                  (image_id, image_meta['disk_format']))
        with utils.temporary_chown(volume_path):
            with utils.file_open(volume_path) as image_file:
                image_service.update(context, image_id, {}, image_file)
        return

    if (FLAGS.image_conversion_dir and not
            os.path.exists(FLAGS.image_conversion_dir)):
        os.makedirs(FLAGS.image_conversion_dir)

    fd, tmp = tempfile.mkstemp(dir=FLAGS.image_conversion_dir)
    os.close(fd)
    with utils.remove_path_on_error(tmp):
        LOG.debug("%s was raw, converting to %s" %
                  (image_id, image_meta['disk_format']))
        convert_image(volume_path, tmp, image_meta['disk_format'])

        data = qemu_img_info(tmp)
        if data.file_format != image_meta['disk_format']:
            raise exception.ImageUnacceptable(
                image_id=image_id,
                reason=_("Converted to %(f1)s, but format is now %(f2)s") %
                {'f1': image_meta['disk_format'], 'f2': data.file_format})

        with utils.file_open(tmp) as image_file:
            image_service.update(context, image_id, {}, image_file)
        os.unlink(tmp)
예제 #3
0
def fetch(context, image_service, image_id, path, _user_id, _project_id):
    # TODO(vish): Improve context handling and add owner and auth data
    #             when it is added to glance.  Right now there is no
    #             auth checking in glance, so we assume that access was
    #             checked before we got here.
    with utils.remove_path_on_error(path):
        with open(path, "wb") as image_file:
            image_service.download(context, image_id, image_file)
예제 #4
0
def fetch(context, image_service, image_id, path, _user_id, _project_id):
    # TODO(vish): Improve context handling and add owner and auth data
    #             when it is added to glance.  Right now there is no
    #             auth checking in glance, so we assume that access was
    #             checked before we got here.
    with utils.remove_path_on_error(path):
        with open(path, "wb") as image_file:
            image_service.download(context, image_id, image_file)
예제 #5
0
파일: rbd.py 프로젝트: twigs/cinder
    def copy_volume_to_image(self, context, volume, image_service, image_meta):
        self._ensure_tmp_exists()

        tmp_dir = self.configuration.volume_tmp_dir or '/tmp'
        tmp_file = os.path.join(tmp_dir,
                                volume['name'] + '-' + image_meta['id'])
        with utils.remove_path_on_error(tmp_file):
            self._try_execute('rbd', 'export',
                              '--pool', self.configuration.rbd_pool,
                              volume['name'], tmp_file)
            image_utils.upload_volume(context, image_service,
                                      image_meta, tmp_file)
        os.unlink(tmp_file)
예제 #6
0
def fetch_to_raw(context, image_service,
                 image_id, dest,
                 user_id=None, project_id=None):
    if (FLAGS.image_conversion_dir and not
            os.path.exists(FLAGS.image_conversion_dir)):
        os.makedirs(FLAGS.image_conversion_dir)

    # NOTE(avishay): I'm not crazy about creating temp files which may be
    # large and cause disk full errors which would confuse users.
    # Unfortunately it seems that you can't pipe to 'qemu-img convert' because
    # it seeks. Maybe we can think of something for a future version.
    fd, tmp = tempfile.mkstemp(dir=FLAGS.image_conversion_dir)
    os.close(fd)
    with utils.remove_path_on_error(tmp):
        fetch(context, image_service, image_id, tmp, user_id, project_id)

        data = qemu_img_info(tmp)
        fmt = data.file_format
        if fmt is None:
            raise exception.ImageUnacceptable(
                reason=_("'qemu-img info' parsing failed."),
                image_id=image_id)

        backing_file = data.backing_file
        if backing_file is not None:
            raise exception.ImageUnacceptable(
                image_id=image_id,
                reason=_("fmt=%(fmt)s backed by:"
                         "%(backing_file)s") % locals())

        # NOTE(jdg): I'm using qemu-img convert to write
        # to the volume regardless if it *needs* conversion or not
        # TODO(avishay): We can speed this up by checking if the image is raw
        # and if so, writing directly to the device. However, we need to keep
        # check via 'qemu-img info' that what we copied was in fact a raw
        # image and not a different format with a backing file, which may be
        # malicious.
        LOG.debug("%s was %s, converting to raw" % (image_id, fmt))
        convert_image(tmp, dest, 'raw')

        data = qemu_img_info(dest)
        if data.file_format != "raw":
            raise exception.ImageUnacceptable(
                image_id=image_id,
                reason=_("Converted to raw, but format is now %s") %
                data.file_format)
        os.unlink(tmp)
예제 #7
0
def fetch_to_raw(context, image_service,
                 image_id, dest,
                 user_id=None, project_id=None):
    if (FLAGS.image_conversion_dir and not
            os.path.exists(FLAGS.image_conversion_dir)):
        os.makedirs(FLAGS.image_conversion_dir)

    # NOTE(avishay): I'm not crazy about creating temp files which may be
    # large and cause disk full errors which would confuse users.
    # Unfortunately it seems that you can't pipe to 'qemu-img convert' because
    # it seeks. Maybe we can think of something for a future version.
    fd, tmp = tempfile.mkstemp(dir=FLAGS.image_conversion_dir)
    os.close(fd)
    with utils.remove_path_on_error(tmp):
        fetch(context, image_service, image_id, tmp, user_id, project_id)

        data = qemu_img_info(tmp)
        fmt = data.file_format
        if fmt is None:
            raise exception.ImageUnacceptable(
                reason=_("'qemu-img info' parsing failed."),
                image_id=image_id)

        backing_file = data.backing_file
        if backing_file is not None:
            raise exception.ImageUnacceptable(
                image_id=image_id,
                reason=_("fmt=%(fmt)s backed by:"
                         "%(backing_file)s") % locals())

        # NOTE(jdg): I'm using qemu-img convert to write
        # to the volume regardless if it *needs* conversion or not
        # TODO(avishay): We can speed this up by checking if the image is raw
        # and if so, writing directly to the device. However, we need to keep
        # check via 'qemu-img info' that what we copied was in fact a raw
        # image and not a different format with a backing file, which may be
        # malicious.
        LOG.debug("%s was %s, converting to raw" % (image_id, fmt))
        convert_image(tmp, dest, 'raw')

        data = qemu_img_info(dest)
        if data.file_format != "raw":
            raise exception.ImageUnacceptable(
                image_id=image_id,
                reason=_("Converted to raw, but format is now %s") %
                data.file_format)
        os.unlink(tmp)
예제 #8
0
def fetch_to_raw(context, image_service,
                 image_id, dest,
                 user_id=None, project_id=None):
    if (FLAGS.image_conversion_dir and not
            os.path.exists(FLAGS.image_conversion_dir)):
        os.makedirs(FLAGS.image_conversion_dir)

    fd, tmp = tempfile.mkstemp(dir=FLAGS.image_conversion_dir)
    os.close(fd)
    with utils.remove_path_on_error(tmp):
        fetch(context, image_service, image_id, tmp, user_id, project_id)

        data = qemu_img_info(tmp)
        fmt = data.file_format
        if fmt is None:
            raise exception.ImageUnacceptable(
                reason=_("'qemu-img info' parsing failed."),
                image_id=image_id)

        backing_file = data.backing_file
        if backing_file is not None:
            raise exception.ImageUnacceptable(
                image_id=image_id,
                reason=_("fmt=%(fmt)s backed by:"
                         "%(backing_file)s") % locals())

        # NOTE(jdg): I'm using qemu-img convert to write
        # to the volume regardless if it *needs* conversion or not
        LOG.debug("%s was %s, converting to raw" % (image_id, fmt))
        convert_image(tmp, dest, 'raw')

        data = qemu_img_info(dest)
        if data.file_format != "raw":
            raise exception.ImageUnacceptable(
                image_id=image_id,
                reason=_("Converted to raw, but format is now %s") %
                data.file_format)
        os.unlink(tmp)