Dec 25 2008
Creating a swap file under Linux
Last week Lucas my co-worker Lucas noticed that in one of our servers didn’t have a swap partition, until now things were going ok because server didn’t have much RAM usage, but now servers is getting more traffic each day and sometimes free ram gets extremely low.
In a normal situation the process here should be, reboot the server, and using gparted or similar create a small amount of free space and allocate that as a swap partition. Fortunately this not needed nowadays, since Kernel 2.6 swap file performance has increased dramatically so the possible benefits from having a dedicated swap partition against a swap file are so small that in some situations it is preferable to have a swap file in case you wan’t to increase / decrease it’s size.
The process for building this kind of swap files is very simple:
# dd if=/dev/zero of=/home/swap bs=1M count=1024
Replacing 1024 with the number of megabytes you want will change the swap file size.
Now that we have the size we want on disk we can prepare it for usage as a swap partition:
# mkswap /home/swap
Now the swap file has being built you must introduce the following line in /etc/fstab file so this partition get’s mounted once the system is rebooted:
/home/swap swap swap defaults 0 0
And finally to test the swap file you should type, which would activate all the partitions marked as “swap” in /etc/fstab
# swapon –a
Now finally we can check that the swap partition has being activated:
# free -m
total used free shared buffers cached
Mem: 2028 1719 308 0 132 1119
-/+ buffers/cache: 468 1559
Swap: 1023 0 1023
Finally there are two useful commands you should be aware if you are playing around with swap files:
# swapon | swapoff
This commands activate and deactivate the usage of swap file.
No responses yet






