6.2 Disk Partitioning in Linux: A Comprehensive Guide

6.2 Disk Partitioning in Linux: A Comprehensive Guide

Introduction:

Partitioning is a fundamental aspect of managing disk space in Linux systems. It involves dividing a disk into one or more logical units to organize data effectively. This comprehensive guide provides an in-depth exploration of disk partitioning using tools such as fdisk, cfdisk, parted, and gparted, covering the creation, deletion, and modification of partitions, as well as understanding partition tables and their types.

Understanding Disk Partitioning

Overview

Disk partitioning is the process of dividing a disk into smaller segments known as partitions. These partitions allow for better organization and utilization of disk space, facilitating data storage and management.

Key Concepts

In Linux systems, disks are identified as /dev/sda, /dev/sdb, etc., in physical servers and /dev/vda, /dev/vdb, etc., in virtual machines. Partitions are named based on the disk name followed by a number, such as /dev/sda1, /dev/sda2, etc.

Partition tables, such as MBR (Master Boot Record) and GPT (GUID Partition Table), define the organization of partitions and data on the disk. Understanding these partition table types is crucial for effective disk management.

MBR vs. GPT Partition Tables

PropertiesMBRGPT
Max Capacity2TB9.7ZB (~9.7 billion terabytes)
Max Partitions26128
Data LocationBeginning of driveThroughout the drive
BIOS TypeLegacy BIOSUEFI

Managing Partitions with fdisk

Overview

fdisk is a command-line tool for managing disk partitions in Linux. It offers a text-menu driven interface for creating, deleting, and modifying partitions.

Basic Commands

  • m: Display the menu

  • p: List the partition table

  • n: Create a new partition

  • d: Delete a partition

  • t: Change a partition type

  • w: Write the new partition table information and exit

  • q: Quit without making changes

Example Usage

sudo fdisk /dev/sdb
sudo fdisk -l /dev/sda

Historically, two commands exist to manipulate disks and partitions:  fdisk  and  parted.

fdisk command doesn’t handle GPT partition tables.

Recently, a new tool called gdisk has been created to deal with GPT partition tables, offering an alternative to the parted command.

#Listing current partition system
lsblk
NAME           MAJ:MIN    RM    SIZE    RO    TYPE    MOUNTPOINT
vda              8:0       0     20G     0    disk
├─vda1           8:1       0      1G     0    part        /boot   #partition
└─vda2           8:2       0     19G     0    part                #partition
    ├─cs-root    253:0       0     17G     0    lvm         /
    └─cs-swap    253:1       0      2G     0    lvm        [SWAP]

#list of partitions on block device /dev/sda
sudo fdisk --list /dev/sda
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical) : 512 bytes / 512 bytes
I/0 size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: exb65442dd
Device      Boot    Start          End     Sectors   Size   Id        Type
/dev/sda1     *      2048      2099199     2097152     1G   83       Linux
/dev/sda2         2099200     41943039    39843840    19G   8e   Linux LVM

Fortunately, no actual changes are made until you write the partition table to the disk by entering w. It is therefore important to verify your partition table is correct (with p) before writing to disk with w. If something is wrong, you can jump out safely with q.

The system will not use the new partition table until you reboot. However, you can use the following command:

sudo partprobe -s

to try and read in the revised partition table. However, this doesn't always work reliably, and it is best to reboot before doing things like formatting new partitions, etc., as mixing up partitions can be catastrophic.

At any time, you can run the following command:

cat /proc/partitions

to examine what partitions the operating system is currently aware of.

You can display the partition table and take no action with the following command:

sudo fdisk -l /dev/sda

Exploring Partitioning with cfdisk

Overview

cfdisk provides a text-based "graphical" interface for partitioning disks. It allows users to create, delete, and modify partitions in a user-friendly manner.

cfdisk command is used to create, delete, and modify partitions on a disk device. It displays or manipulates the disk partition table by providing a text-based “graphical” interface.

sudo cfdisk /dev/sda

Pick gpt from the list (dos for MBR). Now you will see a partition table like this:

See the available free space. Here we have 20 GB. Select NEW and create a new partition. Use up-down arrow keys to navigate and enter to select. You can do many things with the free space, if you are installing a new system with a command line interface, you can see an option of using the selected space as primary partition. Example: Select the size 2GB. Enter -> and select primary. Similarly we can do a logical partition also.

After sizing the partition, select what type do you want, in my case, I am choosing Linux Swap.

  • After selecting the size and type write to the disk:

Understanding parted and gparted

parted

parted is a command-line utility for partitioning disks. It offers more features compared to fdisk, including support for GPT partition tables.

Basic Commands

  • mklabel: Create a new partition table

  • mkpart: Create a new partition

  • rm: Remove a partition

  • resize: Resize a partition

  • print: Display partition information

  • quit: Quit parted

Example Usage

sudo parted /dev/sdc

gparted

gparted is a graphical partition editor that provides a user-friendly interface for managing partitions. It is based on parted but offers a visual representation of disk partitions.

Features

  • Create, delete, resize, move, copy, and paste partitions

  • Support for various file systems

  • Graphical representation of disk partitions

Example Usage

sudo gparted

Conclusion

Mastering disk partitioning is essential for effective disk management in Linux environments. With tools like fdisk, cfdisk, parted, and gparted, users can confidently create, delete, and modify partitions to optimize disk usage and organize data efficiently. Understanding the differences between MBR and GPT partition tables empowers users to choose the appropriate partitioning scheme for their specific needs. Whether through the command line or graphical interfaces, disk partitioning remains a crucial skill for Linux administrators and users alike.

Did you find this article valuable?

Support Vijay Kumar Singh by becoming a sponsor. Any amount is appreciated!