Monday, March 31, 2008

Dynamic Swap on File

1. Create a 500MB swap file.

$ dd if=/dev/zero of=/mnt/WinD/Swap500M bs=1024 count=500000
$ mkswap /mnt/WinD/Swap500M



2. Change variables according to your configuration (or liking) in
the following Shell script (my_swapon.sh).

#!/bin/sh

## Turns on swap ##

SWAP_PARTITION="/mnt/WinD"
SWAP_DEV="$SWAP_PARTITION/Swap500M"
if [[ -e $SWAP_DEV ]]
then
echo "$SWAP_DEV is already mounted."
else
mount $SWAP_PARTITION
fi

IS_ON=`grep -c $SWAP_DEV /proc/swaps`

if [ $IS_ON -eq 1 ]
then
echo "$SWAP_DEV is already turned on."
else
swapon $SWAP_DEV
fi


3. Run this script as root user whenever you want to
use extra swap space. (Above steps 1 and 2, need
to be done only once).

$ sudo ./my_swapon.sh