Swap Partition Priority in Linux

Swap Partition Priority in Linux
Page content

In the Linux community there are often disagreements over what desktop environment is the best, who puts out the best distro, to name but a few flame wars. One thing everyone will agree on is that the Linux operating system is very flexible and adaptable. One lesser-known example of this is that Linux can handle multiple swap spaces. For computers with limited resources or where for some reason more ram memory is needed but ram sticks can’t be installed, up to thirty-two swap spaces can be added under the 2.4 and later kernels.

If you use multiple swap files and partitions to boost the virtual memory, you’ll want to prioritize which spaces are used first.

Setting Swap Priority

Once you have your swap partition or file created, use the mkswap command to set it as swap.

mkswap /dev/sda3 or mkswap /filename

To enable the swap space until your next reboot, use swapon. This command must be run as root.

sudo swapon /dev/sda3 or sudo swapon /filename

Check that it was added by typing swapon -s:

Notice that the priorities for the two swap spaces are negative. This is the default setting. Since -1 is a higher number than -2, the system will swap to /dev/sda3 before /swapfile. By default the priorities are assigned in the order that the swap spaces are added, if done one at a time like we did. If instead you type sudo swapon -a, all swap spaces are prioritized in the order listed in /etc/fstab.

It’s easy to change the swap partition priority in Linux. To give the swapfile a priority of 60, type:

sudo swapoff /swapfile (to make changes you must first disable /swapfile)

sudo swapon -p 60 /swapfile

Typing swapon -s now gives us:

swapfile priority

Configuring fstab

That is the way our swap spaces will be used until the next reboot. Setting the priority using swapon is nice for testing different swap configurations, but once we know they’ll work we’ll want to make the changes permanent. To do this, we’ll edit /etc/fstab.

Fstab is a text file that tells the system how to automatically mount and use file systems. It also handles swap spaces. Changes must be made as the root user, such as typing sudo gedit /etc/fstab in a terminal. However, be careful! Make a backup of fstab first by typing sudo cp /etc/fstab /etc/fstab-bkup.

fstab

Once you have /etc/fstab open as the root user, locate the swap space line you want to change. Add pri= followed by a number from 0 to 32767 after the sw option. To give my /swapfile a priority of 60, the line will look like this:

/swapfile none swap sw,pri=60 0 0

Now, viewing my swap space by typing swapon -s gives me:

higher priority

Organizing Your Swap Spaces

Swap is always slower than a stick of RAM, therefore you most likely want to give your fastest devices the higher priority. In the past, swap files were slower than swap partitions, but with the 2.6 kernel the speed is the same. The only consideration is if you use hibernation to put your computer to sleep. A swap partition is needed to write information about the system at the time of starting hibernation so that when it is booted back up it can restart where it left off.

Resources

An excellent overview of swap space on Linux-Tutorials.info

mkswap man page

swapon man (2) page

swapon man (8) page

How to edit and understand /etc/fstab