42
45
with open(path, "wb") as image_file:
43
46
metadata = image_service.get(context, image_id, image_file)
50
def fetch_to_raw(context, image_href, path, user_id, project_id):
51
path_tmp = "%s.part" % path
52
metadata = fetch(context, image_href, path_tmp, user_id, project_id)
54
def _qemu_img_info(path):
56
out, err = utils.execute('env', 'LC_ALL=C', 'LANG=C',
57
'qemu-img', 'info', path)
59
# output of qemu-img is 'field: value'
60
# the fields of interest are 'file format' and 'backing file'
62
for line in out.splitlines():
63
(field, val) = line.split(':', 1)
70
data = _qemu_img_info(path_tmp)
72
fmt = data.get("file format", None)
75
raise exception.ImageUnacceptable(
76
reason=_("'qemu-img info' parsing failed."), image_id=image_href)
79
staged = "%s.converted" % path
80
if "backing file" in data:
81
backing_file = data['backing file']
83
raise exception.ImageUnacceptable(image_id=image_href,
84
reason=_("fmt=%(fmt)s backed by: %(backing_file)s") % locals())
86
LOG.debug("%s was %s, converting to raw" % (image_href, fmt))
87
out, err = utils.execute('qemu-img', 'convert', '-O', 'raw',
91
data = _qemu_img_info(staged)
92
if data.get('file format', None) != "raw":
94
raise exception.ImageUnacceptable(image_id=image_href,
95
reason=_("Converted to raw, but format is now %s") %
96
data.get('file format', None))
98
os.rename(staged, path)
101
os.rename(path_tmp, path)