Introduction

  • Managing partitions in Linux is a critical system administration task that involves dividing physical storage devices into logical sections called partitions.
  • Proper partition management ensures efficient disk usage, better performance, improved security, and reliable system operation.
  • 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

  • In Linux, storage devices such as hard disks and solid-state drives are treated as files and are usually located in the /dev directory.
  • Storage devices in Linux are represented by device files.
  • Disk devices are commonly named
    • HDD/SSD:/dev/sda, /dev/sdb for SATA, SCSI, and USB disks
    • /dev/nvme0n1 for NVMe(Non-Volatile Memory Express) SSDs
  • Each partition on a disk is represented by a number
    • Partitions: /dev/sda1, /dev/sda2, etc.
  • Linux uses file systems like ext4(Fourth Extended File System), xfs(X File System), and btrfs(B-tree File System) to organize data on partitions. 
  • Storage types in Linux are –
    • 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[New Technology File System]) as needed.
  • The common partition types used are:-
    • Primary Partition: Up to 4 partitions in MBR(Master Boot Record) in old systems.
    • Extended Partition: A container for logical partitions (MBR) in old systems.
    • Logical Partition: Partitions within the extended partition in old systems.
    • EFI(Extensible Firmware Interface) System Partition (ESP): Required for UEFI(Unified Extensible Firmware Interface) boot (GPT) in modern systems.

MBR and GPT Partitions

MBR (Master Boot Record)

  • MBR is a Legacy partitioning scheme, which means that MBR is an older partitioning scheme used on traditional BIOS-based systems or earlier computer systems, but has limitations compared to modern methods. The partitioning system was designed for older hardware and firmware (BIOS-based systems). It is still supported today for compatibility reasons. It has technical limitations that newer schemes have improved upon.
  • MBR is an old, still usable, but largely replaced by more advanced solutions.
  • MBR has several characteristics:
    • Supports a maximum disk partition size up to 2 TB.
    • This stores partition data in a single sector (prone to corruption).
    • Allows a maximum of 4 primary partitions.
    • One primary partition can be an extended partition.
    • Extended partitions can contain multiple logical partitions.
    • Stores partition information in the first sector of the disk.
    • It lacks advanced features like redundancy and error checking.
    • It is not suitable for modern UEFI (Unified Extensible Firmware Interface) systems.

    GPT (GUID Partition Table):

    • A Globally Unique Identifier (GUID) is a 128-bit unique value used to identify objects uniquely across systems and networks.
    • In the context of disk partitioning, GUIDs are used in GPT (GUID Partition Table) to uniquely identify each partition and disk.
    • GUIDs ensure that every partition has a unique identity, which helps in better partition management, reliability, and compatibility with modern systems.
    • GPT (GUID Partition Table) is a modern partitioning scheme that overcomes these limitations and is used in newer systems.
    • GPT is a modern partitioning scheme used with UEFI systems.
    • GPT characteristics are :
      • Supports disks larger than 2 TB
      • Allows up to 128 partitions
      • Stores multiple copies of partition tables for reliability
      • Uses globally unique identifiers (GUIDs)
      • Provides better data integrity than MBR

      Creating MBR Partitions

      • MBR partitions are commonly created using the fdisk utility.

      Step1 : Use the fdisk command first by the administrator as:-

      $ sudo fdisk /dev/sda

      Step2 : Now, create a new partition using the following:

        • Press n to create a partition.
        • Select the primary partition first, and then the extended partition we want to create.
        • Now, specify the partition number and size as needed.

      Step3 : Now, save changes:

        • Press w to write/save the partition table.
        • After partition creation, a file system is created using commands like mkfs.ext4. The partition is then mounted to a directory.
      Now, Creating MBR Extended and Logical Partitions
      • MBR allows only four primary partitions. Therefore, to overcome this limitation, an extended partition is used.
      • An extended partition –
        • Acts as a container for logical partitions
        • Counts as one of the four primary partitions
      • Logical partitions –
        • They are created inside the extended partition
        • Can be many in number of desired size.
      • Partition Created Process –

      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 inside extended partitions:

        • Within the extended partition, we create multiple logical partitions by pressing n and selecting logical options.
        • Each logical partition is formatted and mounted separately after its creation.

      Step3 : Write desired changes:

      $ sudo partprobe (press enter)    # Inform the kernel of partition changes

      Managing GPT Partitions

      • GPT partitions are managed using tools such as gdisk or parted.

      Step1 : Select the disk and use the parted command to create a partition

      $ sudo parted /dev/sda

      Step2 : Creating new partitions with specific sizes with a new GPT desired disk label

      (parted) mklabel gpt

      Step3 : Create a partition type and size using GUIDs and write/save changes to disk 

      (parted) mkpart primary ext4 1MiB 10GiB

      Step4 : To verify the partition table

      sudo parted /dev/sdX print
      Finally, GPT supports advanced features such as partition labels and backup partition tables, making it more reliable than MBR.

      Working with SSD

      • SSD devices differ from HDDs because they use flash memory instead of spinning disks.
      • Partition alignment is essential for performance in SSDs. 
      • TRIM support helps maintain SSD speed and lifespan. Linux supports TRIM using the fstrim command.
      • Modern Linux file systems like ext4 and xfs automatically optimize for SSDs.

      For Optimizing 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. Swap improves system stability under heavy memory load.
      • 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 using mkswap

      $ sudo mkswap /dev/sdX2

      Step3 : Enable/activate the swap partition using swapon

      $ sudo swapon /dev/sdX2

      Step4 : Make it persistent using /etc/fstab for permanent activation.

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

      Encrypted Partitions

      • Encrypted partitions protect sensitive data by converting it into an unreadable format without proper authentication.
      • 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, which is used for this purpose.
      • Encryption ensures data confidentiality and compliance with security standards.
      • Encrypted partitions provide strong security without affecting normal system usage significantly.
      Configuring Encrypted Partitions

      Steps to Encrypt a Partition Using LUKS:

      Step1 : For this, we require a passphrase or key file. The partition is encrypted using cryptsetup luksFormat, i.e., 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 : The encrypted partition is opened using cryptsetup open

      sudo cryptsetup open /dev/sdX1 my_secure_partition

      Step4 : Format the partition. A file system is created on the encrypted device. 

      sudo mkfs.ext4 /dev/mapper/my_secure_partition

      Step5 : Mount the encrypted partition to a directory

      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.