~ubuntu-branches/ubuntu/quantal/virtinst/quantal-proposed

« back to all changes in this revision

Viewing changes to virtinst/ImageParser.py

  • Committer: Bazaar Package Importer
  • Author(s): Marc Deslauriers
  • Date: 2011-02-01 15:40:11 UTC
  • mfrom: (1.3.16 experimental)
  • Revision ID: james.westby@ubuntu.com-20110201154011-op0nusgc240xajvb
Tags: 0.500.5-1ubuntu1
* Merge from debian experimental. Remaining changes:
  - debian/patches/9001_Ubuntu.patch:
     + Updated to add maverick and natty to OS list and enable virtio
       for them.
  - debian/patches/9003-fix-path-to-hvmloader-in-testsuite.patch: adjust
    testsuite for 0001-fix-path-to-hvmloader.patch and
    0002-Fix-path-to-pygrub.patch. (refreshed)
  - debian/control: added acl package to depends.
  - Demote virt-viewer to Suggests, as it's in universe.
  - Recommends libvirt-bin
* Removed patches:
  - debian/patches/9002-libvirt_disk_format.patch: Upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
 
33
33
class Image:
34
34
    """The toplevel object representing a VM image"""
35
 
    def __init__(self, node = None, base = None, filename = None):
 
35
    def __init__(self, node=None, base=None, filename=None):
36
36
        self.storage = {}
37
37
        self.domain = None
38
38
        if filename == None:
43
43
            if filename is not None:
44
44
                self.base = os.path.dirname(filename)
45
45
                if self.base == '' :
46
 
                    self.base ="."
 
46
                    self.base = "."
47
47
            else:
48
 
                self.base ="."
 
48
                self.base = "."
49
49
        else:
50
50
            self.base = base
51
51
        self.name = None
72
72
            if disk.file is None:
73
73
                disk.id = "disk%d.img" % len(self.storage)
74
74
                disk.file = "disk%d.img" % (len(self.storage) + 1)
75
 
            if self.storage.has_key(disk.id):
 
75
            if disk.id in self.storage:
76
76
                raise ParserException("Disk file '%s' defined twice"
77
77
                                           % disk.file)
78
78
            self.storage[disk.id] = disk
84
84
        # Connect the disk maps to the disk definitions
85
85
        for boot in self.domain.boots:
86
86
            for d in boot.drives:
87
 
                if not self.storage.has_key(d.disk_id):
 
87
                if d.disk_id not in self.storage:
88
88
                    raise ParserException(_("Disk entry for '%s' not found")
89
89
                                               % d.disk_id)
90
90
                d.disk = self.storage[d.disk_id]
91
91
 
92
92
class Domain:
93
93
    """The description of a virtual domain as part of an image"""
94
 
    def __init__(self, node = None):
 
94
    def __init__(self, node=None):
95
95
        self.boots = []
96
96
        self.vcpu = None
97
97
        self.memory = None
104
104
        self.boots = [ Boot(b) for b in node.xpathEval("boot") ]
105
105
        self.vcpu = xpathString(node, "devices/vcpu", 1)
106
106
        tmpmem = xpathString(node, "devices/memory")
107
 
        self.interface = int(node.xpathEval("count(devices/interface)")) 
 
107
        self.interface = int(node.xpathEval("count(devices/interface)"))
108
108
        self.graphics = node.xpathEval("count(devices/graphics)") > 0
109
109
 
110
110
        # FIXME: There must be a better way to check this
118
118
            tmpmem = 0
119
119
 
120
120
class ImageFeatures(CapabilitiesParser.Features):
121
 
    def __init__(self, node = None):
 
121
    def __init__(self, node=None):
122
122
        CapabilitiesParser.Features.__init__(self, node)
123
123
 
124
124
    def _extractFeature(self, feature, d, n):
134
134
class Boot:
135
135
    """The overall description of how the image can be booted, including
136
136
    required capabilities of the host and mapping of disks into the VM"""
137
 
    def __init__(self, node = None):
 
137
    def __init__(self, node=None):
