Tuesday, November 8, 2011

How to add swap space on linux

What is swap space

Swap space is used when the amount of physical memory on the system is full. Paging is the process of moving of inactive pages in memory to the swap space. Swap help you to cope up with shortage of RAM but you better increase your physical memory (RAM) on the system. Swap space can be a dedicated swap partition or swap files. When you build the system you have to consider adding better chunk of dedicated swap partition. The size of the swap partition is calculated using the formula below.

Lets say:

M = Amount of RAM in GB, and
S = Amount of swap in GB, then

If M < 2 then
S = M *2
else
S = M + 2

so, if a system has 2 GB of physical RAM then the recommended swap on the system is 4 GB.
If the system with 3 GB of physical RAM would have 5 GB of swap space. Swap space is used when the amount of physical memory (RAM) is full and the pages are start transferring to the swap.

----------------------------------------------------
Sometime you need to add extra swap due to the application and other service need extra space। There are two ways you can add swap space on the system।


I. Add a swap device/file on the system.

1. Using the disk partition as a swap device
2. Using a file as a swap space.

1. Disk partition as a swap space.
If you are planning to use disk partition as a swap space, create a new partition (fdisk /dev/sdb) as a swap file system and add a device as a swap device.

a. Lets say you have a new partition called /dev/sdb1. It is as a swap partition.
# mkswap /dev/sdb1

b. enable your partition
# swapon /dev/sdb1

c. Verify your swap is added to the system.
# swapon -s
# free -k
# cat /proc/swaps


----------------------------------------------------

2. Using a file as a swap space.

a. Display the current swap space on the system using swapon -s or cat /proc/swaps. Out put display in KB in size.
# swapon -s
# cat /proc/swaps


b. Create a swap file using the dd command.
# dd if=/dev/zero of=/path_to_file/meroswap bs=1m count=1024
# dd if=/dev/zero of=/export/meroswap bs=1m count=1024
The above command creates 1GB of file. Note the bs and count values.

# dd of=/dev/zero of=/path_to_file/meroswap bs=1024 count=1048578
# dd of=/dev/zero of=/export/merofile bs=1024 count=1048578
The above command also creates 1gb of file. The block size is 1024 bytes

Note: If you want to have a size in block other then human readable format like mb then you can use the formula below.
If the 512 mb of space multiply by 1024 to make it in block (512*1024=524288)
if you want 1gb (1024mb) multiply by 1024 to get in block (1024*1024=1048576)

Note: bs=bytes and count=blocks (bs=block size)
- You cannot use the cp (copy) command to create a swap file because the swap file must be physically continuous on the hard drive.
- Verify you have enough space on your file system before creating swap file.

c. Make the file as a swap file using the mkswap command. Verify the permission to access by root only.
# mkswap /path_to_file/meroswap
# mkswap /export/meroswap
# chmod 600 /export/meroswap


d. Enable the swap file.
# swapon /export/meroswap

e. Verify your swap space is added successfully and available for use by usingcat /proc/swaps or free command.
# swapon -s
# cat /proc/swaps
# free -k

II. Make it permanent across the reboot.

To make this permanent across the reboot, add an entry to your /etf/fstab.

/dev/sda1 swap swap defaults 0 0
/export/meroswap swap swap defaults 0 0


III. Removing a Swap File
a. Disable the swap file.
# swapoff -v /export/meroswap

b. Remove the entry from /etc/fstab.
/export/meroswap swap swap defaults 0 0

c. Remove the file
# rm /export/meroswap

Note: The output of the swapon -s command under partition displace the swap area if that is a partition or a file.

No comments:

Post a Comment