How To Add Swap To Linux (Filesystem Swap)

In the old days (10 to 15 years ago), raw device swap was preferable. This is where you created a disk partition and specified it as a “swap”.

But with the advances in hard drive technology, OS technology, and the like, filesystem swap is almost just as good and it is much easier to manage and change since you are not making any partition table changes.

So I always recommend filesystem swap than raw device swap when adding swap space. Per the instructions above I will usually create swap to be 4gig when I create the OS if I do not know what will be on the server.
SWAP

The most common reason you will need to do this: if you have maxxed out your memory and your system is crying for swap space (otherwise it may run out of virtual memory and won’t be able to fork any new processes)


Sponsored Links

The other common reason, at least for me, is if Oracle server is later installed on this system and swap needs to be increased after the fact per Oracle’s installation requirements

(As an aside, Oracle pre-installation documentation ROCKS! Oracle has strict guidelines before it will install including filesystem size, kernel paramaters, software installation, and more. Oracle’s docs are VERY easy to read and give you details on how to accomplish the requirements. You learn a wide variety of sysadmin tasks very quickly by going through Oracle’s documents. Just google “Oracle install on [operating system]” to find it at oracle.com)


Sponsored Links

Here are the steps to create your Linux swapfile

1) usually create it on the “root” drive. First make sure there is enough space
# df -h /

2) i create a directory (arbitrary name)
# mkdir /swapfiles

3) now create an empty file. In this case I created one that was roughly 13gig (1 gig extra). Swapfile name is arbitrary, count is 13,000,000 blocks

# cd /swapfiles
# dd if=/dev/zero of=swapfile13gig bs=1024 count=13000000

4) make it able to be used as swapspace

# mkswap /swapfiles/swapfile13gig

5) create an /etc/fstab entry

You can copy the current swap space line (something like this, depends on OS)

/dev/disk/by-id/scsi-blahblahblahblah-part2 swap swap defaults 0 0

And create a new one (copy) and change accordingly:

/swapfiles/swapfile13gig swap swap defaults 0 0

6) turn on all swap spaces in /etc/fstab

# swapon -a

7) check current swap space (using top or whatever command you want)

You’ll see that the swap space has now grown from 4gig to 16.8gig

Magic Computer
Sometimes voodoo won’t work – you’ll need to add swap space

You’ll also see that you can create the swap space as you like in increments. Instead of one 13gig file you could have created a 5gig file, a second 5gigfile, and a third 3gigfile. Creating filesystem swap gives you a lot of versatility in swap management

As another aside – that “dd” command is very useful if you ever need to create large files quickly for any reason (e.g. needing to copy large amounts of files to test resource usage)

Good luck have fun!

, Linux

Leave a Comment

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

Spam protection by WP Captcha-Free