GPT (GUID) partition table: Python parsing library

This page hosts a formal specification of GPT (GUID) partition table using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.


Runtime library

All parsing code for Python generated by Kaitai Struct depends on the Python runtime library. You have to install it before you can parse data.

The Python runtime library can be installed from PyPI:

python3 -m pip install kaitaistruct


Parse a local file and get structure in memory:

data = GptPartitionTable.from_file("path/to/local/file.bin")

Or parse structure from a bytes:

from kaitaistruct import KaitaiStream, BytesIO

raw = b"\x00\x01\x02..."
data = GptPartitionTable(KaitaiStream(BytesIO(raw)))

After that, one can get various attributes from the structure by invoking getter methods like:

data.sector_size # => get sector size

Python source code to parse GPT (GUID) partition table

# This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

import kaitaistruct
from kaitaistruct import KaitaiStruct, KaitaiStream, BytesIO

if getattr(kaitaistruct, 'API_VERSION', (0, 9)) < (0, 9):
    raise Exception("Incompatible Kaitai Struct Python API: 0.9 or later is required, but you have %s" % (kaitaistruct.__version__))

class GptPartitionTable(KaitaiStruct):
    .. seealso::
       Source -
    def __init__(self, _io, _parent=None, _root=None):
        self._io = _io
        self._parent = _parent
        self._root = _root if _root else self

    def _read(self):

    class PartitionEntry(KaitaiStruct):
        def __init__(self, _io, _parent=None, _root=None):
            self._io = _io
            self._parent = _parent
            self._root = _root if _root else self

        def _read(self):
            self.type_guid = self._io.read_bytes(16)
            self.guid = self._io.read_bytes(16)
            self.first_lba = self._io.read_u8le()
            self.last_lba = self._io.read_u8le()
            self.attributes = self._io.read_u8le()
   = (self._io.read_bytes(72)).decode(u"UTF-16LE")

    class PartitionHeader(KaitaiStruct):
        def __init__(self, _io, _parent=None, _root=None):
            self._io = _io
            self._parent = _parent
            self._root = _root if _root else self

        def _read(self):
            self.signature = self._io.read_bytes(8)
            if not self.signature == b"\x45\x46\x49\x20\x50\x41\x52\x54":
                raise kaitaistruct.ValidationNotEqualError(b"\x45\x46\x49\x20\x50\x41\x52\x54", self.signature, self._io, u"/types/partition_header/seq/0")
            self.revision = self._io.read_u4le()
            self.header_size = self._io.read_u4le()
            self.crc32_header = self._io.read_u4le()
            self.reserved = self._io.read_u4le()
            self.current_lba = self._io.read_u8le()
            self.backup_lba = self._io.read_u8le()
            self.first_usable_lba = self._io.read_u8le()
            self.last_usable_lba = self._io.read_u8le()
            self.disk_guid = self._io.read_bytes(16)
            self.entries_start = self._io.read_u8le()
            self.entries_count = self._io.read_u4le()
            self.entries_size = self._io.read_u4le()
            self.crc32_array = self._io.read_u4le()

        def entries(self):
            if hasattr(self, '_m_entries'):
                return self._m_entries

            io = self._root._io
            _pos = io.pos()
   * self._root.sector_size))
            self._raw__m_entries = []
            self._m_entries = []
            for i in range(self.entries_count):
                _io__raw__m_entries = KaitaiStream(BytesIO(self._raw__m_entries[i]))
                self._m_entries.append(GptPartitionTable.PartitionEntry(_io__raw__m_entries, self, self._root))

            return getattr(self, '_m_entries', None)

    def sector_size(self):
        if hasattr(self, '_m_sector_size'):
            return self._m_sector_size

        self._m_sector_size = 512
        return getattr(self, '_m_sector_size', None)

    def primary(self):
        if hasattr(self, '_m_primary'):
            return self._m_primary

        io = self._root._io
        _pos = io.pos()
        self._m_primary = GptPartitionTable.PartitionHeader(io, self, self._root)
        return getattr(self, '_m_primary', None)

    def backup(self):
        if hasattr(self, '_m_backup'):
            return self._m_backup

        io = self._root._io
        _pos = io.pos() - self._root.sector_size))
        self._m_backup = GptPartitionTable.PartitionHeader(io, self, self._root)
        return getattr(self, '_m_backup', None)