Google

Creating a swap file

Creating a swap file

Home: www.packetnexus.com

How To Add A Swap File

First you need to set aside a chunk of your hard drive to use as the swap
file. You must also decide on a name for the file. Generally /swap is pretty
descriptive. You must issue the dd command:

dd if=/dev/zero of=/opt/swap bs=1024 count=1024000

The if=/dev/zero parameter writes data from a special device file called
/dev/zero. The of=/swap is the name of the swap file. You do not need to
create the file beforehand. The other two parameters tell dd how much space
you want for the swap file. The bs parameter tells dd the block size of each
"chunk" in bytes in our case we used 1024 or 1K chunks. The final parameter
tells how many of these "chunks" we want reserved. Since we chose 10240 that
is a total of 10Mb of swap space.

We are not done yet. We have to tell Linux about our new swap file and turn
swapping on. Issue the following command to turn the new /swap file into a
real swap file.

mkswap -c /opt/swap 1024000

This is fairly explanatory, tell mkswap the filename and the size.  Now
issue
the sync command:

sync

Finally we turn on swapping.

swapon /opt/swap

There, finished, it wasnt that hard was it?  To see if your new swap file is
in action issue the free command:

free

This will list your swap space and you should have 1023Mb more than before.

Your new swap file is only temporary, it will disappear next time you
reboot.  If
you want it to be permanant then you must edit your /etc/fstab file and
add the swap file to it.  Its not as good as a whole partition, but a bit
easier
to add.

Add this to /etc/fstab

/opt/swap               swap                    swap    defaults        0 0

Do the following the set the rights on the file.

chmod 755 /opt/swap


Back to the Index