~cerberus/nova/disk_config

« back to all changes in this revision

Viewing changes to nova/virt/images.py

  • Committer: matt.dietz at rackspace
  • Date: 2011-09-21 19:48:25 UTC
  • mfrom: (1511.1.98 nova)
  • Revision ID: matt.dietz@rackspace.com-20110921194825-zv1w4qonfh6o1j2u
Merge from trunk, updated failing tests and pep8

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
Handling of VM disk images.
22
22
"""
23
23
 
 
24
import os
 
25
 
 
26
from nova import exception
24
27
from nova import flags
25
28
from nova.image import glance as glance_image_service
26
29
import nova.image
42
45
    with open(path, "wb") as image_file:
43
46
        metadata = image_service.get(context, image_id, image_file)
44
47
    return metadata
 
48
 
 
49
 
 
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)
 
53
 
 
54
    def _qemu_img_info(path):
 
55
 
 
56
        out, err = utils.execute('env', 'LC_ALL=C', 'LANG=C',
 
57
            'qemu-img', 'info', path)
 
58
 
 
59
        # output of qemu-img is 'field: value'
 
60
        # the fields of interest are 'file format' and 'backing file'
 
61
        data = {}
 
62
        for line in out.splitlines():
 
63
            (field, val) = line.split(':', 1)
 
64
            if val[0] == " ":
 
65
                val = val[1:]
 
66
            data[field] = val
 
67
 
 
68
        return(data)
 
69
 
 
70
    data = _qemu_img_info(path_tmp)
 
71
 
 
72
    fmt = data.get("file format", None)
 
73
    if fmt == None:
 
74
        os.unlink(path_tmp)
 
75
        raise exception.ImageUnacceptable(
 
76
            reason=_("'qemu-img info' parsing failed."), image_id=image_href)
 
77
 
 
78
    if fmt != "raw":
 
79
        staged = "%s.converted" % path
 
80
        if "backing file" in data:
 
81
            backing_file = data['backing file']
 
82
            os.unlink(path_tmp)
 
83
            raise exception.ImageUnacceptable(image_id=image_href,
 
84
                reason=_("fmt=%(fmt)s backed by: %(backing_file)s") % locals())
 
85
 
 
86
        LOG.debug("%s was %s, converting to raw" % (image_href, fmt))
 
87
        out, err = utils.execute('qemu-img', 'convert', '-O', 'raw',
 
88
                                 path_tmp, staged)
 
89
        os.unlink(path_tmp)
 
90
 
 
91
        data = _qemu_img_info(staged)
 
92
        if data.get('file format', None) != "raw":
 
93
            os.unlink(staged)
 
94
            raise exception.ImageUnacceptable(image_id=image_href,
 
95
                reason=_("Converted to raw, but format is now %s") %
 
96
                data.get('file format', None))
 
97
 
 
98
        os.rename(staged, path)
 
99
 
 
100
    else:
 
101
        os.rename(path_tmp, path)
 
102
 
 
103
    return metadata