11.2. Partitioning Disks

Partitioning a disk writes a small table that describes sector ranges; it does not touch the data inside those ranges. That separation is what makes it safe to plan a layout in advance - and also what makes a stale filesystem signature survive repartitioning, which is why Section 11.1.2 paired the partitioning tools with wipefs. This section covers the two label types, plans a layout for the 2 GiB data disk, creates it, and then examines what happens when the kernel's view of a partition table cannot be updated because the disk is in use.

11.2.1. GPT, MBR, and Firmware Requirements

Two partition-table formats matter on Debian. The classic one is MBR (also called msdos): a table in the disk's first sector holding four primary entries, each a 16-byte record with a 32-bit starting sector and length. More than four partitions require designating one entry an extended partition that contains logical ones. The 32-bit sector field caps an MBR disk at 2^32 sectors - 2 TiB with 512-byte sectors, which is why large disks require the modern format.

That modern format is GPT (GUID Partition Table). It keeps a protective MBR in sector 0 (so old tools see a placeholder rather than garbage), a header in sector 1, the partition-entry array in sectors 2–33, and - its great robustness advantage - a backup copy of both header and entries in the last sectors of the disk. Entries are identified by GUIDs: each partition has a unique GUID, a name, and a type GUID that says what it is for. The sgdisk output in Section 11.2.3 shows these locations verbatim for the example disk: 128 entries maximum, entries in sectors 2–33, first usable sector 34.

Firmware is the deciding constraint for boot disks. A machine booting in legacy BIOS mode executes boot code from the disk's first sector, the MBR scheme. A machine booting in UEFI mode reads a special partition - the EFI System Partition, a FAT filesystem with the ESP type GUID - using GPT. Debian's installer therefore lays out boot disks according to the firmware's mode; Chapter 2 made those choices in Section 2.3.2, and Chapter 7 covered the ESP and boot entries in Section 7.2. One extra entry belongs in the vocabulary: a GPT disk booted by legacy BIOS needs a small BIOS boot partition (sgdisk code ef02) for GRUB's core image, since GPT leaves no unallocated first-sector space for it.

For data disks none of this binds. A GPT data disk works fine in a machine whose boot disk uses MBR, and vice versa. GPT is the right choice for any new data disk: no 2 TiB limit, no primary/logical contortions, a backup of the table, and per-partition type GUIDs that tools read.

The example disk will carry the chapter's GPT layout from here on. To still show an MBR table being made - without disturbing the GPT layout the data disk is about to receive - the walkthrough first creates a small MBR-labelled disk image, a plain file that the kernel can attach as a block device through a loop device. The image lives in john's workspace, so create it first (as john, in an ordinary shell):

$ mkdir -p ~/debian-handbook/storage

Silent. Make it the working directory, so the relative filenames used from here on resolve to it:

$ cd ~/debian-handbook/storage

Also silent. A 64 MiB image of zeros is one dd invocation:

$ dd if=/dev/zero of=msdos-disk.img bs=1M count=64 status=none

Silent again - status=none suppresses dd's completion summary, which would otherwise print one line of statistics. The file msdos-disk.img now exists and is 64 MiB of zeros; it is not yet a block device.

A loop device turns it into one. In the root shell, attach the image, label it with an MBR table, create one partition spanning it, print both tools' views, and detach:

# loop=$(losetup --find --show /home/john/debian-handbook/storage/msdos-disk.img)
# parted -s "$loop" mklabel msdos
# parted -s "$loop" mkpart primary ext4 1MiB 100%
# parted "$loop" unit MiB print
# fdisk -l "$loop"
# losetup -d "$loop"
Model: Loopback device (loopback)
Disk /dev/loop0: 64.0MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start    End      Size     Type     File system  Flags
 1      1.00MiB  64.0MiB  63.0MiB  primary

Disk /dev/loop0: 64 MiB, 67108864 bytes, 131072 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x4d1ecd51

Device       Boot Start    End Sectors Size Id Type
/dev/loop0p1       2048 131071  129024  63M 83 Linux

