From 3b2674b32c43497af679f2ab5c1bb946b80309bc Mon Sep 17 00:00:00 2001 From: Nikos Skalkotos Date: Tue, 4 Mar 2014 09:03:07 +0200 Subject: [PATCH] disklabel.py code cleanup --- snf-image-helper/disklabel.py | 165 +++++++++++++++++++++-------------------- 1 file changed, 86 insertions(+), 79 deletions(-) diff --git a/snf-image-helper/disklabel.py b/snf-image-helper/disklabel.py index 75d4b5c..fda1964 100755 --- a/snf-image-helper/disklabel.py +++ b/snf-image-helper/disklabel.py @@ -112,36 +112,28 @@ class MBR(object): return struct.pack('> 32 self.field['bend'] = bend & 0xffffffff def getbend(self): - """Get size of usable region""" + """Get end of usable region""" return (self.field['bendh'] << 32) + self.field['bend'] - bend = property(getbend, setbend, None, "usable region size") + bend = property(getbend, setbend, None, "end of usable region") def enlarge(self, new_size): """Enlarge the disk and return the last usable sector""" @@ -580,9 +583,7 @@ class OpenBSD_Disklabel(DisklabelBase): # Update the checksum self.field['checksum'] = self.compute_checksum() - # bend is the size and not the end of the usable region. I named it - # like this because this is how it is named in OpenBSD. To get the last - # usable sector you need to reduce this value by one. + # The last usable sector is the end of the usable region minus one return self.bend - 1 def get_last_partition_id(self): @@ -624,10 +625,13 @@ class OpenBSD_Disklabel(DisklabelBase): def __str__(self): """Print the Disklabel""" - title = "Disklabel" + # Those values may contain null bytes typename = self.field['typename'].strip('\x00').strip() packname = self.field['packname'].strip('\x00').strip() + duid = "".join(x.encode('hex') for x in self.field['uid']) + + title = "Disklabel" return \ "%s\n%s\n" % (title, len(title) * "=") + \ "Magic Number: 0x%(magic)x\n" \ @@ -660,8 +664,8 @@ class OpenBSD_Disklabel(DisklabelBase): "%s" % self.ptable -if __name__ == '__main__': - +def main(): + """Main entry point""" usage = "Usage: %prog [options] " parser = optparse.OptionParser(usage=usage) @@ -671,9 +675,9 @@ if __name__ == '__main__': parser.add_option("--get-last-partition", action="store_true", dest="last_part", default=False, help="print the label of the last partition") - parser.add_option("--get-duid", action="store_true", dest="duid", - default=False, - help="print the disklabel unique identifier") + parser.add_option( + "--get-duid", action="store_true", dest="duid", default=False, + help="print the Disklabel Unique Identifier (OpenBSD only)") parser.add_option("-d", "--enlarge-disk", type="int", dest="disk_size", default=None, metavar="SIZE", help="Enlarge the disk to this SIZE (in sectors)") @@ -691,11 +695,11 @@ if __name__ == '__main__': if options.list: print disk - sys.exit(0) + return 0 if options.duid: print "%s" % "".join(x.encode('hex') for x in disk.get_duid()) - sys.exit(0) + return 0 if options.last_part: print "%c" % chr(ord('a') + disk.get_last_partition_id()) @@ -707,7 +711,10 @@ if __name__ == '__main__': disk.enlarge_last_partition() disk.write() + return 0 + -sys.exit(0) +if __name__ == '__main__': + sys.exit(main()) # vim: set sta sts=4 shiftwidth=4 sw=4 et ai : -- 1.7.10.4