43
43
FLAGS.register_opts(image_opts)
46
def qemu_img_info(path):
47
"""Return a dict containing the parsed output from qemu-img info."""
49
out, err = utils.execute('env', 'LC_ALL=C', 'LANG=C',
50
'qemu-img', 'info', path)
52
# output of qemu-img is 'field: value'
54
for line in out.splitlines():
55
field, val = line.split(':', 1)
46
63
def fetch(context, image_href, path, _user_id, _project_id):
47
64
# TODO(vish): Improve context handling and add owner and auth data
48
65
# when it is added to glance. Right now there is no
58
75
def fetch_to_raw(context, image_href, path, user_id, project_id):
59
76
path_tmp = "%s.part" % path
60
metadata = fetch(context, image_href, path_tmp, user_id, project_id)
62
def _qemu_img_info(path):
64
out, err = utils.execute('env', 'LC_ALL=C', 'LANG=C',
65
'qemu-img', 'info', path)
67
# output of qemu-img is 'field: value'
68
# the fields of interest are 'file format' and 'backing file'
70
for line in out.splitlines():
71
(field, val) = line.split(':', 1)
77
fetch(context, image_href, path_tmp, user_id, project_id)
78
79
with utils.remove_path_on_error(path_tmp):
79
data = _qemu_img_info(path_tmp)
80
data = qemu_img_info(path_tmp)
81
fmt = data.get("file format")
82
fmt = data.get('file format')
83
84
raise exception.ImageUnacceptable(
84
85
reason=_("'qemu-img info' parsing failed."),
85
86
image_id=image_href)
87
if "backing file" in data:
88
backing_file = data['backing file']
88
backing_file = data.get('backing file')
89
if backing_file is not None:
89
90
raise exception.ImageUnacceptable(image_id=image_href,
90
91
reason=_("fmt=%(fmt)s backed by: %(backing_file)s") % locals())
93
94
staged = "%s.converted" % path
94
95
LOG.debug("%s was %s, converting to raw" % (image_href, fmt))
95
96
with utils.remove_path_on_error(staged):
96
out, err = utils.execute('qemu-img', 'convert', '-O', 'raw',
97
utils.execute('qemu-img', 'convert', '-O', 'raw', path_tmp,
99
data = _qemu_img_info(staged)
100
if data.get('file format', None) != "raw":
100
data = qemu_img_info(staged)
101
if data.get('file format') != "raw":
101
102
raise exception.ImageUnacceptable(image_id=image_href,
102
103
reason=_("Converted to raw, but format is now %s") %
103
data.get('file format', None))
104
data.get('file format'))
105
106
os.rename(staged, path)
108
109
os.rename(path_tmp, path)