Tuesday, July 17, 2007

additional swap file howto

First, we need to know what is swap memory?
Generally, this is a temporary memory storage for an OS to utilize while the OS is trying to get more busy process managing another separate threads from another running applications.

Assuming you already have an existing swap partition, which is usual by default installation, and we would like to have additional swap memory by adding swap file, say on a different partition or harddrive, here's how to do it while inside your redhat based ditro:

VIEW CURRENT SWAP :

# cat /proc/swaps

Similarly, you'll see

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Filename Type Size Used Priority
/dev/sda6 partition 2104472 3904 -1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Currently, I am using a swap partition located from /dev/sda6. Take note of this.
Alternatively, here are the other ways to get the same swap info further:

# free
# fdisk -l | grep swap
# cat /etc/fstab | grep swap
# top -b -n 1 | head -5 | tail -1

CREATE SWAP FILE ON EXISTING PARTITION:

Since we already have a swap memory using swap partition, let us try to add and create additional swap memory using swap file. But this time, we would create it in an already existing linux (after installation), and into a different partition separate from existing swap partition.

Our current swap partition was /dev/sda6. From here, and for a test, let us try to create additional swap memory using a new swap file and placed it to /home/swap .

# cd /home/swap

Here, create a 2 MB swap file named newswap like so

# dd if=/dev/zero of=newswap bs=1024 count=20000
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20000+0 records in
20000+0 records out
20480000 bytes (20 MB) copied, 0.115077 s, 178 MB/s
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ls -la newswap
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-rw-r--r-- 1 root root 20480000 2007-07-17 17:07 newswap
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Now, set it up and inform linux its a swap file

# mkswap newswap
Result:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Setting up swapspace version 1, size = 20475 kB
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Before we add the new swap file, let's note of our current swap info

# free
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Swap: 2104472 7288 2097184
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# cat /proc/swaps

shows
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Filename Type Size Used Priority
/dev/sda6 partition 2104472 7288 -1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Now, activating the new swap file under /home/swap, still as root

# swapon newswap


SWAP VERIFICATION:

# cat /proc/swaps

will now show
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Filename Type Size Used Priority
/dev/sda6 partition 2104472 7288 -1
/home/swap/newswap file 19992 0 -3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The same with
# free


shows your newly created swap file from a different / partition

Basically, this goes the same with a new swap file from a different harddrive. Simply mount the new harddrive and prepare a new folder with it for new swap file

# mount /dev/sdb1 /mnt/newharddisk
# cd /mnt/newharddisk
# mkdir newswapfolder
# cd newswapfolder
# dd if=/dev/zero of=newotherswap bs=1024 count=20000
# mkswap newotherswap
# swapon newotherswap

# cat /proc/swaps
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Filename Type Size Used Priority
/dev/sda6 partition 2104472 7288 -1
/home/swap/newswap file 19992 0 -3
/mnt/newharddisk/newotherswap file 19992 0 -4
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Now you have 3 swap files working :) which is not adviseable, but taken and hereby shown for the sake of creating and deleting swap files.

DISABLING SWAP

Turning off is as easy as

# swapoff swapfilename
# rm -rf swapfilename

HTH

0 comments:

Sign up for PayPal and start accepting credit card payments instantly.
ILoveTux - howtos and news | About | Contact | TOS | Policy