Disk Partitioning, File Systems, and Swap Memory Management in Linux

Disk Partitioning, File Systems, and Swap Memory Management in Linux

What is Disk Partitioning

Disk partitioning involves dividing a physical disk into separate logical sections. Each section, or partition, acts as an independent unit where a file system can be created. Once a partition is created, a file system is built on top of it and mounted to make it accessible.

Why Partition a Disk?

  1. Disk Space Efficiency: Partitioning allows better allocation of disk space for various uses.

  2. Data Encapsulation: If something happens to one partition, other partitions remain unaffected, improving data safety.

  3. Limit Data Growth: Partitioning can prevent uncontrolled data growth by setting limits on each partition.


Disk Partitioning Schema

The MBR (Master Boot Record) Partitioning Schema is a traditional partitioning system, limiting a disk to four primary partitions. However, one of those can be an extended partition, which can hold multiple logical partitions.

  • Partitioning Example:

    • 3P: Three primary partitions

    • 2P + 1E: Two primary partitions and one extended partition

    • 1P + 1E: One primary partition and one extended partition

A primary partition is a basic partition that can hold data, it is created on a hard disk (secondary memory). As the name implies, an extended partition extends the partition table and can hold multiple logical partitions, which are containers for the actual data.


Disk Identification

  1. IDE (Integrated Drive Electronics): /dev/hds

  2. NVME (Non-Volatile Memory Express): /dev/nvme0n1

  3. SCSI (Small Computer System Interface): /dev/sda

  4. Virtual Drive: /dev/vda


Types of File Systems

FilesystemDescriptionJournalingMax Individual File SizeMax Filesystem Size
ext2Second Extended Filesystem.No16GB to 2TB32TB
ext3Third Extended Filesystem.Yes16GB to 2TB32TB
ext4Fourth Extended Filesystem.Yes16GB to 16TB-
xfsHigh-performance 64-bit journaling filesystem.Yes-8 exbibytes

Viewing Partitions

  • fdisk -l

  • parted -l

  • lsblk


Adding a New Disk

First, add a hard disk of SCSI type of a specific disk size and create

Newly added disks do not reflect automatically and require a system reboot. To avoid rebooting:

echo "- - -" > /sys/class/scsi_host/host0/scan
  • Explanation: The three dashes act as wildcards (channel, SCSI target ID, LUN). LUN is essentially a way to refer to a specific storage volume or device

  • Scan for all hosts in /sys/class/scsi_host/:

Scan Script

#!/bin/bash

# List of all hosts
hosts="host0 host1 host2 host3 host4 host5 host6 host7 host8 host9
host10 host11 host12 host13 host14 host15 host16 host17 host18 host19
host20 host21 host22 host23 host24 host25 host26 host27 host28 host29
host30 host31 host32"

# Base path to the SCSI host directories
base_path="/sys/class/scsi_host"

# Loop through each host and perform the scan
for host in $hosts; do
    echo "Scanning $host..."
    echo '- - -' > "$base_path/$host/scan"
done

echo "Scan complete for all hosts."

Partitioning with fdisk

  1. Start the partition administration:

     fdisk /dev/sdb
    
  2. Create 2 Primary Partitions and 1 Extended Partition.

Primary Partition Creation

  1. Both of the same size.

Logical Partition Creation

  1. Create 2 Logical Partitions of equal size within the Extended Partition.

  2. Save partition information.

Update Partition Table

Use the following command to update the partition table without restarting:

partprobe /dev/sdb

Verify Partitions

lsblk /dev/sdb1

fdisk -l

parted -l


Formatting a Partition

Use ext4 filesystem for formatting:

mkfs.ext4 /dev/sdb1


Mounting

  1. Temporary Mounting:

     cd /
     mkdir /data
     mount /dev/sdb1 /data/
    
    • Temporary mount information is stored in /etc/mtab.

  1. Permanent Mounting: Add an entry in /etc/fstab:

    When we reboot the system, the above mount will get unmounted it’s a temporary mount.

    To make it permanent mounting we need to make its entry add an entry in /etc/fstab file.

     /dev/sdb1 /data ext4 defaults 0 0
    

    We can also add the UUID instead of the device name:

     UUID=123e4567-e89b-12d3-a456-426614174000 /data ext4 defaults 0 0
    
    • Find UUID:

        blkid /dev/sdb1
      
  2. /etc/fstab fields:

     Field               | Description
     --------------------|--------------------------------------------------------
     Device Name/ID      | The device or partition being mounted (e.g., /dev/sda1, UUID=xxx, LABEL=xxx).
     Mount Point         | The directory where the device is mounted (e.g., /mnt/data, /home).
     File System Type    | The type of file system (e.g., ext4, xfs, nfs).
     Mount Options       | Options for how the file system is mounted (e.g., rw, ro, noexec).
     Dump                | Used by the dump utility, usually 0 (no dump) or 1 (dump enabled).
     Sequence Check      | Used by fsck to check the file system order during boot (e.g., 0 or 1).
    

Unmounting

  1. Temporary unmount:

     umount /data/
    
  2. Verify unmount:

     df -h
    

    As it is unmounted currently, but when we reboot the system the directory will get mounted again as we have an permanent entry in /etc/fstab file.

    mount -a : mounts all the stuff in /ect/fstab


Deleting a Partition

  1. Start fdisk:

     fdisk /dev/sdb
    
  2. Use the d command to delete the partition and w to save changes.

  3. Update partition table:

     partprobe /dev/sdb
    

  4. Verify:

     lsblk
     parted -l
    


Troubleshooting

  1. You cannot unmount the directory if you are in it:

     cd /data/
     umount /data
    

  2. Cannot unmount if another user is making changes:

     fuser -cu /dev/sdb1    # To check which useres are using the partition
     fuser -ck /dev/sdb1    # To remove the users from working on the partition
    

Swap Memory and Space

Swap mem is a dedicated hard disk partition used as an extension for mem (RAM), when ram gets full the system moves less active data pages to the swap space on the hard disk to free up ram for new processes.

Swap Memory Calculation

  1. If RAM < 2GB: Swap = 2 × RAM Size.

  2. If RAM > 2GB: Swap = RAM Size + 2GB.

Commands

  • Check help:

      swapon --help
    

Creating Swap Partition

  1. Change Partition ID:

     fdisk /dev/sdb
    
    • Use t to change the partition type.

    • Save changes and update the partition table:

        partprobe /dev/sdb
      
    • Verify

  2. Format as Swap Filesystem:

     mkswap /dev/sdb2
    

    Verify:

     parted -l
    
  3. Activate Swap Space:

     swapon /dev/sdb2
    

    Verify:

     swapon -s
    

  4. Deactivate Swap Space:

     swapoff /dev/sdb2