The sequence is one bounded operation - attach, label, partition, inspect, detach - and the variable loop holds the loop device name that losetup --find --show printed (/dev/loop0 here) and that losetup -d later detaches. Three details in the output map directly onto the MBR description above: parted calls the label msdos while fdisk calls the same thing Disklabel type: dos; the partition starts at sector 2048 (1 MiB in) and ends at 131071, the image's last sector; and its type byte is 83, the MBR code for a Linux filesystem. The mkpart arguments differ from GPT usage: an MBR entry needs a type position (primary) rather than a name, and 100% as the end means "the last usable sector". Both tools created and read the same table, which is the interoperability an MBR disk promises.

The image is detached now and stays in the workspace; Section 11.5.2 reuses this loop-image technique for the inode exercise, because a practice filesystem that is also a file is cheap to throw away.

11.2.2. Alignment, Partition Types, and Layout Planning

Where a partition starts and ends is not arbitrary. Every partition boundary should fall on a multiple of the device's preferred I/O unit - in practice, 1 MiB (2048 sectors of 512 bytes). That alignment is larger than any physical sector size, so it satisfies 512e and 4Kn disks; it matches the erase-block-friendly boundaries SSDs prefer; and it leaves room for the metadata offsets of RAID members. Both parted and sgdisk align new partitions to 1 MiB by default, and parted can verify the result with align-check optimal, as Section 11.2.3 shows.

The second planning decision is the partition type - on GPT, a GUID that announces what the partition is for. Tools read it: udisks marks known types for the desktop, installers find the ESP by it, and a human reading sgdisk -p gets a labeled inventory. The codes used in this book:

sgdisk code GPT type GUID (abbreviated) Meaning
8300 0fc63daf-… Linux filesystem - the default for ext4/XFS/Btrfs data
8200 0657fd6d-… Linux swap
8e00 e6d6b37e-… Linux LVM physical volume (Chapter 13)
ef00 c12a7328-… EFI System Partition (boot disks; Chapter 7)
ef02 21686148-… BIOS boot partition (GRUB on BIOS-booted GPT disks)

The MBR equivalents are one-byte codes seen in fdisk output - 83 Linux, 82 swap, 8e LVM, c FAT - set with fdisk's t command or named to parted at mkpart time.

The layout for the 2 GiB data disk follows. Three 512 MiB partitions, each carrying its own filesystem, each serving one later section, and about 512 MiB deliberately left unallocated at the end:

Partition Name Size Type Purpose in this chapter
/dev/vdb1 data 512 MiB 8300 The persistent-mount example (Sections 11.3.2–11.3.4)
/dev/vdb2 optional 512 MiB 8300 The automount example (Section 11.4.2)
/dev/vdb3 scratch 512 MiB 8300 The busy-filesystem example (Section 11.4.3)
(none) - ≈512 MiB - Left free for later growth (Chapter 14)

Leaving the tail of a disk unallocated costs nothing today and buys options later: a fourth partition can be added, or - with Chapter 14's tools - an existing partition can be grown into the space. Partition layouts are much easier to extend forward than backward.

11.2.3. Creating Partitions on a New Data Disk

The walkthrough now writes the planned layout to /dev/vdb.

Warning: mklabel replaces any partition table already on the disk, making existing partitions inaccessible. Everything on /dev/vdb from this point on is lost; confirm with lsblk and wipefs -n that the disk you are about to label is the right one and contains nothing you need.

In the root shell, create the GPT label:

# parted -s /dev/vdb mklabel gpt

Silent. Then create the three partitions of the plan, one mkpart each:

# parted -s /dev/vdb mkpart data ext4 1MiB 513MiB
# parted -s /dev/vdb mkpart optional ext4 513MiB 1025MiB
# parted -s /dev/vdb mkpart scratch ext4 1025MiB 1537MiB

All three are silent. The -s (script) flag keeps parted non-interactive; without it, parted asks for confirmation on destructive operations, which is the safer mode when typing by hand. Each mkpart names the partition, then gives a filesystem type hint, then the start and end positions. The hint is exactly a hint on GPT: parted does not create a filesystem - Section 11.3.1 does that - it records the matching type GUID, so ext4 here sets the 0fc63daf-… Linux-filesystem code from Section 11.2.2's table. The positions are the table's start and end: partition 1 covers 1 MiB through 513 MiB (512 MiB of data), and so on; the first partition starts at 1 MiB rather than sector 0 to keep the 1 MiB alignment.

