Linux Partitioning: A Comprehensive GuidešŸ“

@Harsh
9 min readJan 22, 2024

--

Introduction

Linux partitioning is the bedrock of efficient system management, a pivotal aspect often veiled in complexities.

Embarking on your Linux journey? Letā€™s unravel the fundamentals of partitioning, a crucial aspect of system organization. In this guide, weā€™ll navigate through the basics of Linux partitioning with intuitive diagrams, making the learning process accessible to all.

However, some misconceptions often surround this essential process, leading users down a path of confusion. In this exploration, we unravel the myths surrounding disk partitioning and shed light on the real-world implications of primary, extended, and logical partitions.

Letā€™s start

Disk Partitioning

Disk Partitioning is the process of dividing a disk into one or more logical areas, often known as partitions, on which the user can work separately. It is one step of disk formatting. If a partition is created, the disk will store the information about the location and size of partitions in the partition table. The main advantage of disk partitioning is that each partition can be managed separately. Then the disk is formatted that is to choose the appropriate file system for that disk and mounting it.

SECTORS

Each platter of a hard disk is divided into tracks, and each track is divided into sectors. Sectors are fixed at 512 bytes in size on most file systems. Creation of partitions is based on sectors and while creating partitions, 2048 is a common starting sector because it ensures that the partition is aligned with the 4 KB sector boundary.

Basics of Partitioning

When you set foot into the world of Linux, envision your hard drive as a vast canvas awaiting structure. At its core, primary partitions serve as the foundational blocks where your operating system resides. However, with limits to the number of primary partitions, enter the extended partition ā€” a metaphorical expansion of the canvas. This extended area opens the door to additional logical partitions.

Letā€™s Understand with the following diagram.

Illustration of a Hard Drive with Primary and Extended and Logical Partitions

Imagine a Hard disk of 10GB in size, Now as we cannot use the disk device directly for writing and reading the data, we will create the partitions because partitions are the only way to use our hard disk for storing data.

And, there are three types of partitions, i.e. primary partition, extended partition and logical partition.

Letā€™s understand each one of these and when you relate it with the diagram, you will understand the concept more effectively.

Primary Partition

Definition: Primary partitions are the basic building blocks of a hard drive. These are the partitions where the operating system and essential files are typically installed.

A hard drive can have a maximum of four primary partitions. This limitation arises from the partition table format, particularly the Master Boot Record (MBR). The reason behind this is that, the partition table stores the metadata of each partition and total size it can store is 64 bytes and size of each metadata is 16 bytes.

Function: Each primary partition operates as an independent entity with its file system and purpose. For instance, you might have one primary partition for your operating system and another for important data.

In the diagram we can see that we have created three partitions of 1GB but to make more partitions we have to play a trick. We have configure the 4th partition as a new hard disk and that is known as Extended Partition.

Extended Partition

Definition: To overcome the limitation of only four primary partitions, the extended partition comes into play. It serves as a container, allowing you to create additional logical partitions within it.

There can be only one extended partition on a disk. The extended partition doesnā€™t hold data itself but acts as a boundary that houses logical partitions. It extends the capability to have more than four partitions on a disk.

Like in the diagram our 4th partition is our extended partition of size 7GB and being an extended or a kind of new hard-disk it also have partition table means it also reserved the initial space for partition table. And as we cannot store data directly in it so we have to create additional partitions inside it which is known as logical partitions.

Logical Partitions

Definition: Logical partitions exist within the extended partition and are essentially subdivisions of it. They provide a way to overcome the four-primary-partition limit.

There is no strict limit on the number of logical partitions you can create within an extended partition. As a limit in Linux to access partitions, a single disk drive allows maximum 15 logical partitions in RHEL 8.

Each logical partition within the extended partition functions similarly to a primary partition. You can install an operating system or store data in these logical partitions, just like in primary partitions.

STEPS

