Disk partition in Unix

Each hard disk is partitioned into multiple partitions: logical disks. The information where each partition is stored on the disk is store in a partition map. The format of the partition map is dictated by the partition scheme. The most-often used partition schemes are:


 * GUID Partitioning Table (GPT), a modern and extremely common partition scheme.
 * Master Boot Record (MBR), an older format popular on older DOS/Windows disks. It can only be used for disk smaller than 2 TByte.
 * Apple Partition Map (APM), an old (pre-2006) format used on PowerPC-based Apple disks.

In addition, some removable media (such as floppies, CDs, DVDs and USB sticks) might not use a partitioning, but instead have a single file system fill the whole disk.

If you have the choice, use GPT.

Block Alignment
With MBR, the first 512-byte sector of a disk is reserved to store information for up to 4 partitions and a 446-byte bootloader. Usually, the only job of this small bootloader is to find the location of a second bootloader (typically 512kByte) and start booting from there.

GPT uses 34 512-byte sectors, #0 tot #33, with #0 used for backwards compatibility with MBR.

Thus the first actual partition is usually placed at block #34. However, this does not align with the 4096-byte logical blocks of current diskdrives, so it is recommend to add 6 "free" sectors, and let the first actual partition start at block #40.

List partition tables
On BSD and Linux, use gpart:

pakhuis# gpart show =>      34  117231341  ada0  GPT  (56G) 34      1024     1  bios-boot  (512K) 1058         6        - free -  (3.0K) 1064 117230304     2  freebsd-zfs  (56G) 117231368         7        - free -  (3.5K) =>       34  5860533101  ada1  GPT  (2.7T) 34         94        - free -  (47K) 128    4194304     1  freebsd-swap  (2.0G) 4194432 5856338696     2  freebsd-zfs  (2.7T) 5860533128          7        - free -  (3.5K)

on macOS, use diskutil:

[freek@MLT0079-2] ~% diskutil list /dev/disk0 (internal, physical): #:                      TYPE NAME                    SIZE       IDENTIFIER 0:     GUID_partition_scheme                        *251.0 GB   disk0 1:                       EFI EFI                     209.7 MB   disk0s1 2:         Apple_CoreStorage Untitled                250.1 GB   disk0s2 3:                Apple_Boot Recovery HD             650.0 MB   disk0s3 /dev/disk1 (internal, virtual): #:                      TYPE NAME                    SIZE       IDENTIFIER 0:                 Apple_HFS LittleBit              +249.8 GB   disk1 Logical Volume on disk0s2 23422256-E72D-4272-B75D-2C3BA6A8C762 Unlocked Encrypted

Mount disks on Linux
Create a file system:

mkfs -t ext3 /dev/hda1

Mount a partition:

sudo mount -t ext3 /dev/hda9 /backup

It is advisable to add this information to /etc/fstab so you don't have to specify the mount point and disk type manually:

# /dev/hda1      /               ext3    errors=remount-ro       0       1 /dev/hdc7      /mnt/hdc7       ext3    noauto                  0       2
 * 1) /etc/fstab: static file system information.

Note that while fstab is mostly replaced by udev, it is still commonly used to specify mount points for static disks. The only thing that fstab does not handle properly is mounting is unmounting of removable media (such as USB drives) by regular (non-root) users.

Old MBR tools
List partition table:

sudo fdisk -l /dev/hda

Interactively partition disk:

sudo fdisk /dev/hda