Where to start First steps track · 15 stages
Linux Labs

fdisk and parted — partition a disk

7 min Ubuntu 24.04 · util-linux 2.39 · parted 3.6

fdisk and parted write a partition table (GPT or MBR) and partitions to a disk; parted -s runs without prompts.

Cheat sheet

8 commands Printable version
  • lsblk which disks exist
  • sudo fdisk -l /dev/vdb the disk's partition table
  • sudo parted /dev/vdb print the same with parted
  • sudo parted -s /dev/vdb mklabel gpt new table (wipes the old one)
  • sudo parted -s /dev/vdb mkpart data ext4 1MiB 100%
  • sudo parted /dev/vdb unit MiB print free partitions and free space
  • sudo fdisk /dev/vdb step by step: g, n, w
  • sudo partprobe /dev/vdb kernel re-reads the table

Overview

A new disk is a row of empty blocks. Before it can hold files it is partitioned: a partition table is written at the start of the disk, saying where each partition begins and ends. The kernel shows each partition as a device of its own: /dev/vdb1 on disk /dev/vdb, /dev/nvme0n1p1 on NVMe, /dev/loop0p1 on a loop device. Then a filesystem is created on the partition (mkfs) and mounted (mount).

Table Where you meet it
GPT (gpt) modern systems; up to 128 partitions, disks over 2 TiB
MBR (msdos) older systems; 4 primary partitions, disks up to 2 TiB

Both tools do the same job but save differently:

  • fdisk keeps changes in memory and writes them only on the w command. Quitting with q changes nothing, which makes it safe to practise.
  • parted applies every command at once. With -s (--script) it asks no questions, so it suits scripts and machine setup.

The type in mkpart data ext4 is only a mark in the table. parted does not create the filesystem: after partitioning you still need mkfs.

Options

10

parted has few options; the work is done by commands: mklabel, mkpart, print, rm, resizepart, name. Inside fdisk you type one-letter commands; m lists them. The full lists are in man parted and man fdisk.

Option What it does Example
-s --script Never prompt — not even before wiping a partition table. dangerous sudo parted -s /dev/vdb mklabel gpt
mklabel gpt|msdos Write a new empty partition table; existing partitions are lost. dangerous sudo parted -s /dev/vdb mklabel gpt
mkpart NAME FS START END Create a partition; on MBR the first word is the type primary, not a name. sudo parted -s /dev/vdb mkpart data ext4 1MiB 100%
print Show the table; with free, the free space too. sudo parted /dev/vdb unit MiB print free
-l --list List the partition tables of disks and exit. sudo fdisk -l /dev/vdb
g / o Commands inside fdisk — a new empty GPT (g) or MBR (o) table. dangerous sudo fdisk /dev/vdb
n / p / w / q New partition, print the table, write and quit, quit without saving. sudo fdisk /dev/vdb
unit MiB|GiB|s|% Units for printing and reading partition boundaries. sudo parted /dev/vdb unit MiB print
rm N Remove partition number N from the table. dangerous sudo parted -s /dev/vdb rm 2
partprobe DISK Ask the kernel to re-read the disk's partition table. sudo partprobe /dev/vdb

Nothing found. Try a shorter query.

Recipes

6

Partition an empty disk in one command

bash
lsblk /dev/vdb                       # right disk? empty?
sudo parted -s /dev/vdb mklabel gpt mkpart data ext4 1MiB 100%
lsblk /dev/vdb                       # vdb1 has appeared

The partition starts at 1 MiB: the table sits at the start of the disk, and this offset keeps the partition aligned to the disk's blocks. 100% means up to the end. This is a 512 MiB practice disk after the command:

text
student@ubuntu:~$ sudo parted /dev/loop0 unit MiB print free
Model: Loopback device (loopback)
Disk /dev/loop0: 512MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start    End      Size     File system  Name  Flags
        0.02MiB  1.00MiB  0.98MiB  Free Space
 1      1.00MiB  511MiB   510MiB                data
        511MiB   512MiB   0.98MiB  Free Space

The small free pieces at both ends hold the GPT table and its backup copy.

Two partitions on one disk

bash
sudo parted -s /dev/vdb mklabel gpt
sudo parted -s /dev/vdb mkpart data ext4 1MiB 50%
sudo parted -s /dev/vdb mkpart logs ext4 50% 100%

Add a partition in free space

bash
sudo parted /dev/vdb unit MiB print free    # where the Free Space is
sudo parted -s /dev/vdb mkpart logs ext4 2048MiB 100%

No mklabel here: it would replace the table and drop the existing partitions.

The same in fdisk, step by step

text
sudo fdisk /dev/vdb
  g        new empty GPT table (o — MBR)
  n        new partition: number, start, end — Enter
  p        preview the result
  w        write and quit (q — quit without writing)

For the end you can type a size such as +10G instead of Enter.

An MBR table for an older system

bash
sudo parted -s /dev/vdb mklabel msdos mkpart primary ext4 1MiB 100%

On MBR the first word after mkpart is the partition type (primary), not a name.

The kernel does not see the new partitions

bash
sudo partprobe /dev/vdb      # re-read the table
lsblk /dev/vdb

Troubleshooting

5

Error: /dev/vdb: unrecognised disk label

The disk has no partition table yet — the normal state of a new disk: print shows Partition Table: unknown. Create a table: sudo parted -s /dev/vdb mklabel gpt.

mount: wrong fs type, bad option, bad superblock

You are mounting a disk or partition that has no filesystem. Partitioning creates only partitions; mkfs creates the filesystem. More in mount.

The partition was created but there is no /dev/vdb1

The kernel has not re-read the table: the disk is busy, or the change was made behind its back. Run sudo partprobe /dev/vdb. On a loop device, partitions appear when it is attached with losetup -P — see loop device.

The kernel still uses the old table

fdisk wrote the table, but the kernel keeps the old one because a partition of this disk is mounted. Unmount the disk's partitions and run partprobe; the system disk picks up the change after a reboot.

Warning: The resulting partition is not properly aligned

The start is given in sectors or bytes that do not match the disk's blocks. Give boundaries in MiB or percent: 1MiB, 50%.

Danger zone

2

mklabel on a partitioned disk

mklabel writes a new empty table. The old partitions vanish from it, and with them access to the data. With -s, parted does not even ask. Before partitioning, check the disk name and its partitions with lsblk and findmnt.

The wrong disk

/dev/vdb on one machine and /dev/vdb on another (or after a reboot) can be different disks. Look at the size and contents in lsblk -f right before the command.

Terms

2
partition
A contiguous region of a disk recorded in the partition table. The kernel shows it as a device of its own (/dev/vdb1, /dev/loop0p1) that holds a filesystem. Also appears in: mkfs, blkid, Loop device, Filesystems: ext4, xfs, fsck and 11 more
partition table
A record at the start of a disk of where each partition begins and ends. The formats are GPT (modern systems) and MBR (msdos); fdisk or parted creates it, and blkid -p shows its type (PTTYPE). Also appears in: blkid, mkfs

See also

  • lsblk — find the new disk and check the partitions afterwards.
  • mkfs — create a filesystem on a partition.
  • blkid — the partition's UUID and label for fstab.
  • mount — mount the partition and add it to fstab.
  • loop-device — a practice disk made from a file.
  • lvm — flexible volumes on top of partitions.
Loading…