Partitioning is essential for organizing disk storage in Linux. It allows us to efficiently allocate space for the operating system, swap, data storage, and other purposes.
Disk Storage and Devices
- Storage devices in Linux are represented by device files:
- HDD/SSD:
/dev/sda
,/dev/sdb
, etc. - Partitions:
/dev/sda1
,/dev/sda2
, etc.
- HDD/SSD:
- Storage types in Linux:
- HDD (Hard Disk Drives): It is a spinning disk part and is slower.
- SSD (Solid-State Drives): It is faster, with no moving parts, unlike HDD.
- NVMe (Non-Volatile Memory Express): It is a high-speed SSD and an advanced form of SSD.
Tools/Commands for Disk Management
- fdisk: Partition disks using MBR(Master Boot Record) or GPT(GUID Partition Table).
- parted: Partition disks, especially GPT.
- lsblk: List block devices and their partition layouts.
- blkid: View UUIDs(Universal Unique Identifier) and filesystem types of partitions.
Partition Disk Layout
- A disk can be logically divided into several partitions, each must be formatted with a filesystem (e.g., ext4, xfs, NTFS) as needed.
- The common partition types used are:-
- Primary Partition: Up to 4 partitions in MBR.
- Extended Partition: A container for logical partitions (MBR).
- Logical Partition: Partitions within the extended partition.
- EFI System Partition (ESP): Required for UEFI(Unified Extensible Firmware Interface) boot (GPT).
MBR and GPT Partitions
MBR (Master Boot Record):
- This is a Legacy partitioning scheme.
- Limitations:
- This partition supports up to 4 primary partitions.
- The maximum disk partition size is 2 TB.
- This stores partition data in a single sector (prone to corruption).
GPT (GUID Partition Table):
- This is a modern partitioning scheme used with UEFI systems.
- Advantages:
- This partition supports up to 128 partitions (no primary/extended distinction).
- This partition handles disks larger than 2 TB.
- This partition has redundant partition tables for reliability.
Creating MBR Partitions
Step1 : Use the fdisk command first as:-
Step2 : Now, Create a new partition using the following:
-
- Press
n
to create a partition. - Select the primary or extended partition we want to create.
- Now, choose the partition number and size as needed.
- Press
Step3 : Now, Write changes:
-
- Press
w
to write the partition table.
- Press
Creating MBR Extended and Logical Partitions
Step1 : Create an extended partition first :
-
- For this, Use
fdisk
and pressn
. - Select
e
for an extended partition.
- For this, Use
Step2 : Now, add/create logical partitions:
-
- Within the extended partition, we create multiple logical partitions by pressing
n
and selecting logical.
- Within the extended partition, we create multiple logical partitions by pressing
Step3 : Write desired changes:
sudo partprobe # Inform the kernel of partition changes
Managing GPT Partitions
Step1 : Use parted
command
Step2 : Create a new GPT desired disk label
Step3 : Create partitions type and size
Step4 : To verify the partition table
Working with SSD
For 0ptimizing SSD Usage
Step1 : Use the fstrim
command to enable TRIM(help the built-in garbage collection)
Step2 : Choose Filesystem
-
- Use ext4 or xfs, as they handle SSDs efficiently.
Step3 : Mount options:
-
- Add
discard
or usefstrim
command for TRIM support.
- Add
Adding a Swap Partition
- A swap partition in Linux is a section of a hard disk that stores data when the computer‘s RAM is full. It’s also known as virtual RAM.
-
A swap partition is a feature in Linux that provides virtual memory space. Thus, it allows the OS to handle memory demands efficiently, improving system stability, responsiveness, and heavy workload processing.
Steps to Add Swap Partitions:
Step1 : Create a swap partition
-
-
- Use
fdisk
orparted
command to create a new partition. - Set the partition type to
swap
(code82
in MBR).
- Use
-
Step2 : Format the created partition
Step3 : Enable/Active the swap partition
Step4 : Make it persistent
-
-
- Add it to
/etc/fstab
- Add it to
-
Encrypted Partitions
- Encryption ensures that unauthorized users cannot access data stored on a disk.
- A common encryption tool is LUKS (Linux Unified Key Setup), an industry-standard disk encryption.
Configuring Encrypted Partitions
Steps to Encrypt a Partition Using LUKS:
Step1 : Install cryptsetup
Step2 : Initialize the partition for encryption
Step3 : Open the encrypted partition
Step4 : Format the partition
Step5 : Mount the encrypted partition
Step6 : Make it persistent
Add an entry in /etc/crypttab
Update /etc/fstab
:
Summary of Commands Used in Managing Disk Partitions
Tasks | Commands/Tools |
View disks and partitions | lsblk , fdisk -l , parted |
Create MBR partition | fdisk /dev/sdX |
Create GPT partition | parted /dev/sdX |
Add swap partition | mkswap , swapon , edit /etc/fstab |
Encrypt partition | cryptsetup luksFormat |
Open encrypted partition | cryptsetup open |
Optimize SSD | fstrim -av , use ext4/xfs |
0 Comments