Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Linux partitioning tools
sec = 512
k = 1024
m = k * k
g = m * k
bs = 4 * m /sec
# Generate a partition table for consumption by sfdisk
# (partition plan is hardcoded below)
partplan = (
(32, 83), # home
(8, 83), # var
(8, 83), # tmp
(8, 83), # swap
(0.5, 83), # boot
(8, 83), # root
(32, 83), # usr
)
def align(pos, increment=False):
pos = int(pos)
if pos % bs or increment:
return bs * (1 + pos/bs)
else:
return pos
def parttable(partitions):
curr_sec = bs
table = []
for gb_size, type_code in partitions:
sec_size = int(gb_size * g / sec)
curr_sec = align(curr_sec)
table.append([curr_sec, sec_size, type_code])
curr_sec += sec_size
if len(table) == 3:
curr_sec = align(curr_sec, True)
table.append([curr_sec, '', 5])
curr_sec = align(curr_sec, True)
return table
def printtable(parttab):
print '# partition table of /dev/sda\nunit: sectors\n'
for i, (a, b, t) in enumerate(parttab):
print '/dev/sda%d : start=%10s, size=%10s, Id=%2s' % (i + 1, a, b, t)
printtable(parttable(partplan))
import sys
import re
# Generate legible partition listing from an sfdisk -d partition table dump
sector = 512.
k = 2 * sector
m = k * k
g = k * m
sfdisk_re = re.compile(r'^(/dev/)?(?P<part>[/\w]+) *: start= *(?P<start>\d+), size= *(?P<size>\d+), Id= *(?P<type>[0-9a-f]{1,2})')
units = {
'b': 1,
's': sector,
'k': k,
'm': m,
'g': g,
}
class PartitionTable(list):
units = 'm'
total_bytes = 0
def __init__(self, input):
for line in input:
m = sfdisk_re.match(line.strip())
if m:
self.add(m.groupdict())
def add(self, partition):
p = dict(partition)
if '0' == p['start'] == p['size'] == p['type']:
return
p['start'] = int(p['start']) * sector
p['size'] = int(p['size']) * sector
p['end'] = p['start']
if p['type'] != ' 5':
p['end'] += p['size']
self.total_bytes += p['size']
self.append(p)
def start(self, i):
return (self[i]['start']) / units[self.units]
def end(self, i):
return self[i]['end'] / units[self.units]
def size(self, i):
return (self[i]['size']) / units[self.units]
def sizes(self):
end = 0
for partition in range(len(self)):
start = self.start(partition)
size = self.size(partition)
gap = start - end
end = self.end(partition)
if gap:
yield (gap, 'gap')
yield (size, self[partition]['type'])
def total(self):
return self.total_bytes / units[self.units]
def alignment(self, i):
start = self[i]['start']
for unit in 'gmk':
s = units[unit]
if start % s:
continue
for a in (32, 16, 8, 4, 2, 1):
if not (start % (a * s)):
break
return a, unit
class ToGB(object):
total_bytes = 0
def repl(self, matchobj):
s = int(matchobj.group(0).strip(','))
bytes = s * sector
self.total_bytes += bytes
return '%7.2f G,' % (bytes / g)
def old():
converter = ToGB()
for line in sys.stdin:
sys.stdout.write(re.sub(r'(?<=size=)\s*\d+,(?! Id= 5)', converter.repl, line))
sys.stdout.write('Total Allocated: %0.2f GiB' % (converter.total_bytes / g))
def new(fpath):
with open(fpath) as f:
pt = PartitionTable(f)
i = 0
for size, type in pt.sizes():
if type != 'gap':
a, u = pt.alignment(i)
i += 1
print '%2d %9.2f %s %2s | %d %s' % (i, size, pt.units.upper(), type, a, u.upper())
else:
print ' %9.2f %s %2s' % (size, pt.units.upper(), type)
print '%0.2f %s TOTAL' % (pt.total(), pt.units.upper())
if __name__ == '__main__':
new(sys.argv[1])
""" Here's what the input looks like:
# partition table of /dev/sda
unit: sectors
/dev/sda1 : start= 8192, size= 67108864, Id=83
/dev/sda2 : start= 67117056, size= 16777216, Id=83
/dev/sda3 : start= 83894272, size= 16777216, Id=83
/dev/sda4 : start=100679680, size=876093488, Id= 5
/dev/sda5 : start=100687872, size= 16777216, Id=82
/dev/sda6 : start=117473280, size= 1048576, Id=83
/dev/sda7 : start=118530048, size= 16777216, Id=83
/dev/sda8 : start=135315456, size= 67108864, Id=83
/dev/sda9 : start=202432512, size= 58720256, Id=83
/dev/sda10: start=261160960, size= 4194304, Id=82
/dev/sda11: start=265363456, size= 19841024, Id=83
/dev/sda12: start=285212672, size= 2095104, Id=82
/dev/sda13: start=287309824, size= 20969472, Id= 7
/dev/sda14: start=308281344, size= 67106816, Id= 7
/dev/sda15: start=375390208, size=314572800, Id=83
/dev/sda16: start=689971200, size=251658240, Id=8e
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment