Creating and using swap partitions on Linux

Rate this post

69 views

Linux

Overview

Swap space represents an area on the physical disk (usually a dedicated partition) which holds temporarily a process memory image. This area is often called virtual memory because it allows processes to use memory beyond the physical RAM available on your computer. Swapping and paging algorithms allow processes or portions of processes to move between physical memory and a mass storage device. This frees up space in physical memory.

As the swap space is stored on the disk the access times tend to be slow compared to the RAM. In Linux we can have two types of swap space: a dedicated partition on the physical disk or a swap file which resides among other files on the filesystem. Linux kernels newer that 2.4.10 allow 32 swap areas.

Preparing the swap space

The first question what comes into our mind when we prepare the swap space is “How much swap do I need ?”. In most cases it’s recommended that the swap space should be twice the amount of physical memory (RAM). So for example if you have 2GB of RAM you should create a swap partition or file of 4GB. This is mostly recommended on desktop computers. Servers which have huge amounts of RAM may not need that much swap space.

If you didn’t create a swap partition when you installed the operating system, you can add one by using the mkswap command. First make sure you have some unallocated space or an unused partition using the fdisk -l command. If that is not the case we will create the swap space in a file which will be discussed later.

fdisk -l /dev/sd[x]

where [x] is the letter of the unused partition. This command will return the size and type of the partition like below:

Device          Start       End         Blocks             Id        System
/dev/sda7        1017      1204       1510078+             82        Linux Swap / Solaris

If the partition ID isn’t set to 82 which coresponds to the Linux Swap/Solaris filesystem you must use fdisk utility in interactive mode and change the partition ID using the “t” menu option.

fdisk /dev/sda7

After setting the partition ID use the “w” option to commit the changes and exit fdisk. Be careful that changing a partition’s ID is a destructive operation and all data on that partition will be lost. Assuming all these operations completed with success the next step is to set up the swap space using the mkswap command. To run this command you need root privileges.

mkswap /dev/sda7

The mkswap command can be used also to check the partition for bad blocks before creating the swap area. For this we need to use the “-c” option of the mkswap command.

mkswap -c /dev/sda7

Please be advise that this is a time-consuming operation depending on the partition size. Also we can specify a label using the “-L” option to the mkswap command which is mostly useful for partition identification and permits mounting the swap space in /etc/fstab by specifying this label. At this point we are ready to activate the swap space by using the swapon command.

swapon /dev/sda7

To make sure the swap partition will be available at the next reboot we need to add and entry in the /etc/fstab file. This way the partition will be mounted automatically without any user intervention. An fstab entry for the swap partition will look similar the the below:

/dev/sda7        none        swap        sw        0        0

Adding swap to a file

If you don’t have a free partition which can be used for swap, we can create a file which will behave as a regular swap partition. For instance if we need 1GB additional space for swap, we can use the dd (disk dump) command to initialize a file like in the following example:

dd if=/dev/zero of=/mnt/swapfile bs=1M count=1024

which will display

1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 3.6827 s, 292 MB/s

This command will create a swap file named here “swapfile” in the /mnt folder and it will fill its contents with zeroes. After this we must prepare the swap space using the mkswap command but instead of the device partition we will specify the location of the swap file:

mkswap /mnt/swapfile

Finally the swap file must be activated by using the swapon command.

swapon /mnt/swapfile

Now the swap file is ready to use. If we need the swap file to be available all time add a fstab entry as we did previously. When activating the swap file we might get a warning which inform us that the swap file has insecure permission (0644). In order to resolve this we need to change the permission on the file using chmod command.

chmod 600 /mnt/swapfile

This command will ensure that only the user who created the file (usually root) will have read and write access to the file.

Displaying swap space information

In order to view the currently used swap space we could use interrogate the proc filesystem or use the “swapon -s ” command.

cat /proc/swaps

Both command have identical output similar to the below:

Filename                        Type            Size    Used    Priority
/dev/sda7                       partition       1510068 1292     -1
/mnt/swapfile                   file            1048568 0        -2

Each line from the output represents a separate swap partition or swap file denoted here by the Type column. The Size column is self-explanatory. The Used column shows how much space from the swap is currently in use. The display unit is in kilobytes so here we have almost 1.3 MB of swap used. The last columns reveals the priority of each swap space and determines which is used first. The priority is a value between 0 and 32767. A higher priority (high number) indicates that swap pages are allocated from areas in order of priority. Priorities can also be specified in /etc/fstab file by adding a pri=value entry to the option field (4th column).

Disabling swap areas

There are situations when you can disable the swap area for instance if you have a server with large amounts of memory. In order to do this you must use the following command:

swapoff -v device

where device is the name of the swap partition or file that you wish to disable. For example, to disable swapping on the device /dev/sda7, use the command:

swapoff -v /dev/sda7

The “-v” parameter displays some additional information about the operation. When the “-a” flag is given, swapping is disabled on all known swap devices and files (as found in /proc/swaps or /etc/fstab). If you have disabled a swap partition using swapoff, you are free to reuse that partition as you see fit: remove it using fdisk, or do whatever.

Tweaking swap parameters

In Linux 2.6 kernels a new kernel parameter called swappiness was added. This controls how much the kernel transfers processes from physical memory into the swap. Swappiness can have a value of between 0 and 100. A higher value means more pages will be swapped to disk and a lower value means that more pages are kept in physical memory. The default value for swappiness in most Linux distributions is 60. In order to display the current value use the following command:

cat /proc/sys/vm/swappiness

In order to modify the swappiness value temporarily use the following command:

echo 30 > /proc/sys/vm/swappiness

To make this change permanently edit the /etc/sysctl.conf manually and modify or add the following line with the desired value.

vm.swappiness = 30

You can also run the following command to update the sysctl.conf file.

sysctl -w vm.swappiness = 30

All these changes will be preserved across a reboot. In order to make this changes you need root privileges.

Summary

Using swap files has some benefits like allowing old system with little RAM to expand its memory and some drawbacks regarding the access speed compared to physical memory. Overall taking into consideration all of the benefits of using swapping we can tweak a system to run at maximum performance.

Leave a Reply

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