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:
| Feature | Master Boot Record (MBR / DOS) | GUID Partition Table (GPT) |
|---|---|---|
| Maximum Disk Capacity | 2 Terabytes (2.2 TB) | 9.4 Zettabytes (9.4 billion TB) |
| Partition Limit | Maximum 4 primary partitions (requires extended partitions for more) | 128 primary partitions by default |
| Firmware Support | Legacy BIOS | Modern UEFI (with legacy BIOS fallback support) |
| Data Redundancy | Single sector; single point of failure if corrupted | Backup 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), andw(write changes to disk). - sudo parted /dev/sdb:Advanced partition tool suitable for scripting and drives over 2TB. Example:
sudo parted /dev/sdb mklabel gptfollowed bysudo 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
| Filesystem | Default In | Core Strengths | Notable Limitations |
|---|---|---|---|
| ext4 | Ubuntu, Debian | Mature journaling filesystem; supports shrinking and online growing | Fixed inode count chosen at format time; max volume 1 Exabyte |
| XFS | RHEL, Rocky | High parallel I/O throughput; dynamic inode allocation; great for multi-terabyte files | Cannot be shrunk; only extended |
| Btrfs | Fedora, openSUSE | Copy-on-Write (CoW); subvolumes; instantaneous snapshots; built-in RAID | Higher 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/dataorlsof +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 Header | Example Value | Description |
|---|---|---|---|
| 1 | Device Identifier | UUID=e7c8b21... | Universally Unique Identifier of the filesystem volume |
| 2 | Mount Point | /mnt/storage | Target directory where files will appear |
| 3 | Filesystem Type | ext4 | Filesystem format driver (ext4, xfs, btrfs, nfs) |
| 4 | Mount Options | defaults,noatime | Mount parameters (defaults: rw, suid, dev, exec, auto, nouser, async) |
| 5 | Dump Frequency | 0 | Legacy dump utility backup flag (almost always 0 in modern systems) |
| 6 | Fsck Pass Order | 2 | Boot 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/fstabdrops the server into emergency maintenance mode during reboot. Always test your edits by runningsudo 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-rflag automatically calls resize2fs or xfs_growfs online while mounted). - Add New Drive to Existing Pool:
sudo vgextend data_pool /dev/sdd1immediately 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 withswapon --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/fstabon 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 Level | Strategy | Minimum Disks | Fault Tolerance | Use Case |
|---|---|---|---|---|
| RAID 0 | Block Striping | 2 | None (1 disk failure loses all data) | Maximum speed for non-critical scratch caches |
| RAID 1 | Block Mirroring | 2 | 1 disk failure tolerated | Operating system boot drives and small database servers |
| RAID 5 | Striping + Distributed Parity | 3 | 1 disk failure tolerated | General file storage balancing cost and redundancy |
| RAID 10 | Mirrored Stripes (1+0) | 4 | Tolerates up to 1 failure per mirror pair | High-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+foundnumbered 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?