~ubuntu-branches/ubuntu/quantal/nova/quantal-proposed

« back to all changes in this revision

Viewing changes to nova/virt/images.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-08-16 14:04:11 UTC
  • mto: This revision was merged to the branch mainline in revision 84.
  • Revision ID: package-import@ubuntu.com-20120816140411-0mr4n241wmk30t9l
Tags: upstream-2012.2~f3
ImportĀ upstreamĀ versionĀ 2012.2~f3

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
FLAGS.register_opts(image_opts)
44
44
 
45
45
 
 
46
def qemu_img_info(path):
 
47
    """Return a dict containing the parsed output from qemu-img info."""
 
48
 
 
49
    out, err = utils.execute('env', 'LC_ALL=C', 'LANG=C',
 
50
                             'qemu-img', 'info', path)
 
51
 
 
52
    # output of qemu-img is 'field: value'
 
53
    data = {}
 
54
    for line in out.splitlines():
 
55
        field, val = line.split(':', 1)
 
56
        if val[0] == " ":
 
57
            val = val[1:]
 
58
        data[field] = val
 
59
 
 
60
    return data
 
61
 
 
62
 
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
57
74
 
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)
61
 
 
62
 
    def _qemu_img_info(path):
63
 
 
64
 
        out, err = utils.execute('env', 'LC_ALL=C', 'LANG=C',
65
 
            'qemu-img', 'info', path)
66
 
 
67
 
        # output of qemu-img is 'field: value'
68
 
        # the fields of interest are 'file format' and 'backing file'
69
 
        data = {}
70
 
        for line in out.splitlines():
71
 
            (field, val) = line.split(':', 1)
72
 
            if val[0] == " ":
73
 
                val = val[1:]
74
 
            data[field] = val
75
 
 
76
 
        return(data)
 
77
    fetch(context, image_href, path_tmp, user_id, project_id)
77
78
 
78
79
    with utils.remove_path_on_error(path_tmp):
79
 
        data = _qemu_img_info(path_tmp)
 
80
        data = qemu_img_info(path_tmp)
80
81
 
81
 
        fmt = data.get("file format")
 
82
        fmt = data.get('file format')
82
83
        if fmt is None:
83
84
            raise exception.ImageUnacceptable(
84
85
                reason=_("'qemu-img info' parsing failed."),
85
86
                image_id=image_href)
86
87
 
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())
91
92
 
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
 
                                         path_tmp, staged)
 
97
                utils.execute('qemu-img', 'convert', '-O', 'raw', path_tmp,
 
98
                              staged)
98
99
 
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'))
104
105
 
105
106
                os.rename(staged, path)
106
107
 
107
108
        else:
108
109
            os.rename(path_tmp, path)
109
 
 
110
 
    return metadata