138
138
        # 'xen' or 'hvm'
139
139
        self.type = None
140
140
        # Either 'pygrub' or nothing; might have others in the future
184
184
class Drive:
185
185
    """The mapping of a disk from the storage section to a virtual drive
186
186
    in a guest"""
187
 
    def __init__(self, node = None):
 
187
    def __init__(self, node=None):
188
188
        self.disk_id = None
189
189
        self.target = None
190
190
        self.disk = None   # Will point to the underlying Disk object
206
206
    USE_USER = "user"
207
207
    USE_SCRATCH = "scratch"
208
208
 
209
 
    def __init__(self, node = None):
 
209
    def __init__(self, node=None):
210
210
        self.id = None
211
211
        self.file = None
212
212
        self.format = None
228
228
            csumvalue = xpathString(d, "")
229
229
            self.csum[csumtype] = csumvalue
230
230
        formats = [Disk.FORMAT_RAW, Disk.FORMAT_QCOW, Disk.FORMAT_QCOW2, Disk.FORMAT_VMDK, Disk.FORMAT_ISO]
231
 
        validate (formats.count(self.format) > 0,
232
 
                  _("The format for disk %s must be one of %s") %
233
 
                  (self.file, ",".join(formats)))
 
231
        validate(formats.count(self.format) > 0,
 
232
                 _("The format for disk %s must be one of %s") %
 
233
                 (self.file, ",".join(formats)))
234
234
 
235
235
    def check_disk_signature(self, meter=None):
236
236
        try:
237
237
            import hashlib
238
 
            has_hashlib = True
 
238
            sha = None
239
239
        except:
240
240
            import sha
241
 
            has_hashlib = False
 
241
            hashlib = None
242
242
 
243
243
        m = None
244
 
        if has_hashlib is True:
245
 
            if self.csum.has_key("sha256"):
 
244
        if hashlib:
 
245
            if "sha256" in self.csum:
246
246
                csumvalue = self.csum["sha256"]
247
247
                m = hashlib.sha256()
248
 
            elif self.csum.has_key("sha1"):
 
248
 
 
249
            elif "sha1" in self.csum:
249
250
                csumvalue = self.csum["sha1"]
250
251
                m = hashlib.sha1()
251
252
        else:
252
 
            if self.csum.has_key("sha1"):
 
253
            if "sha1" in self.csum:
253
254
                csumvalue = self.csum["sha1"]
254
255
                m = sha.new()
255
256
 
262
263
            meter.start(size=disk_size,
263
264
                        text=_("Checking disk signature for %s" % self.file))
264
265
 
265
 
        f = open(self.file,"r")
 
266
        f = file(self.file)
266
267
        while 1:
267
268
            chunk = f.read(65536)
268
269
            if not chunk:
275
276
        if checksum != csumvalue:
276
277
            logging.debug(_("Disk signature for %s does not match "
277
278
                            "Expected: %s  Received: %s" % (self.file,
278
 
                             csumvalue,checksum)))
279
 
            raise ValueError (_("Disk signature for %s does not "
280
 
                                "match" % self.file))
 
279
                             csumvalue, checksum)))
 
280
            raise ValueError(_("Disk signature for %s does not "
 
281
                               "match" % self.file))
281
282
 
282
283
def validate(cond, msg):
283
284
    if not cond:
284
285
        raise ParserException(msg)
285
286
 
286
 
def xpathString(node, path, default = None):
 
287
def xpathString(node, path, default=None):
287
288
    result = node.xpathEval("string(%s)" % path)
288
289
    if len(result) == 0:
289
290
        result = default
317
318
        if root.name != "image":
318
319
            raise ParserException(_("Root element is not 'image'"))
319
320
 
320
 
        image = Image(root, filename = filename)
 
321
        image = Image(root, filename=filename)
321
322
    finally:
322
323
        doc.freeDoc()
323
324
 
327
328
    f = open(filename, "r")
328
329
    xml = f.read()
329
330
    f.close()
330
 
    return parse(xml, filename = filename)
 
331
    return parse(xml, filename=filename)