Creating a swap file on a Linux server

  • 7019235
  • 09-Jun-2016
  • 07-Aug-2017

Environment


Retain 4
SLES 11

Situation


My swap is only 2G and the index migration process is bogging down because the indexer wants more RAM.  If I cannot add an additional swap disk, how can I create a swap file?

Resolution


The swap disk is ideal, but if it cannot be created for some reason, here are the steps to creating a file that gets converted to being used as swap memory.  These instructions are based off of this Internet article:  https://www.maketecheasier.com/swap-partitions-on-linux/

1.  Find a volume that has the available disk space.

2.  Create the file that will be converted to swap:

dd if=/dev/zero of=/[path to swapfile]/[swapfile name] bs=1024 count=[total byte size of the file]

Example of creating a 1G swap file called "swapfile":  
dd if=/dev/zero of=/vol2/swap/swapfile bs=1024 count=1048576

Example of creating a 100G swap file called "swapfile":  
dd if=/dev/zero of=/vol2/swap/swapfile bs=1024 count=104857600

3.  Make the file a swap file:

mkswap /[path to swapfile]/[swapfile name]

Example:  mkswap /vol2/swap/swapfile

4.  Turn on swap for that file:

swapon /[path to swapfile]/[swapfile name]

Example:  swapon /vol2/swap/swapfile

5.  Add the new swapfile to fstab so that it automatically becomes swap memory upon bootup:

The entry in /etc/fstab should look like this:

/[path to swapfile]/[swapfile name]           none     swap     sw     0     0

Example:  /vol2/swap/swapfile          none     swap     sw     0     0

Additional Information

This article was originally published in the GWAVA knowledgebase as article ID 2809.