Skip to content

Linux Foundations / Understand the system underneath ยท Step 4 of 38

Lesson: Disk PartitioningSwitch
โ† Linux Foundations

System administration

Disk Partitioning

Inspect and change partition tables with fdisk and parted, without destroying the wrong disk.

Partitioning divides a disk into sections. Changing a partition table can destroy access to everything on the disk, so practise on a spare disk or a throwaway virtual machine, and keep backups.

Identify the disk first

$ lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda      8:0    0   40G  0 disk
โ””โ”€sda1   8:1    0   40G  0 part /
sdb      8:16   0   10G  0 disk

Here sdb is empty and sda holds the running system. Check size and mount points every time.

Using fdisk

Start it on the whole disk, not on a partition:

$ sudo fdisk /dev/sdb

Command (m for help):

Useful keys inside fdisk:

  • m shows help
  • p prints the current table
  • g creates a new empty GPT table, o a new MBR (dos) table
  • n adds a partition
  • d deletes one
  • t changes a partition type
  • w writes changes and exits
  • q quits without saving

Your edits stay in memory until you press w. If you make a mistake, press q.

For a new partition, accept the default first sector and give a size such as +5G for the last sector. Then check with p before writing.

Using parted

parted also handles MBR and GPT, and is convenient in scripts. Unlike fdisk, it applies changes to the disk immediately.

$ sudo parted /dev/sdb mklabel gpt
$ sudo parted /dev/sdb mkpart data ext4 1MiB 5GiB
$ sudo parted /dev/sdb print

mklabel replaces the partition table. In mkpart, data is a GPT partition name and ext4 is only a type hint. It does not create a filesystem.

Graphical tools such as GParted do the same jobs. cfdisk offers a menu in the terminal.

After writing

The kernel usually notices the new table at once. If the disk is in use it may not, and you may need sudo partprobe /dev/sdb or a reboot. Confirm with lsblk:

$ lsblk /dev/sdb
NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sdb      8:16   0  10G  0 disk
โ””โ”€sdb1   8:17   0   5G  0 part

The new partition still has no filesystem. That is the next step.

Try it

You can practise without a real disk. Make an empty file and partition that instead:

$ truncate -s 200M disk.img
$ fdisk disk.img

Create a GPT table with g, add two partitions with n, check with p, then write with w. Delete disk.img when finished.

Check your understanding

Disk Partitioning

All 5 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.