Partitioning a Disk

Why partition?

A whole disk can be formatted directly, but usually you first divide it into partitions: independent sections, each of which can hold its own filesystem. One disk might carry a system partition, a data partition and a swap partition. The list of partitions is the disk's partition table, written at the very start of the disk.

There are two partition-table formats: the old MBR (also called msdos, up to four primary partitions) and the modern GPT (many partitions, required for disks over 2 TB). Partition names add a number to the disk: the first partition of /dev/sdb is /dev/sdb1.

Creating partitions with parted

parted writes the partition table. mklabel starts a fresh table and mkpart adds a partition between two points on the disk. Here we make an MBR table and two primary partitions, then look at the result with lsblk:

sudo losetup -fP --show big.img
sudo parted -s /dev/loop0 mklabel msdos
sudo parted -s /dev/loop0 mkpart primary ext4 1MiB 64MiB
sudo parted -s /dev/loop0 mkpart primary ext4 64MiB 100%
lsblk /dev/loop0

In the terminal

student@vulntech:~$ sudo losetup -fP --show big.img
[sudo] password for student:
/dev/loop0
student@vulntech:~$ sudo parted -s /dev/loop0 mklabel msdos
student@vulntech:~$ sudo parted -s /dev/loop0 mkpart primary ext4 1MiB 64MiB
student@vulntech:~$ sudo parted -s /dev/loop0 mkpart primary ext4 64MiB 100%
student@vulntech:~$ lsblk /dev/loop0
NAME      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
loop0       7:0    0  128M  0 loop
├─loop0p2 259:1    0   64M  0 part
└─loop0p1 259:2    0   63M  0 part

The disk now has two children, loop0p1 and loop0p2 (on a real disk they'd be sdb1 and sdb2). fdisk -l prints the partition table in detail:

sudo fdisk -l /dev/loop0

In the terminal

student@vulntech:~$ sudo fdisk -l /dev/loop0
[sudo] password for student:
Disk /dev/loop0: 128 MiB, 134217728 bytes, 262144 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: 0x24502362

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

Format and mount a partition

A partition is itself a block device, so you format and mount it exactly like a whole disk — you just use the partition's name:

sudo mkfs.ext4 /dev/loop0p1
sudo mkdir -p /mnt/part1
sudo mount /dev/loop0p1 /mnt/part1
df -h /mnt/part1

In the terminal

student@vulntech:~$ sudo mkfs.ext4 /dev/loop0p1
[sudo] password for student:
mke2fs 1.47.0 (5-Feb-2023)
Creating filesystem with 64512 1k blocks and 16128 inodes
Filesystem UUID: ae16b57f-0bb6-4d01-bd3c-e0809fff0225
Superblock backups stored on blocks:
	8193, 24577, 40961, 57345

Allocating group tables: 0/8   done
Writing inode tables: 0/8   done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: 0/8   done
student@vulntech:~$ sudo mkdir -p /mnt/part1
student@vulntech:~$ sudo mount /dev/loop0p1 /mnt/part1
student@vulntech:~$ df -h /mnt/part1
Filesystem      Size  Used Avail Use% Mounted on
/dev/loop0p1     54M   14K   50M   1% /mnt/part1

Tip: fdisk is the classic interactive partition editor: sudo fdisk /dev/sdb, then n for a new partition, p to print, and w to write and quit (nothing is saved until w). parted -s, used above, does the same non-interactively — handy in scripts.

Common mistake: Changing the partition table of a disk that holds data puts that data out of reach. Partition new or empty disks; on a disk in use, check twice with lsblk first.

Remember: Partitioning divides a disk into independent sections. parted (or interactive fdisk) writes the table; a partition is a block device like /dev/sdb1 that you mkfs and mount on its own.

HOME LEARN COMMUNITY DASHBOARD
Practice terminal Starting…