Disk Partitioning: MBR vs GPT



What is disk partitioning?

Disk Partitioning is a process of creating different regions or blocks, commonly referred as partitions of mass storage devices like fixed disks and removable disks.

Why disk partitioning is required?

The whole purpose of Disk Partitioning is to let Operating System(OS) manage information/data in each region/partition separately.

There are two different disk partitioning schemes as follows:
  1. MBR (Master Boot Record)
  2. GPT (GUID Partition Table)
MBR (Master Boot Record)

MBR is a special type of boot sector resides in partitioned mass storage device. The concept of MBR was publicly introduced in 1983 with PC DOS 2.0 (From Wikipedia). Conventionally, sector size is 512 bytes and first sector of any storage drive contains MBR if that drive is partitioned with MBR scheme. MBR holds minimum boot loader code, primary/logical/ extended partition entry information, file system type, boot signature etc.

The structure of classical MBR is illustrated below,

Let me give a brief about above figure.

Bootstrap code area - Bootloader code resides in this section(446 bytes). From linux perspective GRUB(GRand Unified Bootloader)'s 1-stage code lives in this section. BIOS hands over the system control to code present in this section.

Partition Table - Partition Table is of 16 bytes containing 4 partition entries. MBR partitioning scheme only allows to create 4 primary partitions. You can create extended and logical partition though. You can create 3 primary partition and 1 extended partition which can further create 15 logical partitions. Each partition entry has following information:

Partition Flag - Status of physical drive (bit 7 set is for active or bootable, old MBRs only accept 80hex, 00hex means inactive, and 01hex–7Fhex stand for invalid)

Start CHS Address - CHS(Cylinder-Head-Sector) address of first absolute sector in partition

Partition Type - It is a byte value intended to specify the file system the partition contains and/or to flag special access methods used to access these partition

End CHS Address - CHS(Cylinder-Head-Sector) address of last absolute sector in partition

Start LBA - LBA(Logical Block Addressing) of first absolute sector in the partition

Number of sectors - Total number of sectors present in this partition

Question here? 
        Why MBR has starting sector as both CHS and LBA?


Starting Sector in CHS values (3 bytes). These values pinpoint the location of a partition's first sector, if it's within the first 1024 cylinders of a hard disk. When a sector is beyond that point, the CHS tuples are normally set to their maximum allowed values of 1023, 254, 63; which stand for the 1024th cylinder, 255th head and 63rd sector, due to the fact, cylinder and head counts begin at zero. These values appear on the disk as the three bytes: FE FF FF (in that order).

Starting Sector (4 bytes). LBA (Absolute Sector) value. This value uniquely identifies the first sector of a partition just as Starting CHS values do. But it does so by using a 4-byte Logical Block Address (starts counting from Absolute Sector 0), which means it can locate the beginning of a partition within the first FFFF FFFFh or 4,294,967,296 sectors, for hard disks up to about 2,199,023,255,552 bytes (exactly 2,048 GiB)!

Conclusion: CHS is the conventional field and only used if a particular partition is in the range of first 1024 cylinders of the disk and beyond that LBA is required to find where exactly the partition is on the disk.

Boot Signature - Boot signature is of 2 bytes: 55h, AAh. This is used to verify whether an MBR is valid or not.

From Wikipedia,
        By convention, there are exactly four primary partition table entries in the MBR partition table scheme, although some operating systems and system tools extended this to five (Advanced Active Partitions (AAP) with PTS-DOS 6.60 and DR-DOS 7.07), eight (AST and NECMS-DOS 3.x as well as Storage Dimensions SpeedStor), or even sixteen entries (with Ontrack Disk Manager).

Major limitations of MBR
  • MBR limits the maximum addressable storage space of a disk to 2 TiB (2^32 × 512 bytes) for disk with 512 bytes sectors.
  • MBR also only supports up to four primary partitions—if you want more, you have to make one of your primary partitions an “extended partition” and create logical partitions inside it as I already explain above. This is a silly little hack and shouldn’t be necessary.
  • The partitioning and boot data is stored in one place. If this data is overwritten or corrupted, you’re in trouble.
GPT (GUID Partition Table)

GPT is another partitioning scheme which is latest and overcome limitations which MBR has.Name itself implies that this scheme uses GUID(Globally Unified IDentity) to find out particular partition on disk. Though GPT is a part of UEFI(Unified Extensible Firmware Interface), it is used on some BIOS systems because of MBR limitations. Mostly all modern PC Operating System supports GPT.

