Quick tutorial on adding filesystem swap on linux and unix systems. I am doing the actual work on a test linux system here. The procedure is the same for unix also. Will also cover a few common questions… like “What is swap anyhow?”!
For the impatient..add filesystem swap in 9 easy steps
- top to find out how much swap you currently have and help you calculate how much you need
- df to find a filesystem (or filesystems) with enough combined free disk space for the swap you want to add.
- mkdir [directoryname] create a directory for your swapfile, just my own convention to keep things neat
- dd if=/dev/zero of=[filename] bs=1024 count=[N] create an empty file for your swap, N = # of kbytes
- mkswap [filename] make it swappable
- cp -p /etc/fstab /etc/fstab.`date +”%Y%m%d-%H%M%S”` create a backup of your fstab file with date and timestamp in the filename. Don’t forget the “-p” option to preserve file timestamp
- vi /etc/fstab add a line to /etc/fstab for your new swapfile
- swapon -a swap on all swap areas in /etc/fstab. I like to do this because it ensures it will work on boot. Do NOT use this if you are deliberately turning some swap areas in your /etc/fstab off since it will turn them back on again. I add that just for completeness though it is improbable it will apply to anyone.
- top confirm swap is there
And now the details… for adding filesystem swap to linux and unix
Questions to answer:
What is swap?
Swap, also known as “virtual memory”, is where processes go when they are not being worked on by the CPU.
When a process is currently being worked on, it is in active memory or RAM. When it is not worked on, it is “swapped out” to disk where it waits its turn to be worked on by the CPU. When the CPU is ready for it, it takes the process from the swap space and puts it back into RAM
What is filesystem swap vs raw device swap?
There are two types of swap space which I will call: raw device swap and filesystem swap.
Raw device swap
is when you partition some space on a drive (e.g. /dev/sdb1) or an LVM, format it as swap space, then configure the OS to use that space as swap space. Typically, raw device swap is configured during the OS install. It can be added after OS install, however it can get a bit convoluted if you end up needing to repartition your existing hard drive to create space
”faster”, at least in the old days, not so much anymore, but theoretically faster
more difficult to add “after the fact” for regular devices
more difficult to add “after the fact” if all disk space has already been partitioned/allocated whether regular device or LVM
is when you create an empty file on the filesystem (e.g. /swapfiles/2gigfile), mark it as swappable, then configure the OS to use that as swap space. Filesystem swap is always added after OS install, naturally since the OS needs to be up for you to create filesystems
Easy to add, can be added on the fly
Can be added in any type of increment you wish: 1gig, 5gig, etc.
“slower”, at least in the old days, not so much anymore, but theoretically slower.
NOTE on “speed”: Filesystem swap is theoretically slower because, naturally, there is more overhead and logic to swap to it than to a raw device. That being said, with improvements in disk access technology and OS optimizations in disk access technology, combined with the fact that accessing swap itself regardless of its type is a bottleneck that most servers seek to avoid, it is a general consensus for years that the speed difference for all intents and purposes theoretical
What is a good convention for adding swap?
If you’re following along, you can see that the advantages of filesystem swap far outweigh the advantages of raw device swap.
In the “old” days convention was, disk space willing, create swap equal to 2x the amount of RAM. If that was not achievable, then at least equal to the size of RAM. The logic was that if there was ever a crash, the OS could dump core easily if the swap was at least equal to the size of RAM.
I would say this is an old Unix convention that is slightly outdated. I have never used these core files AND this is also when memory in systems never went above 8G.
You do need at least SOME amount of raw device swap when you configure the OS so it can at least boot and operate at some baseline level.
My convention is to add 2gig or 4gig of raw device swap just depending on how much RAM is in the system. If there is 8gig or less RAM, I will go with 2gig of swap. If there is more than 8gig, I will go with 4gig of swap, assuming there is enough disk space.
Even still, if you for some reason only configured 2gig of swap on a high memory system, it’s not the end of the world. You can probably watch the system and note that it will perform fine. And if not you can always add filesystem swap later
Why would I need to add filesystem swap?
Two main reasons
- You find the system is suffering from not having enough swap space. Normally, it is likely the system is actually needing a RAM upgrade, not a swap space upgrade. A swap space upgrade is more like a band-aid for systems that are performing near its max resources and a memory upgrade is what is needed
- The system is going to have Oracle installed. Oracle is VERY picky about having proper swap space in place for it to install. Whether it actually uses it or not is up to debate by many sysadmins and oracle Gurus.
RAM between 1GB and 2GB, then SWAP = 1.5x RAM
RAM between 2GB and 16GB, then SWAP = 1x RAM
RAM over 16GB, then SWAP = 16GB
NOTE: this does not necessarily mean raw device swap. This is total swap. I would still configure this system this way:
- 2GB to 4GB raw device swap on OS install
- Remaining “balance” filesystem swap post-OS install, pre-Oracle install
How do I add filesystem swap?
So with all that in mind, how do you add filesystem swap?
1. Determine how much swap you have and how much you need
On our example system TESTLINUX01, there is about 20G ram and 2G swap. Oracle is to be installed, so using the above Oracle convention I am going to add an additional 14G swap. You can pull this information at the top of “top” (ha ha “top” of “”top””)
TESTLINUX01:~ # top
Mem: 19814M total, 9906M used, 9908M free, 161M buffers
Swap: 2054M total, 0M used, 2054M free, 9137M cached
2. Find filesystems with at least 16G combined space free
Generally the root disk will have enough space free. There will not be any other apps which means a lot of free disk space, even the old systems with 73gig drives should have enough space free.
TESTLINUX01:~ # df -h /
Filesystem Size Used Avail Use% Mounted on
/dev/sds3 272G 15G 257G 6% /
Create the swapfile
My convention is to create a directory called “swapfiles” in the filesystem I will be using. In this case, I am using the “/” disk so I will create the swapfile in “/swapfiles”.
TESTLINUX01:~ # mkdir /swapfiles
TESTLINUX01:~ # cd /swapfiles
To create the file, you will use the “dd” command to do a copy. As input you will use /dev/zero (null file) and as output you will create an arbitrarily named file. My convention is to name the file so it is obvious what it is using size and the word “swap”
TESTLINUX01:/swapfiles # dd if=/dev/zero of=/swapfiles/14gigswapfile bs=1024 count=14000000
14000000+0 records in
14000000+0 records out
14432000000 bytes (14 GB) copied, 123.941 s, 149 MB/s
TESTLINUX01:/swapfiles # ls -l /swapfiles/14gigswapfile
-rw-r--r-- 1 root root 14432000000 Mar 14 10:14 /swapfiles/14gigswapfile
Make file swappable
Use the “mkswap” command
TESTLINUX01:/swapfiles # mkswap /swapfiles/14gigswapfile
Setting up swapspace version 1, size = 13999996 KiB
no label, UUID=03c94951-be6e-4303-9abf-6b085d6674d6
Create an entry in /etc/fstab so the OS knows it can use it
First, create a backup of the file ALWAYS ALWAYS ALWAYS. I use a date and timestamp most of the time in the filename. And ALWAYS use the “-p” option to preserve the modification time of the file.
TESTLINUX01:/swapfiles # cp –p /etc/fstab/etc/fstab.201306-140300
A fun habit to get into is to use the backticks on the command line. So for the above you could also do
TESTLINUX01:/swapfiles # cp –p /etc/fstab/etc/fstab.`date+"%Ym%d-%H%M%S"`
Now add this into /etc/fstab file
/swapfiles/14gigswapfile swap swap defaults 0 0
Swap on all devices in /etc/fstab
This way I know it will work on bootup
TESTLINUX01:/swapfiles # swapon -a
Verify using “top” that swap is now increased
TESTLINUX01:/swapfiles # top
Mem: 19814M total , 19498M used, 316M free, 140M buffers
Swap: 15633M total , 3M used, 15629M free, 14755M cached