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.
  • 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:-

sudo fdisk /dev/sdX

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.

Step3 : Now, Write changes:

    • Press w to write the partition table.

Creating MBR Extended and Logical Partitions

Step1 : Create an extended partition first :

    • For this, Use fdisk and press n.
    • Select e for an extended partition.

Step2 : Now, add/create logical partitions:

    • Within the extended partition, we create multiple logical partitions by pressing n and selecting logical.

Step3 : Write desired changes:

sudo partprobe  # Inform the kernel of partition changes

Managing GPT Partitions

Step1 : Use parted command

sudo parted /dev/sdX

Step2 : Create a new GPT desired disk label

(parted) mklabel gpt

Step3 : Create partitions type and size

(parted) mkpart primary ext4 1MiB 10GiB

Step4 : To verify the partition table

sudo parted /dev/sdX print

Working with SSD

For 0ptimizing SSD Usage

Step1 : Use the fstrim command to enable TRIM(help the built-in garbage collection)

sudo fstrim -av

Step2 : Choose Filesystem

    • Use ext4 or xfs, as they handle SSDs efficiently.

Step3 : Mount options:

    • Add discard or use fstrim command for TRIM support.

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 or parted command to create a new partition.
      • Set the partition type to swap (code 82 in MBR).

Step2 : Format the created partition

sudo mkswap /dev/sdX2

Step3 : Enable/Active the swap partition

sudo swapon /dev/sdX2

Step4 : Make it persistent

      • Add it to /etc/fstab
        /dev/sdX2 none swap sw 0 0

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

sudo apt install cryptsetup  # Debian/Ubuntu
sudo yum install cryptsetup  # RHEL/CentOS

Step2 : Initialize the partition for encryption

sudo cryptsetup luksFormat /dev/sdX1

Step3 : Open the encrypted partition

sudo cryptsetup open /dev/sdX1 my_secure_partition

Step4 : Format the partition

sudo mkfs.ext4 /dev/mapper/my_secure_partition

Step5 : Mount the encrypted partition

sudo mount /dev/mapper/my_secure_partition /mnt

Step6 : Make it persistent

Add an entry in /etc/crypttab

my_secure_partition /dev/sdX1 none luks

Update /etc/fstab:

/dev/mapper/my_secure_partition /mnt ext4 defaults 0 
The above command and process ensure a robust understanding of disk partition management, enabling us to configure and secure our disk storage effectively.

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

Loading

Categories: Unix/Linux OS

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.