DEV Community

Heshan Dharmasena
Heshan Dharmasena

Posted on

Linux Disk Partitions

Available partition types

Primary Partition: The main partitions (maximum up to 4 in MBR).

Extended Partition: Used to create multiple logical partitions (in MBR).

Logical Partition: Inside an Extended Partition explained above (in MBR).

GPT Partitions: Used in modern systems, supports 128 partitions (in GPT).

Swap Partition: Used for virtual memory (disk storage as memory).

Lets learn about these words.

When we connect a disk (storage) to a linux server/host, linux server identifies it as a file (In Linux world everything is a file).

Here I am not going to talk about root disk (/dev/vda is the root disk). So I am assuming a use case, we need a additional storage called new Disk of 1GB. You can execute lsblk command to monitor attached disks.

First disk identified as a /dev/sda (a means first), second goes like /dev/sdb (b is second), and goes likewise..

/dev means, storage detects as a device file, all device files goes under /dev directory). sdx usually identified type of the disk (here I assume it is SATA type)

You can not use disk soon after it plugged. There is a way how linux identified this as a usable disk. This process has 3 steps.

1.Partition
2.Format
3.Mount

First you need to plug/attach the disk, Then you need to do the Partitioning. After partition you can Format it. Then only Mount is possible.

Partition Schemes

MBR and GPT are two types of partitioning schemes used to manage partitions on a disk. Behaviour of partitioning and number of partitions and type of partitions different from each schemes.

MBR (Master Boot Record)

MBR is very old partition scheme use by linux /linux based systems. MBR scheme disk can only have maximum of 4 primary partitions, or 3 primary + 1 extended (with maximum of 128 logical partitions inside). MBR scheme can only partition up to 2TB of disk size.

Lets talk about primary partition, extended partition and logical partitions.

Primary Partition

Lets say you want to mount newly attached disk to server. You need to create 2 partitions (in simply think C drive and D drive in Windows).

We can create 2 partitions easily using Primary Partition mechanism. But if we use Primary Partitions, we have to limit maximum of 4 primary partitions. You can not create more than 4 primary partitions.

Let’s say you create 1st partition (/dev/sdb1) size of 200MB, 2nd partition (/dev/sdb2) size of 200 MB, 3rd partition (/dev/sdb3) size of 200MB. Since you already created 3 primary partition, next partition will be last primary partition. You have left ~400MB on hand (1GB — 600MB). Hence last partition size should be 400MB or less (if it you select 200MB, rest of 200MN will be waste). So remember, you need to utilise remaining all space if you’re going with 4th primary partition. Otherwise it will be waste.

Extended Partition

This is not a partition type to use. This is register space as a primary partition to use for Logical Partitions. However when we create a extended partition, it will allocate small amount of space to store local partition. table data.

Logical Partition

Logical partition/s is/are the real partitions that we can create from Extended Partitions. Unless you create any Logical Partitions, you can not make use of Extended Partition even that capacity is reserved under Extended Partition. So the point is, you only need to create Extended Partition if you want to to create Logical Partitions. And why you want Logical Partitions, is because you want to create more than 4 Partitions from that disk (which is not possible with Primary Partitions).

But there is a limit of creating Logical partitions under Extended partition. It is 128, so you can go up to 128 Logical Partitions under Extended Partition.

/dev/vda is the first disk attached to the system.

/dev/vda1 is the primary partition from /dev/vda disk.

/dev/vda2 is the extended partition (we can’t use it space).

/dev/vda5 is the logical partition created under the /dev/vda2 extended partition. Likewise you can go up to 128 logical paritions.

GPT (GUID Partition Table)

Is the modern scheme UEFI of partitioning. Supports up to 128 partitions (there is no concept like primary, extended or logical). GPT scheme supports up to 9.4 ZB size of disks. This required for UEFI boot; modern OS’s like Windows 10+, Linux, macOS.

SWAP Partition

Swap is virtual memory (physical space of disk) use when the physical RAM (memory) is full.

Swap runs slower than actual RAM memory, but recomeded to use when you are running with. high memory-intensive applications to preventing crashes when memory is exhausted

Top comments (1)

Collapse
 
heshand profile image
Heshan Dharmasena