I recently ran out of swap space on one of my production application servers, and needed to add some additional swap on the fly. Since I didn’t have a spare slice free on the server, I created a 1GB file on my / file system with dd, and then used the mkswap and swapon utilities to create a swap device out of that file:
$ dd if=/dev/zero of=/swap1.swp bs=1024 count=512K
$ mkswap /swap1.swp
$ swapon /swap1.swp
To verify the new swap device was available, I dumped /proc/swaps:
$ cat /proc/swaps
Filename Type Size Used Priority /dev/hda2 partition 522104 160 -1 /swap1.swp file 1048568 0 -2
Sizing swap is easy to do, but when a server changes roles, previous swap estimates no longer come into play. I am planning to kickstart the server with a different disk layout, which will allow me to allocate a block device of the right size to swap. For the interim, this met our needs.