GPT structure is as shown below,

Let's try to understand what each field means here,

        LBA is generally 512 bytes, 1 sector by convention.

Protective MBR(LBA 0) - For the sake of compatibility with legacy MBR, GPT has this section, but it is now used in a way that prevents MBR-based disk utilities from misrecognizing and possibly overwriting GPT disk and that's why this is referred to as a protective MBR.

GPT Header(LBA 1) - From wikipedia, GPT header look like as below,

Offset
Length
Contents
0 (0x00)
8 bytes
Signature ("EFI PART", 45h 46h 49h 20h 50h 41h 52h 4h or 0x5452415020494645ULL[a] on little-endian machines)
8 (0x08)
4 bytes
Revision (for GPT version 1.0 (through at least UEFI version 2.7 (May 2017)), the value is 00h 00h 01h 00h)
12 (0x0C)
4 bytes
Header size in little endian (in bytes, usually 5Ch 00h 00h 00h or 92 bytes)
16 (0x10)
4 bytes
CRC32/zlib of header (offset +0 up to header size) in little endian, with this field zeroed during calculation
20 (0x14)
4 bytes
Reserved; must be zero
24 (0x18)
8 bytes
Current LBA (location of this header copy)
32 (0x20)
8 bytes
Backup LBA (location of the other header copy)
40 (0x28)
8 bytes
First usable LBA for partitions (primary partition table last LBA + 1)
48 (0x30)
8 bytes
Last usable LBA (secondary partition table first LBA − 1)
56 (0x38)
16 bytes
Disk GUID (also referred as UUID on UNIXes)
72 (0x48)
8 bytes
Starting LBA of array of partition entries (always 2 in primary copy)
80 (0x50)
4 bytes
Number of partition entries in array
84 (0x54)
4 bytes
Size of a single partition entry (usually 80h or 128)
88 (0x58)
4 bytes
CRC32/zlib of partition array in little endian
92 (0x5C)
*
Reserved; must be zeroes for the rest of the block (420 bytes for a sector size of 512 bytes; but can be more with larger sector sizes)

The partition table header defines the usable blocks on the disk. It also defines the number and size of the partition entries that make up the partition table.

Partition entries (LBA 2–33) - After the header, the Partition Entry Array describes partitions, using a minimum size of 128 bytes for each entry block. The starting location of the array on disk, and the size of each entry, are given in the GPT header. The first 16 bytes of each entry designate the partition type's globally unique identifier (GUID). For example, the GUID for an EFI system partition is C12A7328-F81F-11D2-BA4B-00A0C93EC93B. The second 16 bytes are a GUID unique to the partition. Then follow the starting and ending 64 bit LBAs, partition attributes, and the 36 character (max.) Unicode partition name. As is the nature and purpose of GUIDs and as per RFC4122, no central registry is needed to ensure the uniqueness of the GUID partition type designators.

GUID partition entry format

Offset
Length
Contents
0 (0x00)
16 bytes
16 (0x10)
16 bytes
Unique partition GUID
32 (0x20)
8 bytes
First LBA (little endian)
40 (0x28)
8 bytes
Last LBA (inclusive, usually odd)
48 (0x30)
8 bytes
Attribute flags (e.g. bit 60 denotes read-only)
56 (0x38)
72 bytes
Partition name (36 UTF-16LE code units)

After partition entries, actual partitions are started from LBA 34 in which all the information go.

Once all the actual partitions are over, you could see, again same things are repeated like entries and GPT header. This is called as Secondary GPT and upper one is known as Primary GPT. The secondary GPT is kept because if anything corrupts in primary then partition table information can be retrieved which overcomes one of the limitation of MBR.

If you go back to MBR limitations, GPT overcomes all of them as follows,
  • For disks with 512-byte sectors, maximum size is 9.4 ZB (9.4 × 1021 bytes) or 8 ZiB (coming from 264 sectors × 29 bytes per sector)
  • 128 partitions can be created with GPT which overcomes MBR 4 primary partitions limit
  • Partitioning and boot data is stored in two different places which is kind of backup mechanism for partition information as I explained above

Comments

Popular posts from this blog

MBR partitioning with 'parted' utility

Replace default splash screen in Yocto