Revision 63656985

b/snf-image-helper/disklabel.py
258 258
        self.disklabel.enlarge_last_partition()
259 259

  
260 260

  
261
class BSD_Disklabel(object):
261
class DisklabelBase(object):
262
    """Disklabel base class"""
263

  
264
    def __init__(self, device):
265
        """Create a Disklabel instance"""
266
        raise NotImplementedError
267

  
268
    def pack(self, checksum=None):
269
        """Return a binary copy of the Disklabel block"""
270
        raise NotImplementedError
271

  
272
    def compute_checksum(self):
273
        """Compute the checksum of the disklabel"""
274

  
275
        raw = cStringIO.StringIO(self.pack(0))
276
        checksum = 0
277
        try:
278
            uint16 = raw.read(2)
279
            while uint16 != "":
280
                checksum ^= struct.unpack('<H', uint16)[0]
281
                uint16 = raw.read(2)
282
        finally:
283
            raw.close()
284

  
285
        return checksum
286

  
287
    def enlarge(self, new_size):
288
        """Enlarge the disk and return the last usable sector"""
289
        raise NotImplementedError
290

  
291
    def write_to(self, device):
292
        """Write the disklabel to a device"""
293
        raise NotImplementedError
294

  
295
    def enlarge_last_partition(self):
296
        """Enlarge the last partition to consume all the usable space"""
297
        raise NotImplementedError
298

  
299
    def __str__(self):
300
        """Print the Disklabel"""
301
        raise NotImplementedError
302

  
303

  
304
class BSD_Disklabel(DisklabelBase):
262 305
    """Represents an BSD Disklabel"""
263 306

  
264 307
    class PartitionTable(PartitionTableBase):
......
313 356
    """
314 357

  
315 358

  
316
class OpenBSD_Disklabel(object):
359
class OpenBSD_Disklabel(DisklabelBase):
317 360
    """Represents an OpenBSD Disklabel"""
318 361

  
319 362
    class PartitionTable(PartitionTableBase):
......
461 504
                           self.ptable.pack() +
462 505
                           ((364 - self.npartitions * 16) * '\x00'))
463 506

  
464
    def compute_checksum(self):
465
        """Compute the checksum of the disklabel"""
466

  
467
        raw = cStringIO.StringIO(self.pack(0))
468
        checksum = 0
469
        try:
470
            uint16 = raw.read(2)
471
            while uint16 != "":
472
                checksum ^= struct.unpack('<H', uint16)[0]
473
                uint16 = raw.read(2)
474
        finally:
475
            raw.close()
476

  
477
        return checksum
478

  
479 507
    def setdsize(self, dsize):
480 508
        """Set disk size"""
481 509
        self.secperunith = dsize >> 32
......
504 532
        return (self.bendh << 32) + self.bend
505 533

  
506 534
    def enlarge(self, new_size):
507
        """Enlarge the size of the disk and return the last usable sector"""
535
        """Enlarge the disk and return the last usable sector"""
508 536

  
509 537
        assert new_size >= self.getdsize(), \
510 538
            "New size cannot be smaller that %s" % self.getdsize()

Also available in: Unified diff