Adding Swap Space to a Live Rocky Linux System
Introduction
This article demonstrates how to safely add additional swap space to a Rocky Linux system while it's running and actively using existing swap. This procedure is non-disruptive and doesn't require a system reboot or affect running processes.
Problem
A system administrator needs to increase available swap space on a server that's currently using its existing swap partition. The system has 2 GB of swap space configured as an LVM partition and requires an additional 10 GB of swap capacity without interrupting any active processes or services.
Resolution
Create a Swap File
Instead of modifying existing partition sizes, create a new swap file on the root filesystem. This approach avoids the complexity of resizing LVM volumes and can be done while the system is running.
Create the swap file using fallocate
sudo fallocate -l 10G /swapfile_10gb
Set appropriate permissions
sudo chmod 600 /swapfile_10gb
Format the file as swap
sudo mkswap /swapfile_10gb
Enable the new swap file
sudo swapon /swapfile_10gb
This command activates the swap file immediately without affecting your existing swap partition or any running processes.
Verify both swap spaces are active
sudo swapon --show
Expected output should show:
- Your original swap partition (e.g.,
/dev/mapper/rl-swap- 2.0GB) - Your new swap file (
/swapfile_10gb- 10GB) - Total: 12 GB of swap space
You can also verify with:
free -h
Make the Change Persistent
To ensure the system activates the swap file automatically after reboots, add an entry to /etc/fstab:
echo '/swapfile_10gb none swap defaults 0 0' | sudo tee -a /etc/fstab
Reload systemd configuration
sudo systemctl daemon-reload
Notes
Swap Files vs Swap Partitions
Both swap files and swap partitions provide equivalent performance on modern filesystems. Swap files offer several advantages:
- No need to modify partition tables or LVM volumes
- You can create, resize, or remove them without downtime
- Multiple swap files can coexist with swap partitions
- Easier to manage on systems with complex storage configurations
Filesystem Compatibility
Rocky Linux typically uses XFS for the root filesystem, and both XFS and ext4 fully support non-sparse swap files created with fallocate. Modern kernels treat a properly allocated swap file the same as a swap partition for reliability and performance. Using fallocate offers advantages over older dd if=/dev/zero examples because it's faster and avoids writing unnecessary zeroes.
Note: Advanced or copy-on-write filesystems, for example Btrfs, can require extra steps such as setting the "no copy-on-write" attribute before creating a swap file. This isn't a concern for default Rocky Linux XFS or ext4 deployments and is outside the scope of this article.
Swap Priority
By default, the kernel uses swap spaces with equal priority. If you need to prioritize one swap space over another, you can specify the pri option in /etc/fstab, for example:
/swapfile_10gb none swap defaults,pri=10 0 0
The kernel uses higher priority values first. Priority values range from -2 (lowest, the default) to 32767 (highest). By default, if no priority is specified, the kernel assigns a priority of -2.
References & related articles
Rocky Linux Documentation Filesystem - swapon swapoff How to Limit Tmpfs for Taking Swap Space