1 # Copyright 2012 GRNET S.A. All rights reserved.
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
7 # 1. Redistributions of source code must retain the above
8 # copyright notice, this list of conditions and the following
11 # 2. Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following
13 # disclaimer in the documentation and/or other materials
14 # provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
29 # The views and conclusions contained in the software and
30 # documentation are those of the authors and should not be
31 # interpreted as representing official policies, either expressed
32 # or implied, of GRNET S.A.
43 """Represents a Master Boot Record."""
44 class Partition(object):
45 """Represents a partition entry in MBR"""
48 def __init__(self, raw_part):
49 """Create a Partition instance"""
57 ) = struct.unpack(self.format, raw_part)
60 """Pack the partition values into a binary string"""
61 return struct.pack(self.format,
71 """Returns the size of an MBR partition entry"""
72 return struct.calcsize(MBR.Partition.format)
75 start = self.unpack_chs(self.start)
76 end = self.unpack_chs(self.end)
77 return "%d %s %d %s %d %d" % (self.status, start, self.type, end,
78 self.first_sector, self.sector_count)
80 def unpack_chs(self, chs):
81 """Unpacks a CHS address string to a tuple."""
85 head = struct.unpack('<B', chs[0])[0]
86 sector = struct.unpack('<B', chs[1])[0] & 0x3f
87 cylinder = (struct.unpack('<B', chs[1])[0] & 0xC0) << 2 | \
88 struct.unpack('<B', chs[2])[0]
90 return (cylinder, head, sector)
92 def pack_chs(self, cylinder, head, sector):
93 """Packs a CHS tuple to an address string."""
95 assert 1 <= sector <= 63
96 assert 0 <= cylinder <= 1023
97 assert 0 <= head <= 255
100 byte1 = (cylinder >> 2) & 0xC0 | sector
101 byte2 = cylinder & 0xff
103 return struct.pack('<BBB', byte0, byte1, byte2)
105 format = "<444s2x16s16s16s16s2s"
107 Offset Length Contents
108 0 440(max. 446) code area
109 440 2(optional) disk signature
117 def __init__(self, block):
118 """Create an MBR instance"""
125 self.signature) = struct.unpack(self.format, block)
129 self.part[i] = self.Partition(raw_part[i])
133 """Return the size of a Master Boot Record."""
134 return struct.calcsize(MBR.format)
137 """Pack an MBR to a binary string."""
138 return struct.pack(self.format,
149 ret += "Partition %d: %s\n" % (i, self.part[i])
150 ret += "Signature: %s %s\n" % (hex(ord(self.signature[0])),
151 hex(ord(self.signature[1])))
155 class GPTPartitionTable(object):
156 """Represents a GUID Partition Table."""
157 class GPTHeader(object):
158 """Represents a GPT Header of a GUID Partition Table."""
159 format = "<8s4sII4xQQQQ16sQIII"
161 Offset Length Contents
164 12 4 bytes Header size in little endian
165 16 4 bytes CRC32 of header
166 20 4 bytes Reserved; must be zero
167 24 8 bytes Current LBA
168 32 8 bytes Backup LBA
169 40 8 bytes First usable LBA for partitions
170 48 8 bytes Last usable LBA
171 56 16 bytes Disk GUID
172 72 8 bytes Partition entries starting LBA
173 80 4 bytes Number of partition entries
174 84 4 bytes Size of a partition entry
175 88 4 bytes CRC32 of partition array
176 92 * Reserved; must be zeroes
180 def __init__(self, block):
181 """Create a GPTHeader instance"""
188 self.first_usable_lba,
189 self.last_usable_lba,
191 self.part_entry_start,
193 self.part_entry_size,
194 self.part_crc32) = struct.unpack(self.format, block)
197 """Packs a GPT Header to a binary string."""
198 return struct.pack(self.format,
205 self.first_usable_lba,
206 self.last_usable_lba,
208 self.part_entry_start,
210 self.part_entry_size,
215 """Return the size of a GPT Header."""
216 return struct.calcsize(GPTPartitionTable.GPTHeader.format)
219 """Print a GPTHeader"""
220 return "Signature: %s\n" % self.signature + \
221 "Revision: %r\n" % self.revision + \
222 "Header Size: %d\n" % self.hdr_size + \
223 "CRC32: %d\n" % self.header_crc32 + \
224 "Current LBA: %d\n" % self.current_lba + \
225 "Backup LBA: %d\n" % self.backup_lba + \
226 "First Usable LBA: %d\n" % self.first_usable_lba + \
227 "Last Usable LBA: %d\n" % self.last_usable_lba + \
228 "Disk GUID: %s\n" % uuid.UUID(bytes=self.uuid) + \
229 "Partition entries starting LBA: %d\n" % \
230 self.part_entry_start + \
231 "Number of Partition entries: %d\n" % self.part_count + \
232 "Size of a partition entry: %d\n" % self.part_entry_size + \
233 "CRC32 of partition array: %s\n" % self.part_crc32
235 def __init__(self, disk):
236 """Create a GPTPartitionTable instance"""
238 with open(disk, "rb") as d:
239 # MBR (Logical block address 0)
240 lba0 = d.read(BLOCKSIZE)
243 # Primary GPT Header (LBA 1)
244 raw_header = d.read(self.GPTHeader.size())
245 self.primary = self.GPTHeader(raw_header)
247 # Partition entries (LBA 2...34)
248 d.seek(self.primary.part_entry_start * BLOCKSIZE)
249 entries_size = self.primary.part_count * \
250 self.primary.part_entry_size
251 self.part_entries = d.read(entries_size)
253 # Secondary GPT Header (LBA -1)
254 d.seek(self.primary.backup_lba * BLOCKSIZE)
255 raw_header = d.read(self.GPTHeader.size())
256 self.secondary = self.GPTHeader(raw_header)
259 """Return the payload size of GPT partitioned device."""
260 return (self.primary.backup_lba + 1) * BLOCKSIZE
262 def shrink(self, size, old_size):
263 """Move the secondary GPT Header entries to the address specified by
267 # Most partition manipulation programs leave 2048 sector after the last
269 aligned = size + 2048 * BLOCKSIZE
271 # new_size is at least: size + Partition Entries + Secondary GPT Header
272 new_size = aligned if aligned <= old_size else \
273 size + len(self.part_entries) + BLOCKSIZE
275 assert new_size <= old_size, "The secodary GPT fits in the device"
277 if new_size == self.size():
280 lba_count = new_size // BLOCKSIZE
283 #TODO: Check if the partition tables is hybrid
284 self.mbr.part[0].sector_count = (new_size // BLOCKSIZE) - 1
287 self.primary.header_crc32 = 0
288 self.primary.backup_lba = lba_count - 1 # LBA-1
289 self.primary.last_usable_lba = lba_count - 34 # LBA-34
290 self.primary.header_crc32 = \
291 binascii.crc32(self.primary.pack()) & 0xffffffff
293 # Fix Secondary header
294 self.secondary.header_crc32 = 0
295 self.secondary.current_lba = self.primary.backup_lba
296 self.secondary.last_usable_lba = lba_count - 34 # LBA-34
297 self.secondary.part_entry_start = lba_count - 33 # LBA-33
298 self.secondary.header_crc32 = \
299 binascii.crc32(self.secondary.pack()) & 0xffffffff
301 # Copy the new partition table back to the device
302 with open(self.disk, "wb") as d:
303 d.write(self.mbr.pack())
304 d.write(self.primary.pack())
305 d.write('\x00' * (BLOCKSIZE - self.primary.size()))
306 d.write(self.part_entries)
307 d.seek(self.secondary.part_entry_start * BLOCKSIZE)
308 d.write(self.part_entries)
309 d.seek(self.primary.backup_lba * BLOCKSIZE)
310 d.write(self.secondary.pack())
311 d.write('\x00' * (BLOCKSIZE - self.secondary.size()))
315 if __name__ == '__main__':
316 ptable = GPTPartitionTable(sys.argv[1])
318 print "MBR:\n%s" % ptable.mbr
319 print "Primary partition table:\n%s" % ptable.primary
320 print "Secondary partition table:\n%s" % ptable.secondary
322 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :