Help File Library: Fly Swapping
Written By:
test
A little Background
Fly swapping or as it should be called, Swap on-the-Fly allows the
addition of swap space as and when needed. This is very handy if you have
just ran out of Memory while trying to do a big compile or, more often,
you mis-configured your swap at installation and ran out of memory before
even X starts.
Though this process is by no means a solution to the problem (there are
speed implications) it is non the less a nice quick fix, and I have used
this method on many Unix variants.
How do we do it
Well first we must be root to do this, the we need to use the command "dd"
to create a swap file, this is not too bad.
Decide how much more swap you need, but dont worry too much since you can
add multiple swap files.
Lets take an example that we need 20Mb more of swap, so first we execute
the command :-
"dd if=/dev/zero of=/usr/local/newswap bs=1000000 count=20"
This will go and create a file of 20Mb called newswap in the /usr/local
directory and it will be filled with zero's.
We must next format the file to accept being used as a swap file, first we
must set its permissions so everyone on the system can use it, use the
command :-
"chmod 600 /usr/local/newswap"
Now we actualy apply a file system to the file (bit of a strange concept,
but hey ;) ), use the command :-
"mkswap /usr/loca/newswap"
Nearly done now, all that needs to be done is to mount it, this can be
achieved useing this command :-
"swapon /usr/local/newswap"
And with any luck if you type swapon -s you should see the file listed...
You can have this swap file, and others, mounted during bootup, by
editing your /etc/fstab and under the current
swap mounting add
"/location/to/swapfile swap swap defaults 0 0"
Don't forget to change "/location/to/swapfile"
to the actual location of the swapfile, such as /usr/local/newswap.
If at any time you wih to stop useing this swap file, just issue the
command (as root) "swapoff
/location/to/swapfile"
And now a little script to automate the process
#!/bin/sh
if [ `id | awk '{print$1}' | tr "()" " " | awk '{print$2}'` != root ];
then
clear
echo "You must be logged in as root to build images."
exit 1
fi
dd if=/dev/zero of=/usr/local/${1}-megs bs=1000000 count=$1
chmod 777 /usr/local/${1}-megs
mkswap /usr/local/${1}-megs
swapon /usr/local/${1}-megs
# Remove the next line to NOT auto mount at bootup
echo "/usr/local/${1}-megs swap swap defaults 0 0" >>
/etc/fstab
if swapon -s | grep -q ${1} ; then
echo "Fly Swap was sucessfull"
echo
fi
This will allow you to excute the command "fly-swap.sh 20" and get the
same results as the example. The parameter is the number of megs you
want.