Mastering Linux Storage (Part 3)

Modernizing Linux Swap with Dynamic Swap Files

In Part 2 of this series, we dynamically expanded our QEMU .qcow2 image, triggered a live kernel rescan, and redefined our partition sector boundaries in fdisk to claim 50 GB of unallocated disk space.

To accomplish that without complex block migration, we intentionally deleted our secondary swap partition (/dev/vda5), which was standing in the way of our root filesystem expansion.

In this final installment, we will complete our storage migration by replacing the rigid, old-school swap partition with a modern Linux Swap File and updating our system boot tables in /etc/fstab.

1. Swap Partitions vs. Swap Files: Why the Paradigm Shift?

Historically, Linux installations allocated a rigid, dedicated disk partition (e.g., /dev/vda5 or /dev/sda2) exclusively for swap space. While this was necessary decades ago for HDD performance optimization, modern Linux kernel memory management (kernel 2.6 and newer) treats swap files living on block-aligned filesystems with the exact same performance efficiency as dedicated partitions.

The Problem with Dedicated Swap Partitions

  • Rigid Boundaries: A swap partition acts as a physical wall on your disk layout, blocking adjacent partitions from contiguous expansion.
  • Complex Resizing: Changing the size of a swap partition requires shrinking or moving adjacent partitions—a high-risk operation.

The Swap File Advantage

  • Flexibility: A swap file is simply a regular file residing inside your main root (/) filesystem.
  • Dynamic Sizing: Need to increase your swap space from 2 GB to 8 GB? You can delete, resize, or recreate a swap file in seconds without touching disk partitions or rebooting.

2. Step-by-Step: Creating and Activating a Swap File

Now that our main /dev/vda1 ext4 partition spans the full 90 GB of available disk storage, we will allocate a replacement 2 GB swap file inside it.

Step 1: Pre-allocate Storage Space

Use fallocate to instantly reserve block space on disk without write overhead:

Bash

sudo fallocate -l 2G /swapfile

Note: If your filesystem is xfs or doesn’t support fallocate, fall back to dd: sudo dd if=/dev/zero of=/swapfile bs=1M count=2048

Step 2: Restrict File Permissions

For system security, swap space must be strictly readable and writable only by the root user (0600). Exposing swap files to world-read permissions allows local processes to inspect raw kernel memory pages.

Bash

sudo chmod 600 /swapfile

Verify permissions with ls -l:

Bash

ls -lh /swapfile

Plaintext

-rw------- 1 root root 2.0G Aug 28 14:00 /swapfile

Step 3: Format and Initialize Swap Structure

Write the swap header metadata to the newly allocated file using mkswap:

Bash

sudo mkswap /swapfile

Plaintext

Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=a1b2c3d4-e5f6-7890-abcd-1234567890ef

Step 4: Activate Swap Memory

Tell the Linux kernel to start using the new swap file:

Bash

sudo swapon /swapfile

Verify that your new virtual memory is active using swapon or free:

Bash

sudo swapon --show

Plaintext

NAME      TYPE SIZE USED PRIO
/swapfile file   2G   0B   -2

3. Cleaning Up /etc/fstab for Persistent Boots

Because we deleted the old /dev/vda5 partition in Part 2, your system’s filesystem table (/etc/fstab) still contains a stale reference or missing UUID for the old swap drive.

If you reboot without fixing /etc/fstab, the system will stall for up to 90 seconds during boot searching for a non-existent device UUID!

Step 1: Open /etc/fstab

Open the mount table in your text editor:

Bash

sudo nano /etc/fstab

Step 2: Remove the Legacy Entry

Look for any line referencing swap or the old partition ID (e.g., /dev/vda5 or UUID strings pointing to swap):

Plaintext

# UNWanted Old Entry:
UUID=3d12a45b-6789-4012-abcd-ef0123456789 none swap sw 0 0

Comment out or delete that line entirely.

Step 3: Add the New Swap File Entry

Append the following line to the bottom of /etc/fstab to automatically mount your new swap file on boot:

Plaintext

/swapfile none swap defaults 0 0

Save and exit (Ctrl+O, Enter, then Ctrl+X in nano).

4. Tuning Swappiness (Optional Pro Tip)

The vm.swappiness kernel parameter controls how aggressively your Linux system moves memory pages from RAM to swap (ranging from 0 to 100).

  • For desktop/dev workloads: A swappiness value of 10 or 60 works well.
  • To check your current swappiness:

Bash

cat /proc/sys/net/ipv4/ip_forward /proc/sys/vm/swappiness

To set swappiness to 10 temporarily:

Bash

sudo sysctl vm.swappiness=10

To make it permanent, add vm.swappiness=10 to /etc/sysctl.conf.

Conclusion: The Complete Storage Transformation

Across this 3-part series, we transitioned our Linux system through a complete storage modernization pipeline:

  1. Diagnosed storage limitations using df, du, and decoded contiguous sector boundaries with fdisk.
  2. Expanded QEMU block storage on the host and performed live kernel rescans without dropping the running VM.
  3. Re-engineered partition boundaries safely around ext4 data signatures.
  4. Modernized memory management by replacing rigid disk partitions with dynamic swap files.

By understanding how block devices, partition maps, and filesystems interact under the hood, you can confidently manipulate system storage, debug cloud server issues, and maintain high-availability infrastructure like a seasoned systems engineer.

chevron_left

Leave a comment

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

Comment
Name
Email
Website

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