Linux disk amangement (4) - disk partition table MBR and GPT

In Linux world, disk management is an interesting area. As time moves on, the disk capacity is increasing, speed is improving, and price is dropping. In this post, let’s talk about disk partition table type:MBR and GPT.

Linux partition type

Master Boot Record (MBR)

MBR was born in the early 1980s for IBM PCs, way back in the thrilling days of ten-megabyte hard disks. The MBR must live on the first 512 bytes (first sector) of disk, and it holds the bootloader and partition table. The bootloader occupies 446 bytes, the partition table uses 64 bytes, and the remaining two bytes store the boot signature. The MBR is limited to four primary partitions, and a single primary partition can be an extended partition which can then be divided into logical partitions. Linux supports (theoretically) an unlimited number of logical partitions. MBR is tiny and inflexible, and has lasted this long thanks to clever hacks to get around its limitations. Logical Block Addressing (LBA) gets around the limitations of its original cylinders, heads, and sectors (CHS) addressing. The traditional hard disk block size of 512 bytes limits partitions to 2TB in size. Even though MBR is still widely used, but it is retiring…

GUID Partition Table (GPT)

GPT is part of the Unified Extensible Firmware Interface (UEFI) specification, GPT does not have primary and logical partitions.

GPT has several advantages over the MBR:

  • 64-bit disk pointers allows 2 powerof 64 total sectors, so a hard disk with 512-byte blocks can be as large as 8 zebibytes. With 4096-byte sectors your maximum disk size is really really large.
  • The default maximum number of partitions is 128, and if your operating system supports it you can have more.
  • No more CHS cruft or hacky primary-extended-logical partitioning scheme, which falls down if you need Windows because Windows is inflexible and hogs primary partitions
  • GPT has fault-tolerance by keeping copies of the partition table in the first and last sector on the disk
  • GPT computes a cyclic redundancy check (CRC) checksum to verify its own integrity, and of the partition table
  • Unique IDs for disks and partitions.

Let’s do some simple exercise by using a 1GB USB pendrive. I insert 1GB UBS pendrive into a box running Centos, it is detected as /dev/sdb.

First thing to notice is that the partition tool “gdisk”, instead of “fdisk”. Second thing, “gdisk -l /dev/sdb” tells there is no partition exists in the device so far.

[root@sg-centos-hv1 scratch]# gdisk -l /dev/sdb
GPT fdisk (gdisk) version 0.8.10

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.
Disk /dev/sdb: 2025472 sectors, 989.0 MiB
Logical sector size: 512 bytes
Disk identifier (GUID): D8D32FE0-922C-4CE4-9FA1-CCB016160620
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 2025438
Partitions will be aligned on 2048-sector boundaries
Total free space is 2025405 sectors (989.0 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
[root@sg-centos-hv1 scratch]#

Let’s create 5 partitions, first one is 100MB, the second one is 200MB, and third one is 100MB, fourth is 100MB, fifth has the rest of space.

[root@sg-centos-hv1 scratch]# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.10

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.

Command (? for help): ?
b       back up GPT data to a file
c       change a partition's name
d       delete a partition
i       show detailed information on a partition
l       list known partition types
n       add a new partition
o       create a new empty GUID partition table (GPT)
p       print the partition table
q       quit without saving changes
r       recovery and transformation options (experts only)
s       sort partitions
t       change a partition's type code
v       verify disk
w       write table to disk and exit
x       extra functionality (experts only)
?       print this menu

Command (? for help): p
Disk /dev/sdb: 2025472 sectors, 989.0 MiB
Logical sector size: 512 bytes
Disk identifier (GUID): D8D32FE0-922C-4CE4-9FA1-CCB016160620
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 2025438
Partitions will be aligned on 2048-sector boundaries
Total free space is 2025405 sectors (989.0 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name

As it is shown, there is no partition table so far.

Command (? for help): n
Partition number (1-128, default 1):
First sector (34-2025438, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-2025438, default = 2025438) or {+-}size{KMGTP}: +100M
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'

Command (? for help): p
Disk /dev/sdb: 2025472 sectors, 989.0 MiB
Logical sector size: 512 bytes
Disk identifier (GUID): D8D32FE0-922C-4CE4-9FA1-CCB016160620
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 2025438
Partitions will be aligned on 2048-sector boundaries
Total free space is 1820605 sectors (889.0 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048          206847   100.0 MiB   8300  Linux filesystem

As it is shown, first partition with 100MB is created now.

Command (? for help): n
Partition number (2-128, default 2):
First sector (34-2025438, default = 206848) or {+-}size{KMGTP}:
Last sector (206848-2025438, default = 2025438) or {+-}size{KMGTP}: +200M
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'

Command (? for help): p
Disk /dev/sdb: 2025472 sectors, 989.0 MiB
Logical sector size: 512 bytes
Disk identifier (GUID): D8D32FE0-922C-4CE4-9FA1-CCB016160620
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 2025438
Partitions will be aligned on 2048-sector boundaries
Total free space is 1411005 sectors (689.0 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048          206847   100.0 MiB   8300  Linux filesystem
   2          206848          616447   200.0 MiB   8300  Linux filesystem

As it is shown, first and second partitions are created.

Command (? for help): n
Partition number (3-128, default 3):
First sector (34-2025438, default = 616448) or {+-}size{KMGTP}:
Last sector (616448-2025438, default = 2025438) or {+-}size{KMGTP}: +100M
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'

Command (? for help): n
Partition number (4-128, default 4):
First sector (34-2025438, default = 821248) or {+-}size{KMGTP}:
Last sector (821248-2025438, default = 2025438) or {+-}size{KMGTP}: +100M
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'

Command (? for help): n
Partition number (5-128, default 5):
First sector (34-2025438, default = 1026048) or {+-}size{KMGTP}:
Last sector (1026048-2025438, default = 2025438) or {+-}size{KMGTP}:
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'

Command (? for help): p
Disk /dev/sdb: 2025472 sectors, 989.0 MiB
Logical sector size: 512 bytes
Disk identifier (GUID): D8D32FE0-922C-4CE4-9FA1-CCB016160620
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 2025438
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048          206847   100.0 MiB   8300  Linux filesystem
   2          206848          616447   200.0 MiB   8300  Linux filesystem
   3          616448          821247   100.0 MiB   8300  Linux filesystem
   4          821248         1026047   100.0 MiB   8300  Linux filesystem
   5         1026048         2025438   488.0 MiB   8300  Linux filesystem

As it is shown, all five partitions are created. Now it is time to save it to make it permanent.

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.

Now let’s verify it.

[root@sg-centos-hv1 scratch]# gdisk -l /dev/sdb
GPT fdisk (gdisk) version 0.8.10

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.
Disk /dev/sdb: 2025472 sectors, 989.0 MiB
Logical sector size: 512 bytes
Disk identifier (GUID): D8D32FE0-922C-4CE4-9FA1-CCB016160620
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 2025438
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048          206847   100.0 MiB   8300  Linux filesystem
   2          206848          616447   200.0 MiB   8300  Linux filesystem
   3          616448          821247   100.0 MiB   8300  Linux filesystem
   4          821248         1026047   100.0 MiB   8300  Linux filesystem
   5         1026048         2025438   488.0 MiB   8300  Linux filesystem
[root@sg-centos-hv1 scratch]#

All looks good!