In order to successfully partition a disk and to make it useful, we need to ensure that we have completed the below four steps, regardless of the Operating system and Hardware of the system.

  1. Attach the disk to the proper port.
  2. Create partitions in the disk.
  3. Create a file system on the partition.
  4. Mounting the file systems

STEP: 1, Attach the disk to the proper port

We are going to use AWS EC2 Linux for showing this practical.

While launching the instance, we will add one more volume of 10GB.

STEP-2, Creating Partitions

ā†’ To see how many disk and partitions we currently have in our system, we will use this command.

We can see we have two disk one is primary disk that is attached for installing all system softwares. And second we have our disk drive that we have created while installation.

To manipulate partition inside disks, we have fdisk command.

To see the details of drives and paritions we use the command fdisk -l

To enter into the disk /dev/xvdb for creating partition, we will use fdisk <disk-name> command and for having info of that disk we have p command.

To create new partition in that disk, we have ā€œnā€ command.

We can see that the start of partition is from 2048 means that it had reserved some space for partition table.

Similarly we will create the 2 more partitions but after that you will reach your limit.

So for creating more partitions we have to create 4th one as a extended partition.

Now we can easily create more partition that is known as logical partitions and you will notice that those partitions are within the limit of these extended ones.

And also we can notice one more thing here that the starting sector of the logical partition is 6295552 which is 2048 sectors ahead of the start of extended sector 62935504. Hence we can conclude that the extended partition also reserved some space that 2048 sectors for storing partition table and some more metadata in it.

Donā€™t forget to press w before exiting from the disk, it will save all the changes we made.

So now our disk is ready with the partitions:

STEP- 2, Create a file system on the partition.

FORMAT

Formatting a partition is the process of creating or changing the file system on a disk. A file system is a way of organizing and storing data on a disk, such as FAT32, NTFS, or EXT4. It is a compulsory or mandatory step to be done after creating partitions.

When you format a hard drive partition, you delete all existing data and install a new file system on the partition, disk, or removable storage device. Formatting a partition will delete all data while maintaining the current partition of the drive.

But in reality, this is a myth lies in the market. Whenever we delete something or format the partition the data is never removed it is always present in the hard disk. The formatting of the partition cleans the inode table (a kind of index table) and our OS who is completely depends on inode table thinks that there is no data available in the drive.

But the data is as it is available inside hard disk and for this reason there is many recovery tool available in the market.

So after formatting the disk when we again start to put the data inside the drive, it will start overriding the data inside hard-disk.

The mkfs command is a command line tool that formats a disk or partition into a specific file system. The letters in mkfs stand for "make file systemā€. Inside mkfswe have many types of file system.

mkfs.ext4 <partition-name> 

Similarly we format the other partitions also to make it usable.

STEP 4, Mounting the file systems

The very next step after formatting the partition is to mount it with some folder as there is no way out there for connecting to partition directly and start storing data on them directly. In any OS for storing data, we have to create Folder and inside folder we have files and inside files we have our final data.

Hence we will mount it with the some directory but the data is actually storing inside partitions.

We will create some files for verifying what we have studied.

Now lets unmount the partitions and then mount it to some other directory and see what will happen.

We can see that the data is gone from that folder after unmount it from partition. Letā€™s again mount the partition but this time to some other directory.

After mounting the partition to some other directory the entire data that we have stored in the previous directory is now present inside this directory.

Hence this practical shows that the data is actually stored in partition but the medium is directory.

Conclusion

By visualizing the basics of Linux partitioning through diagrams, youā€™ve laid a solid foundation for efficient system organization. As you navigate your Linux landscape, remember that partitioning is the canvas on which your digital world unfolds.

This guide aims to illuminate this critical aspect, empowering users to navigate the Linux partitioning landscape with confidence. Whether youā€™re a beginner or an experienced sysadmin, mastering Linux partitioning opens doors to enhanced system performance and efficiency.

Ready to paint your Linux masterpiece? This visual journey is just the beginning. šŸ–ŒļøšŸ§

--

--