23 August 2007

Adjusting your swap files

I have a small Xen server which wouldn't start up with a swap partition. Whenever I add it in, it gives a strange error. Something which Ill have to figure out one of these days.

However its not a big deal, because its easy to set up a file based swap file in linux anyway.

First thing is to create the file to allocate disk space for it, for this case I just need a 500MB swapfile:

# dd if=/dev/zero of=swap.file bs=1M count=500
# chmod 600 swap.file
# mkswap swap.file
Setting up swapspace version 1, size = 524283 kB


Next is to make sure that it gets used at each bootup.
# cat >> /etc/fstab
/mnt/data/swap/swap.file none swap sw 0 0


but if you need it now, you just use 'swapon':

# free | grep swap
Swap: 0 0 0
# swapon /mnt/data/swap/swap.file
# free | grep swap
Swap: 511992 0 511992


and if you do a top,
top - 17:13:31 up 1 day, 6:57, 1 user, load average: 0.02, 0.03, 0.00
Tasks: 53 total, 2 running, 51 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 257024k total, 251808k used, 5216k free, 1036k buffers
Swap: 511992k total, 0k used, 511992k free, 183108k cached


Nice swap!

You can dynamically change the size of your swap files using swapon / swapoff combos.

yk.