disk partition: how to partition a hard disk in linux

Disk Partitioning is a process by which one or more logical drives are created in a single data storage device. This creates a continuous set of blocks on the device along with its own address tables and is treated by the operating system as an independent disk.

There are mainly 4 different partition types that can be used…

Primary Partition: A primary partition contains just one file system. A boot partition which houses the kernel is an example of a primary partition. A primary partition can also be used to store your root file system.

Extended Partition: An extended partition is essentially a primary partition that has been sub-divided into multiple partitions. You are allowed to have only one extended partition on a device. An extended partition is not formatted or assigned a driver letter (in windows), but logical drives within this extended partition is and can be formatted.

Swap Partition: Swap partition or swap space is a partition that is dedicated entirely to the virtual memory management of your system.

Logical Partition: Each sub-divided partition inside of an extended partition is called a logical partition.

Linux allows only four primary partitions on a single device. If you need to have more than 4 partitions, then you can sub-divide one of the primary partitions into more partitions. Though you can sub-divide only one of the primary partition which is then called an extended partition.

There are several softwares that allow you to create partitions and manage the partition table on your disk. fdisk, cfdisk, sfdisk, parted and gparted are all softwares or utilities that enable you to do this. Some are GUI based while others are console based.

In this post, we will mostly use fdisk as an example as it is available on almost all Linux distros and that it supports an interactive console based interface. It is very likely that you will encounter a situation where you will not have a system where the GUI based system while performing a partition and/or formatting of a disk.

This is not a step-by-step instruction for prepping the primary disk in a Linux install or for what partition and file system formats you should create for your disk. These will depend on your particular Linux distro and the requirements as to why you are partitioning the disk.

running fdisk on linux

WARNING: All actions in and to the partition table is capable of deleting data and thus making the system unusable. Make sure to backup any data that you might want, before performing any of these actions.

Before you proceed with the partitioning, it is wise to know the hard disks that are mounted on your system. You can list out all the currently mounted hard disks on your system, and figure out the mount points or device names which will make the partitioning commands easier.

You can start  in interactive mode by typing fdisk with the device name as the command line option. All the operations are performed when in the interactive mode by typing specific character commands.

bash$ fdisk /dev/sda

Creating a new partition

This will create a brand new partition on the device. You will provide the starting sector and the ending sector or the size of the partition. These sectors should not be part of another partition and be free. You will need to delete the existing partitions if the sectors are part of another partition.

You will also need to choose between either a primary partition or an extended partition. The character command to create a new partition is ‘n’. The program will ask for the partition type and the size of the partition.

Deleting a partition

This will delete an existing partition of the disk. This is not a revokable action and you will lose all stored data on this partition. The option for deletion is ‘d’ and you will also specify the particular partition number that is to be deleted.

Making a partition bootable

The partition should exist and be a primary partition in order to be assigned as bootable. You can make a partition bootable with the option ‘a’.

Write/Save the partition table to disk

After you have created the desired partitions, you will need to persist it to the disk in order for your changes to take effect. After you have created or modified all the partitions as required and verifying that everything is correct you can save it using the ‘w’ command.

Verify the partitions

It is also important to verify your partitions. You can verify the partition table with the command ‘v’.

Print or View all partitions

It is easy enough to print out all the partitions on all your connected devices.

bash$ fdisk -l

If you are in the fdisk interactive user interface, then use the option ‘p’ to print out the current partition table of the current device.

bash$ parted -l