Revision 287c1028 snf-image-helper/disklabel.py

b/snf-image-helper/disklabel.py
230 230
        return self.disklabel.get_last_partition_id()
231 231

  
232 232
    def get_duid(self):
233
        return self.disklabel.field['uid']
233
        """Get the Disk UID (work only on OpenBSD)"""
234
        if 'uid' in self.disklabel.field:
235
            return self.disklabel.field['uid']
236

  
237
        return ""
234 238

  
235 239

  
236 240
class DisklabelBase(object):
......
352 356
            'Partition', 'size, offset, fsize, fstype, frag, cpg')
353 357

  
354 358
    format = "<IHH16s16sIIIIIIHHIHHHHIII20s20sIHHII364s"
355
    """
356
    Offset  Length          Contents
357
    0       4               Magic
358
    4       2               Drive Type
359
    6       2               Subtype
360
    8       16              Type Name
361
    24      16              Pack Identifier
362
    32      4               Bytes per sector
363
    36      4               Data sectors per track
364
    40      4               Tracks per cylinder
365
    44      4               Data cylinders per unit
366
    48      4               Data sectors per cylinder
367
    52      4               Data sectors per unit
368
    56      2               Spare sectors per track
369
    58      2               Spare sectors per cylinder
370
    60      4               Alternative cylinders per unit
371
    64      2               Rotation Speed
372
    66      2               Hardware sector interleave
373
    68      2               Sector 0 skew, per track
374
    70      2               Sector 0 skew, per cylinder
375
    72      4               Head switch time
376
    76      4               Track-to-track seek
377
    80      4               Generic Flags
378
    84      5*4             Drive-type specific information
379
    104     5*4             Reserved for future use
380
    124     4               Magic Number
381
    128     2               Xor of data including partitions
382
    130     2               Number of partitions following
383
    132     4               size of boot area at sn0, bytes
384
    136     4               Max size of fs superblock, bytes
385
    140     16*16           Partition Table
386
    """
359

  
360
    def __init__(self, device):
361
        """Create a BSD DiskLabel instance"""
362

  
363
        device.seek(BLOCKSIZE, os.SEEK_CUR)
364
        # The offset of the disklabel from the beginning of the partition is
365
        # one sector
366
        sector1 = device.read(BLOCKSIZE)
367

  
368
        d_ = OrderedDict()      # Off  Len    Content
369
        (d_["magic"],           # 0    4      Magic
370
         d_["dtype"],           # 4    2      Drive Type
371
         d_["subtype"],         # 6    2      Subtype
372
         d_["typename"],        # 8    16     Type Name
373
         d_["packname"],        # 24   16     Pack Identifier
374
         d_["secsize"],         # 32   4      Bytes per sector
375
         d_["nsectors"],        # 36   4      Data sectors per track
376
         d_["ntracks"],         # 40   4      Tracks per cylinder
377
         d_["ncylinders"],      # 44   4      Data cylinders per unit
378
         d_["secpercyl"],       # 48   4      Data sectors per cylinder
379
         d_["secperunit"],      # 52   4      Data sectors per unit
380
         d_["sparespertrack"],  # 56   2      Spare sectors per track
381
         d_["sparespercyl"],    # 58   2      Spare sectors per cylinder
382
         d_["acylinders"],      # 60   4      Alternative cylinders per unit
383
         d_["rpm"],             # 64   2      Rotation Speed
384
         d_["interleave"],      # 66   2      Hardware sector interleave
385
         d_["trackskew"],       # 68   2      Sector 0 skew, per track
386
         d_["cylskew"],         # 70   2      Sector 0 skew, per cylinder
387
         d_["headswitch"],      # 72   4      Head switch time
388
         d_["trkseek"],         # 76   4      Track-to-track seek
389
         d_["flags"],           # 80   4      Generic Flags
390
         d_["drivedata"],       # 84   5*4    Drive-type specific information
391
         d_["spare"],           # 104  5*4    Reserved for future use
392
         d_["magic2"],          # 124  4      Magic Number
393
         d_["checksum"],        # 128  2      Xor of data including partitions
394
         d_["npartitions"],     # 130  2      Number of partitions following
395
         d_["bbsize"],          # 132  4      size of boot area at sn0, bytes
396
         d_["sbsize"],          # 136  4      Max size of fs superblock, bytes
397
         ptable_raw             # 140  16*16  Partition Table
398
         ) = struct.unpack(self.format, sector1)
