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)
def fetch_to_raw(context, image_service, image_id, dest, user_id=None, project_id=None): if CONF.image_conversion_dir and not os.path.exists(CONF.image_conversion_dir): os.makedirs(CONF.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=CONF.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)