How To Add Swap To A Running System



written by: nizar abed 01012004
nizar__AtN@Spam__srcget.com

Index

Preamble

Back to index

Every Operating System(OS) requires memory in order to run itself and the
programs it includes.Every standard machine comes with some amount of RAM,
Unfortunately the RAM is so expensive and it's imposible to depend only on it.
Modern OS's supports 'virtual memory'. In most OS's the virtual memory is
called swap,Linux(kernel) manages physical\virtual memory by itself ,Usually
it's configured at installation time as an individual partition and the recommended
swap size is twice the RAM amount.Linux(kernel) 2.2.x- and earlier has a swap size
limit of 128 MB, Fortunately, There is no 128 MB limit under linux(kernel) 2.2.x+
and later, There is a limit on the swap size but it's about 2 GB on x86 platforms.
It's possible to run linux without swap BUT it's highly recommended NOT to do that.

Determine the size of existing swap partition(s):

Back to index
more /proc/swaps
The above command will list all swaps currently in use.
example:
root AT lolitta (root) # more /proc/swaps
Filename                                Type            Size    Used    Priority
/dev/ide/host0/bus0/target0/lun0/part2  partition       1000432 0       -1

Make two additional swap files in /var/tmp:

as root:
cd /var/tmp
dd if=/dev/zero of=myswap1 bs=1024 count=1048576
dd if=/dev/zero of=myswap2 bs=1024 count=1048576

This will create two EMPTY files(myswap1,myswap2) in /var/tmp each 1GB in size.
Using dd ensures that the files have no holes.

Turn new swap into swap area:

Back to index
as root:
mkswap -c -v1 /var/tmp/myswap1
mkswap -c -v1 /var/tmp/myswap2
This checks each file for bad blocks(-c argument) and creates a new style swap
area(-v1 argument), [-v] argument default for new style, If you have an old
kernel (older than 2.1.117) use [-v0] for old style swap.

Turn on new swap files:

Back to index
as root:
swapon /var/tmp/myswap1
swapon /var/tmp/myswap2
This will enable the new swap files for paging and swapping.

Verify new swap space:

Back to index
more /proc/swaps

You should see that you have three swaps:
1- The original one.
2- myswap1.
3- myswap2.

The new two added swap files should look like:
Filename                        Type            Size    Used    Priority
/var/tmp/myswap1                file            1048568 0       -2
/var/tmp/myswap2                file            1048568 0       -3
swapon(2) for more details about Priority

Load new swap space at startup:

Back to index
The swap space usually activated from /etc/rc.d/rc.sysinit.
as root:
Open this file in a text editor, find the line that contains:
action "Activating swap partitions: " swapon -a -e

append the following to it:
/var/tmp/myswap1 /var/tmp/myswap2

The line should now look like:
action "Activating swap partitions: " swapon -a -e /var/tmp/myswap1 /var/tmp/myswap2

The [-a] option is to activate all devices marked as
swap devices in /etc/fstab, Devices that are already running
as swap are silently skipped.
The [-e] option(if doesn't exist then add it) makes swapon
silently skip devices that don't exist.

Alternatively we can activate the new swap files from
the file /etc/fstab by adding a single line for each swap file
we want to activate:
[path to swap] swap swap defaults 0 0





Comments and/or improvments, please tell me.

A hebrew translation for this document can be fond at http://www.penguin.org.il/guides/swap/