justlinux.com
Mon, 23-Nov-2009 09:30:37 GMT
internet.com
Forum: Registered Users: 73495, Online: 258
nhfs Here you can view your subscribed threads, work with private messages and edit your profile and preferences Registration is free! Calendar Find other members Frequently Asked Questions Search Home Home

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.


internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
internet.commerce
Be a Commerce Partner











internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs