Hard disk drives  (HDDs)

Block device partitioning has a long history, related to the physical construction of hard disk drives – to their cylinders, heads and sectors.  Current hard disk drives present themselves to the operating system as a simple sequence of blocks.  For practical partitioning purposes the physical construction no longer matters except that Zone Bit Recording means that a partition at the beginning of the drive (physically the outer edge) transfers data faster than one at the end of the drive.  With the introduction of 4k sector sizes, aligning partitions on sector boundaries has become important.

Wikipedia hard disk drive

Wikipedia disk sector

Wikipedia zone bit recording (ZBR a.k.a Zone Constant Angular Velocity (Zone CAV, Z-CAV or ZCAV))

Wikipedia disk partitioning

Wikipedia advanced format (> 512 B sectors)

Partition tables

Reference: http://en.wikipedia.org/wiki/Partition_table

DOS partition table (a.k.a MBR)

Also known as the MBR partition table.

Reference: http://en.wikipedia.org/wiki/Master_boot_record

The EBR/EPBR

Each logical partition is preceded by an Extended Boot Record (EBR) a.k.a. Extended Partition Boot Record (EPBR) of one sector followed by a number of unused sectors. There are commonly 62 unused sectors which, with the EBR, fill a historical 63 sector track.

Which partition management utility to use?

(warning) If grub is installed in the MBR, using gdisk or cgdisk will make the system unbootable by overwriting the MBR.

From the fdisk man page:

There are several *fdisk programs around. Each has its problems and strengths. Try them in the order cfdisk, fdisk, sfdisk. (Indeed, cfdisk is a beautiful program that has strict requirements on the partition tables it accepts, and produces high quality partition tables. Use it if you can. fdisk is a buggy program that does fuzzy things - usually it happens to produce reasonable results. Its single advantage is that it has some support for BSD disk labels and other non-DOS partition tables. Avoid it if you can. sfdisk is for hackers only -- the user interface is terrible, but it is more correct than fdisk and more powerful than both fdisk and cfdisk. Moreover, it can be used noninteractively.)

These days there also is parted. The cfdisk interface is nicer, but parted does much more: it not only resizes partitions, but also the filesystems that live in them.

GUID partition table (GPT)

GPT is required when the HDD presents 512 kB sectors and is greater than 2 TiB.

References:

Which partition management utility to use?

For GPT-partitioning, there is gdisk and its full-screen version cgdisk.  They automatically do 4096 kB physical sector alignment.

Appendix – exact DOS partition sizes

Find an exact partition size

The easiest way to find an exact partition size is to read the /sys/block/sd*/sd*/size file, for example cat /sys/block/sda/sda2/size.  This gives the size of the sda2 partition in sectors.

TODO: check what happens with a 4 kB sector size HDD.  If /sys/block/sd*/sd*/size then lists the size in 4k sectors, how best to find the sector size?  fdisk?

The alternatives are:

The partition size shown by fdisk (in 1024 bytes Blocks) is rounded; if an exact partition size is required it can be calculated by End - Start + 1 (fdisk shows End and Start in sectors). For example:

root@CW8:~# fdisk -l /dev/sdb
[snip]
Device Boot Start End Blocks Id System
[snip]
/dev/sdb7 1224268508 1250261614 12996553+ 83 Linux

End - Start + 1 is 1250261614 - 1224268508 + 1 = 25993107.

Exact size in sectors, converted to blocks: 25993107 / 2 = 12996553.5

The most informative listing is produced by the print option -P with the display sectors option -s (other units are rounded for display).

In the output, illustrated below:

root@CW8:~# cfdisk -Ps /dev/sdb
Partition Table for /dev/sdb
               First       Last
 # Type       Sector      Sector   Offset    Length   Filesystem Type (ID) Flag
-- ------- ----------- ----------- ------ ----------- -------------------- ----
[snip]
 7 Logical  1224268445* 1250261614*    63    25993170 Linux (83)           None

Length less Offset: 25993170 - 63 = 25993107.

Create a partition with an exact size