Skip to main content

LFCS Certification Course

Chapter #4: How to Partition Disks, Format Filesystems, and Set Up Swap in Linux

Learn how to partition storage devices using fdisk and gdisk, format filesystems like ext4, and configure swap space in Linux.

Partitioning is a means to divide a single hard drive into one or more parts or β€œslices” called partitions. A partition is a section on a drive that is treated as an independent disk and that contains a single type of file system, whereas a partition table is an index that relates those physical sections of the hard drive to partition identifications.

In Linux, the traditional tool for managing MBR partitions (up to ~2009) in IBM PC compatible systems is fdisk. For GPT partitions (~2010 and later), we will use gdisk.

Each of these tools can be invoked by typing its name, followed by a device name (such as /dev/sdb).

You can find available disks using any of the following commands, which will give you a list of available disks and their existing partitions.

lsblk
or
sudo fdisk -l

Managing MBR Partitions With fdisk

We will start by using fdisk to manage MBR partitions, which will launch an interactive prompt where you can enter partitioning commands.

If you're unsure what to do next, press the m key to display the help menu, which lists all available options.

fdisk /dev/sdb
Create and Manage MBR Partitions on Linux Systems

In the above image, the most frequently used options are highlighted. At any moment, you can press p to display the current partition table.

Display the Current Partition Table.

The Id column shows the partition type (or partition id) that has been assigned by fdisk to the partition.

A partition type serves as an indicator of the file system the partition contains, or, in simple words, the way data will be accessed in that partition.

Tip: Fdisk does not write changes to disk until you explicitly write them using w. Until then, you're working in memory.

Please note that a comprehensive study of each partition type is out of the scope of this tutorial, as this chapter is focused on the LFCS exam, which is performance-based.

  • You can list all the partition types that can be managed by fdisk by pressing the l option (lowercase L).
  • Press d to delete an existing partition. If more than one partition is found in the drive, you will be asked which one should be deleted. Enter the corresponding number, and then press w (write modifications to the partition table) to apply changes.

In the following example, we will delete /dev/sdb2, and then print (p) the partition table to verify the modifications:

Removing a Partition in Linux
  • If the partition Id that fdisk chose is not the right one for our setup, we can press t to change it:
Modify Partition Type in Linux

When you’re done setting up the partitions, press w to commit the changes to disk:

Save Partition Changes in Linux

After partitioning, always run partprobe or kpartx -a /dev/sdX to notify the OS of partition table changes.

Managing GPT Partitions with gdisk

In the following example, we will use /dev/sdb:

gdisk /dev/sdb

We must note that gdisk can be used either to create MBR or GPT partitions:

Using gdisk to Create MBR or GPT Partitions

The advantage of using GPT partitioning is that we can create up to 128 partitions in the same disk, whose size can be up to the order of petabytes, whereas the maximum size for MBR partitions is 2 TB.

Note that most of the options in fdisk are the same in gdisk. For that reason, we will not go into detail about them.

Tip: Use sudo gdisk -l /dev/sdX to inspect GPT partition tables before modifying.

Formatting Filesystems in Linux

Once we have created all the necessary partitions, we must create filesystems. To find out the list of filesystems supported in your system, run: