Written by the

Linux: Storage and Filesystems

Master Linux disk partitioning (MBR vs GPT), filesystem creation (ext4, xfs, btrfs), persistent fstab mounting, Logical Volume Management (LVM), swap space, software RAID with mdadm, and filesystem repair on Ubuntu 24.04 LTS.

8.1 Disks and Partitions

Linux abstracts physical storage hardware (SATA SSDs, NVMe drives, virtual disks) into block devices inside /dev. Dividing raw storage drives into separate partitions allows isolating system files, user data, and database logs.

8.1.1 Block Device Discovery: lsblk and blkid

Before partitioning or mounting storage volumes, inspect device identifiers:

Storage Inspection Utilities

  • lsblk:Displays a clean visual tree of all detected physical drives (e.g., sda, nvme0n1), partitions (sda1, nvme0n1p1), sizes, and active mount points.
  • lsblk -f:Adds filesystem format labels, types (ext4, xfs, swap), and unique UUID identifiers to the device tree.
  • sudo blkid:Directly queries block device attributes, printing device paths alongside their filesystem UUID, PARTUUID, and partition type.

8.1.2 Partition Schemes: MBR vs GPT

Partition tables dictate how sector boundaries and boot metadata are written to disk:

FeatureMaster Boot Record (MBR / DOS)GUID Partition Table (GPT)
Maximum Disk Capacity2 Terabytes (2.2 TB)9.4 Zettabytes (9.4 billion TB)
Partition LimitMaximum 4 primary partitions (requires extended partitions for more)128 primary partitions by default
Firmware SupportLegacy BIOSModern UEFI (with legacy BIOS fallback support)
Data RedundancySingle sector; single point of failure if corruptedBackup header stored at disk end with CRC32 checksums

8.1.3 Partitioning Tools: fdisk and parted

Partitioning Utilities

  • sudo fdisk /dev/sdb:Interactive partition editor. Modern fdisk versions support both MBR and GPT. Common menu commands: p (print partition table), n (create new partition), d (delete), t (change partition type), and w (write changes to disk).
  • sudo parted /dev/sdb:Advanced partition tool suitable for scripting and drives over 2TB. Example: sudo parted /dev/sdb mklabel gpt followed by sudo parted /dev/sdb mkpart primary ext4 1MiB 100%.
  • partprobe /dev/sdb:Instructs the Linux kernel to re-read the partition table without needing a system reboot.

8.2 Creating and Mounting Filesystems

A raw partition cannot store files until it is formatted with a filesystem. The filesystem organizes disk sectors into directory structures, allocation tables, and file metadata inodes.

8.2.1 Filesystem Comparison: ext4, XFS, and Btrfs

FilesystemDefault InCore StrengthsNotable Limitations
ext4Ubuntu, DebianMature journaling filesystem; supports shrinking and online growingFixed inode count chosen at format time; max volume 1 Exabyte
XFSRHEL, RockyHigh parallel I/O throughput; dynamic inode allocation; great for multi-terabyte filesCannot be shrunk; only extended
BtrfsFedora, openSUSECopy-on-Write (CoW); subvolumes; instantaneous snapshots; built-in RAIDHigher CPU/RAM overhead; parity RAID 5/6 write-hole edge cases

8.2.2 Formatting and Mounting: mkfs, mount, and umount

Filesystem Commands

  • sudo mkfs.ext4 /dev/sdb1:Formats partition with ext4 journaling filesystem.
  • sudo mkfs.xfs /dev/sdb2:Formats partition with high-performance XFS filesystem.
  • sudo mount /dev/sdb1 /mnt/data:Attaches filesystem to designated directory mount point.
  • sudo umount /mnt/data:Flushes dirty cache buffers to disk and unmounts the volume.
  • Device Busy Resolution (fuser / lsof):If unmounting fails with 'target is busy', identify which process is holding files open with fuser -mv /mnt/data or lsof +D /mnt/data.

8.2.3 Persistent Mounts with /etc/fstab

Manual mounts made with mount disappear upon system reboot. Permanent mount points are defined inside /etc/fstab using 6 structured fields:

Field #Field HeaderExample ValueDescription
1Device IdentifierUUID=e7c8b21...Universally Unique Identifier of the filesystem volume
2Mount Point/mnt/storageTarget directory where files will appear
3Filesystem Typeext4Filesystem format driver (ext4, xfs, btrfs, nfs)
4Mount Optionsdefaults,noatimeMount parameters (defaults: rw, suid, dev, exec, auto, nouser, async)
5Dump Frequency0Legacy dump utility backup flag (almost always 0 in modern systems)
6Fsck Pass Order2Boot check order: 1 for root (/); 2 for other partitions; 0 to skip check

fstab Testing Safety Rule

  • Always Test Before Rebooting:A single syntax typo or missing UUID in /etc/fstab drops the server into emergency maintenance mode during reboot. Always test your edits by running sudo mount -a. If this completes silently without errors, your fstab entries are valid.

8.2.4 Why UUIDs Outperform Device Names

Traditional device paths like /dev/sdb1 depend on disk enumeration order. If a storage controller detects a USB drive or secondary disk first during boot, /dev/sdb might swap with /dev/sda, attempting to mount the wrong partition. A UUID is written directly into the filesystem superblock and remains fixed forever.

8.3 Logical Volume Manager (LVM)

Standard partitions are rigid; extending them requires unmounting filesystems, deleting boundaries, and resizing tables. LVM inserts a virtualization layer between physical storage and filesystems, enabling dynamic online disk expansion.

8.3.1 The 3-Tier LVM Architecture

LVM Structural Tiers

  • 1. Physical Volume (PV):Raw block devices or partitions initialized for LVM (e.g., pvcreate /dev/sdb1 /dev/sdc1).
  • 2. Volume Group (VG):A unified storage pool pooling multiple Physical Volumes together (e.g., vgcreate data_pool /dev/sdb1 /dev/sdc1).
  • 3. Logical Volume (LV):Virtual partitions carved out of the Volume Group pool, formatted with a filesystem and mounted normally (e.g., lvcreate -L 50G -n web_lv data_pool).

8.3.2 Extending Volumes Online Without Downtime

When a database volume runs low on space, extend both the logical volume and its underlying filesystem in a single command:

Live Volume Expansion

  • Extend LV and Filesystem Together:sudo lvextend -L +20G -r /dev/data_pool/web_lv (the -r flag automatically calls resize2fs or xfs_growfs online while mounted).
  • Add New Drive to Existing Pool:sudo vgextend data_pool /dev/sdd1 immediately expands pool capacity.

8.3.3 LVM Snapshots

Point-in-Time Copy-on-Write Snapshots

  • Creating a Snapshot: lvcreate -L 5G -s -n db_snap /dev/data_pool/db_lv:Creates a frozen state of the database volume. Stores only modified blocks (CoW), allowing instant crash-consistent backups without stopping database writers.
  • Reverting to Snapshot: lvconvert --merge /dev/data_pool/db_snap:Rolls the volume back to the exact snapshot state if an application deployment or database migration fails.

8.4 Swap Space, Quotas, and Software RAID

8.4.1 Swap Files and Partitions

Swap provides virtual memory when physical RAM becomes exhausted, paging inactive memory pages to disk:

Creating a Swap File Step by Step

  • 1. Allocate 4GB Disk File:sudo fallocate -l 4G /swapfile
  • 2. Lock Permissions:sudo chmod 600 /swapfile (strictly restricted to root)
  • 3. Format as Swap:sudo mkswap /swapfile
  • 4. Activate Swap:sudo swapon /swapfile (verify with swapon --show)
  • 5. Swappiness Tuning (vm.swappiness):Controls kernel aggressiveness in paging to swap (0-100). Default is 60; production servers often set to 10 via sysctl vm.swappiness=10.

8.4.2 Enforcing User and Group Disk Quotas

Storage Quota Management

  • Mount Flags (usrquota, grpquota):Enabled in /etc/fstab on the target filesystem.
  • sudo edquota -u student:Opens editor to assign soft limits (grace period warnings) and hard limits (strict write blocks) on byte size and inode count.
  • sudo repquota -a:Generates an administrative summary report of quota usage across all users.

8.4.3 Software RAID with mdadm

The mdadm utility manages kernel-level software RAID arrays across multiple physical drives:

RAID LevelStrategyMinimum DisksFault ToleranceUse Case
RAID 0Block Striping2None (1 disk failure loses all data)Maximum speed for non-critical scratch caches
RAID 1Block Mirroring21 disk failure toleratedOperating system boot drives and small database servers
RAID 5Striping + Distributed Parity31 disk failure toleratedGeneral file storage balancing cost and redundancy
RAID 10Mirrored Stripes (1+0)4Tolerates up to 1 failure per mirror pairHigh-throughput production database environments

RAID Management Commands

  • Create RAID 1: sudo mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sdb1 /dev/sdc1:Constructs virtual mirror array device /dev/md0.
  • Monitor Status: cat /proc/mdstat:Displays live rebuild progress, active drives, and degraded status flags.

8.5 Checking and Repairing Filesystems

8.5.1 Filesystem Integrity: fsck and xfs_repair

Repair Best Practices

  • NEVER Run fsck on a Mounted Filesystem:Checking an actively mounted read-write filesystem corrupts directory trees. Always unmount first: sudo umount /dev/sdb1 && sudo fsck -y /dev/sdb1.
  • XFS Filesystem Repair:XFS utilizes specialized repair logic: sudo xfs_repair /dev/sdb2 (unmounted).
  • Lost and Found (/lost+found):Repaired files whose parent directory inode was destroyed are placed into /lost+found numbered by inode integer.

8.5.2 SMART Hardware Health Checks with smartctl

Hard drives and SSDs log internal hardware failure telemetry using SMART (Self-Monitoring, Analysis and Reporting Technology):

SMART Diagnostics

  • sudo smartctl -i /dev/sda:Verifies drive model, serial number, firmware revision, and SMART feature status.
  • sudo smartctl -H /dev/sda:Quick health assessment. Returns PASSED or warning alerts.
  • sudo smartctl -A /dev/sda:Inspects raw telemetry attributes: reallocated sector count (bad sectors remapped by disk controller), power-on hours, and SSD wear-leveling percentage.
  • sudo smartctl -t short /dev/sda:Triggers internal disk controller electrical and mechanical self-tests in background.

Knowledge Check: Module 8 Storage and Filesystems

1. Which partition scheme supports drives larger than 2 Terabytes?

2. Which configuration file defines permanent filesystem mount points at boot?

3. In LVM, what combines multiple physical storage devices together?

4. Why are UUIDs preferred over device names like /dev/sdb1 in fstab?

5. Which RAID level stripes data across drives without parity redundancy?

6. Which utility checks drive health and predicts disk hardware failures?