Wednesday, January 27, 2010

How to add more swap space on Linux

An easy method if you don't want to repartition the whole disk.

dd if=/dev/zero of=/tmp/swap.img bs=1M count=256

Set up a loop device and connect the dummy file with that device:

losetup /dev/loop3 /tmp/swap.img

Then, it's just formatting the loop device:

mkswap /dev/loop3

It also needs activation:

swapon /dev/loop3

Voilá, there is a new swap partition.

To remove it:

swapoff /dev/loop3
losetup -d /dev/loop3
rm /tmp/swap.img