HOWTO: Add a Swapfile at Boot
A while back I outfitted my personal workstation with 4GB of RAM. That’s plenty of memory and since disk space was tight I didn’t want to waste any on a swap partition, and I lived happily without swap for a very long time until I began using more virtualization. (I freed up space on my physical desk top by migrating my work environment from a laptop to a virtual machine on my personal workstation.)
I wrote a script to add a “temporary” swap file on demand but what I wanted was a swap file at boot. That actually turns out to be pretty simple. In this case, I just prepare the swap file:
FILE=/mnt/swapfile SIZE=8388608 # 8 GB dd if=/dev/zero of=$FILE bs=1024 count=$SIZE mkswap $FILE $SIZE swapon $FILE swapon -s
Then, to make it stick, add this line to /etc/fstab
just as you would for a swap partition:
/mnt/swapfile none swap sw 0 0
This is a win for the Unix everything-is-a-file philosophy.
See Also: Ubuntu Community Swap FAQ