399

  
400
        assert d_['magic'] == d_['magic2'] == DISKMAGIC, "Disklabel not valid"
401
        self.ptable = self.PartitionTable(ptable_raw, d_['npartitions'])
402
        self.field = d_
403

  
404
    def __str__(self):
405
        """Print the Disklabel"""
406

  
407
        title = "Disklabel"
408
        typename = self.field['typename'].strip('\x00').strip()
409
        packname = self.field['packname'].strip('\x00').strip()
410

  
411
        return \
412
            "%s\n%s\n" % (title, len(title) * "=") + \
413
            "Magic Number: 0x%(magic)x\n" \
414
            "Drive type: %(dtype)d\n" \
415
            "Subtype: %(subtype)d\n" % self.field + \
416
            "Typename: %s\n" % typename + \
417
            "Pack Identifier: %s\n" % packname + \
418
            "# of bytes per sector: %(secsize)d\n" \
419
            "# of data sectors per track: %(nsectors)d\n" \
420
            "# of tracks per cylinder: %(ntracks)d\n" \
421
            "# of data cylinders per unit: %(ncylinders)d\n" \
422
            "# of data sectors per cylinder: %(secpercyl)d\n" \
423
            "# of data sectors per unit: %(secperunit)d\n" \
424
            "# of spare sectors per track: %(sparespertrack)d\n" \
425
            "# of spare sectors per cylinder: %(sparespercyl)d\n" \
426
            "Alt. cylinders per unit: %(acylinders)d\n" \
427
            "Rotational speed: %(rpm)d\n" \
428
            "Hardware sector interleave: %(interleave)d\n" \
429
            "Sector 0 skew, per track: %(trackskew)d\n" \
430
            "Sector 0 skew, per cylinder: %(cylskew)d\n" \
431
            "Head switch time, usec: %(headswitch)d\n" \
432
            "Track-to-track seek, usec: %(trkseek)d\n" \
433
            "Generic Flags: %(flags)r\n" \
434
            "Drive data: %(drivedata)r\n" \
435
            "Reserved for future use: %(spare)r\n" \
436
            "The magic number again: 0x%(magic2)x\n" \
437
            "Checksum: %(checksum)d\n" \
438
            "Number of partitions: %(npartitions)d\n"  \
439
            "Size of boot aread at sn0: %(bbsize)d\n"  \
440
            "Max size of fs superblock: %(sbsize)d\n" % self.field + \
441
            "%s" % self.ptable
387 442

  
388 443

  
389 444
class OpenBSD_Disklabel(DisklabelBase):
......
392 447
    class PartitionTable(PartitionTableBase):
393 448
        """Reprepsents an OpenBSD Partition Table"""
394 449
        format = "<IIHHBBH"
395

  
396 450
        """
397 451
        Partition Entry:
398 452
        Offset  Length          Contents
......
404 458
        13      1               Filesystem Fragment per block
405 459
        14      2               FS cylinders per group
406 460
        """
407

  
408 461
        Partition = namedtuple(
409 462
            'Partition', 'size, offset, offseth, sizeh, fstype, frag, cpg')
410 463

  
......
525 578
        self.ptable.setpsize(2, new_size)
526 579

  
527 580
        # Update the checksum
528
        self.checksum = self.compute_checksum()
581
        self.field['checksum'] = self.compute_checksum()
529 582

  
530 583
        # bend is the size and not the end of the usable region. I named it
531 584
        # like this because this is how it is named in OpenBSD. To get the last

Also available in: Unified diff