Add a Swap File to RHEL/CentOS

Sometimes, due to some new specific server requirements, you will find it necessary to increase your swap space. Even if your swap partition is setup as a Logical Volume, your requirements may exceed what is available. This is where creating a new swap file is the best option. In this example, we are going to add a new 12GB swap file.

Check Current Swap Space

Verify the total amount of used and free physical and swap memory with the free command and the -h human-readable flag

# free -ht
              total        used        free      shared  buff/cache   available
Mem:           755G        321G        3.0G         62G        430G        670G
Swap:            4G          1G          3G
Total:         771G        322G          6G

Display the swap usage summary by device using swapon. Same as cat /proc/swaps

# swapon -s
Filename				Type		Size	Used	Priority
/dev/dm-1                              	partition	4194300	6772	-1

Create a New Swap File

In order to allocate space for our swap file, we will use the dd command. You may see some references to using fallocate but the manpage for swap warns against this

You should not use swapon on a file with holes.  This can be seen in the system log as

	swapon: swapfile has holes.

The swap file implementation in the kernel expects to be able to write to the file directly, without the assistance of the filesystem.  This is a problem on preallocated files (e.g.  fallocate(1)) on filesystems like XFS  or  ext4, and on copy-on-write filesystems like btrfs.

It is recommended to use dd(1) and /dev/zero to avoid holes on XFS and ext4.

Use dd to allocate a 12GB block. The count option is multiplied by bs in order to get the correct size.

# dd if=/dev/zero of=/swapfile bs=1K count=12M

Enable the Swap File for use

Configure the swap file to be usable with the mkswap command

# mkswap /swapfile

Set the file permissions to only be readable & writable by root

# chmod 600 /swapfile

Verify the Swap File

Make sure Linux recognizes the new swap space with free or swapon

# free -ht
              total        used        free      shared  buff/cache   available
Mem:           755G        321G        3.0G         62G        430G        670G
Swap:           16G          1G         15G
Total:         771G        321G         18G
# swapon -s
Filename				Type		Size	Used	Priority
/dev/dm-1                              	partition	4194300	6772	-1
/swapfile                              	file	12582908	0	-1

Make Swap Permanent

Add an entry within /etc/fstab to make sure the new swap file is persistent after reboot

# vi /etc/fstab

/swapfile 	swap 	swap 	defaults 0 0

Final Thoughts

There are a couple of options you may want to consider changing for your new swap file. The first is the Priority value. By default, if you have multiple swap areas, allocation use will round-robin between them. This may be inefficient for your application. This can be changed with the -p flag from swapon or set permanently in /etc/fstab using pri= option. Review the following manpages

# man swapon

-p, --priority priority
Specify the priority of the swap device.  priority is a value between -1 and 32767. Higher numbers indicate higher priority. See swapon(2) for  a full description of swap priorities. Add pri=value to the option field of /etc/fstab for use with swapon -a. When no priority is defined, it defaults to -1.
# man -s 2 swapon

Priority
Each swap area has a priority, either high or low. The default priority is low. Within the low-priority areas, newer areas are even lower priority than older areas.

All priorities set with swapflags are high-priority, higher than default. They may have any nonnegative value cho‐sen by the caller. Higher numbers mean higher priority.

Swap pages are allocated from areas in priority order, highest priority first. For areas with different priori‐ties, a higher-priority area is exhausted before using a lower-priority area. If two or more areas have the same priority, and it is the highest priority available, pages are allocated on a round-robin basis between them.

The second option to consider is the kernel value for swappiness. This will control how aggressive the kernel will swap memory. The default value is 60. Lowering this value will decrease the amount of swap. Review documentation for swappiness here https://www.kernel.org/doc/html/latest/admin-guide/sysctl/vm.html#swappiness

Verify the current setting

# cat /proc/sys/vm/swappiness 
60

To change this value on a running system, use the sysctl command

# sysctl vm.swappiness=30
vm.swappiness = 30

After verifying the setting is what you require, this option can be added to /etc/sysctl.conf to be maintained across reboots.

Please feel free to reach out with questions if any of the steps require more clarification.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s