Make the kernel re-read the table:

# partprobe /dev/vdb

Silent. Modern partitioning tools already ask the kernel to re-read after each change; partprobe does the same thing explicitly, which is worth doing once after a batch of changes to be certain the kernel's partition devices match what is on disk. (partx -a DEVICE is the finer-grained alternative: it adds any partitions the kernel is missing one by one, without asking the kernel to discard its existing view - useful exactly in the busy situation Section 11.2.4 describes.)

Now inspect what was built. parted again, with the same device-and-commands form used to read the blank disk:

# parted /dev/vdb unit MiB print
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 2048MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start    End      Size    File system  Name      Flags
 1      1.00MiB  513MiB   512MiB               data
 2      513MiB   1025MiB  512MiB               optional
 3      1025MiB  1537MiB  512MiB               scratch

The table is GPT now, and the three partitions carry their planned names and sizes. sgdisk -p shows the same layout at sector granularity, with the type codes and the GPT housekeeping numbers:

# sgdisk -p /dev/vdb
Disk /dev/vdb: 4194304 sectors, 2.0 GiB
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 33E98279-A585-4D37-A4C9-D8A2A24692DC
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 4194270
Partitions will be aligned on 2048-sector boundaries
Total free space is 1048509 sectors (512.0 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         1050623   512.0 MiB   8300  data
   2         1050624         2099199   512.0 MiB   8300  optional
   3         2099200         3147775   512.0 MiB   8300  scratch

Every planning claim from Section 11.2.2 is visible here as a number: the header and entry array occupy sectors 2–33 (GPT's on-disk layout), the first usable sector is 34 but the first partition starts at 2048 ("aligned on 2048-sector boundaries" - the 1 MiB rule), all three partitions carry code 8300, and the free tail is 512 MiB as planned.

Verify the alignment explicitly:

# parted /dev/vdb align-check optimal 1
1 aligned

align-check optimal N asks parted to confirm that partition N's boundaries match the device's optimal I/O topology. Partition 1 passes.

Finally, the kernel's own view, which is what the rest of the chapter consumes:

# lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINTS /dev/vdb
NAME    SIZE TYPE FSTYPE MOUNTPOINTS
vdb       2G disk        
├─vdb1  512M part        
├─vdb2  512M part        
└─vdb3  512M part        

Three partition devices, none with a filesystem yet. The next section turns the first one into a mounted ext4 filesystem - after checking that nothing is already written there.

11.2.4. Rereading Partition Tables and Recognizing Busy Devices

The kernel keeps one in-memory copy of each disk's partition table, built when the device appears and updated when user space asks. Tools request that update through a re-read ioctl (BLKRRPART), which blockdev --rereadpt invokes directly and partprobe asks for across the devices given to it. The kernel performs the replacement wholesale - it discards its current set of partitions for that disk and installs the new one - and it refuses the whole operation if any partition of the disk is in use: an in-use partition cannot simply vanish from under whoever is using it. That refusal surfaces as Device or resource busy.

The practical consequences:

  • After partitioning a disk that is not in use, a re-read just works, as Section 11.2.3's partprobe did.
  • After repartitioning a disk with a mounted partition, the re-read fails until the partition is unmounted. The unchanged old partitions stay active; only genuinely new additions can be grafted on individually with partx -a.
  • Recognizing the failure is the diagnostic skill: the words Device or resource busy in answer to a partition-table operation mean "something is using a partition of this disk", and the something is usually a mounted filesystem. Section 11.3.2 captures the exact failure, once a filesystem is mounted to be busy, and the two-step resolution - find the user of the disk, stop it, retry - is the same one Section 11.4.3 demonstrates for unmounting.

Busy devices also protect against formatting accidents: mkfs.ext4 checks whether its target is mounted and refuses outright, which Section 11.3.2 also captures. The busy check is the kernel and the tools agreeing on one rule - no structural change under an active user - and every storage tool in this chapter honors it.