Partitioning in Linux

Do not miss this exclusive book on Binary Tree Problems. Get it now for free.

In this article, we discuss the concept of partitioning a drive, naming conventions for Linux partitions, types of partitions and demonstrate how to partition a drive in Linux using parted utility.

Table of contents.

  1. Introduction.
  2. Naming conventions.
  3. Types of partitions.
  4. Partitioning process.
  5. Summary.
  6. References.

Prerequisites.

  1. Linux file hierarchy
  2. Linux file system

Introduction.

Partitioning a disk is the first step before installing a file system since creation of any files or directories is done in a file system.

It involves dividing a HDD into smaller logical drives, therefore we can say that a partition is a contiguous set of block treated as an independent disk.
A partition table is an index which shows the relationship between the HDD and its partitions.

We partition a HDD for the following reasons,

  • Ability to run multiple OSs or multiple OS versions on the same disk.
  • Load balancing and distribution by managing processor allocations across applications ans system users.
  • Leveraging Capacity on demand and Pay As You Grow hardware models.

Naming conventions.

sd is used for SCSI disks although modern Linux systems will treat all disks as SCSI and therefore name them with starting with sd.

Examples

  • hda/sda - primary master.
  • hdb/sdb - primary slave.
  • hdc/sdc - Secondary master.
  • hdd/sdd - Secondary slave.
  • Others may be sde, sdf etc.

Each disk and partition in a Linux system is represented as a device special file and it is stored in the /dev directory. i.e
/dev/sda would represent the first disk, /dev/sdb the second etc.
Logical partitions inside an extended partition start at 5 i.e /dev/hda5 the first logical partition and /dev/hda6 the second and so on.

Types of partitions.

Different partitions will contain different file systems.

Primary partitions
This partition will store all operating system files. Only four primary partitions can be created.
For example the first primary partition will be labeled /dev/sda1.

Logical partitions
A primary partition may be partitioned again into sub-partitions, we refer to these as logical partitions.
Logical partitions allow us to go above the 4-partition limitation.
The primary partition housing logical partitions is referred to as an extended partition.
Logical partitions must be contiguous. Each logical partition will have a pointer to the next implying that we can have an unlimited number of logical partitions however Linux limits this where by we can only have 15 for SCSI and 63 for IDE.
These partitions start at 5, i.e /dev/sda5.

Swap partitions
Running processes are allocated a number of RAM blocks called pages. In-memory pages referenced by the processor in the near future are referred to as a working set.Linux predicts memory accesses by assuming that used pages will be used in subsequent accesses and therefore stores these pages in RAM.
If there are too many processes running, the kernel tries to free up space by writing the pages to disk.
Swap space is used to increase the amount of memory available although disk accesses are still very slow compared to RAM.
Swap space is important although it is no substitute to RAM.

Partitioning process.

Partitioning can be done with various different tools, they include, sfdisk, cfdisk, parted, partition magic or, disk drake.

Partitioning using parted.

Parted is a command line utility that allows users to easily manage hard disk partitions. With parted we can add, delete, extend or shrink disk partitions together with their corresponding file systems.

To get started type in the parted command.

parted

From the output we can see the version and the primary partition /dev/sda.
To exit we type *quit.

The help command while still in parted, will show all available options.

To view disk partitions while still in parted we write,

print

To view free partitions we write,

print free

To select a disk we want to partition we write,

select /dev/sdb

We then create a new partitioning table by using the mklabel command, but first lets look at the mklabel options.

help mklabel

From the output we can see that the disk label type must be among the listed ones.
For example lets create a msdos type partition,

mklabel msdos

You can use print command to view the created table.

To create a primary partition we use the mkpart command as follows,

mkpart primary

For a logical partition we write,

mkpart logical

We can also specify the file system and storage space as follows,

mkpart primary ext4 1MB 2048MB

The above command states that, create a new partition using the ext4 file system whose disk start is 1MB and end is 2048MB.

We can remove a partition using the rm command as follows,
First we have to know the partition number therefore we use th print command to list all partitions.

print

From the output, get the partition number in the Number column.
To remove a partition, write

rm

Then enter the partition number.

Summary.

In this article we have discussed how partitions are labeled in Linux, the various types of partitions and demonstrated how to partition a drive using the parted utility.

We have also talked about reasons why one would need to partition a disk.

References.

  1. Mounting and unmounting in Linux

Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.