Free Bitcoins

HOW-TO: Add SWAP space from a file (Linux)

It may often be necessary to add swap space to an existing Linux machine as the application or service hosted demands additional space.

Swap in Linux is an entirely different beast from the /tmp. While Solaris or SunOS treats /tmp and swap as one. Adding swap can be done in two ways: [1] adding swap from a free partition; or [2] adding swap from a file. In this how-to, we will add swap from a file. How is this done?

In order to see the swap space and its usage, use "swapon -s" or "cat /proc/swaps" to get more information. The output will be somewhat similar to this:
 Filename        Type            Size    Used    Priority
/dev/sda1 partition 859436 0 -1

To add swap, follow the directions below:

1. Determine the size of the new swap file and multiple by 1024 to determine the block size. For example, the block size of a 64 MB swap file is 65536.

2. At a shell prompt as root, type the following command with count being equal to the desired block size:
 ==> 64MB
root@host # dd if=/dev/zero of=/PATH/swapfile bs=1024 count=65536
==> 512MB
root@host # dd if=/dev/zero of=/PATH/swapfile bs=1024 count=524288
==> 1GB
root@host # dd if=/dev/zero of=/PATH/swapfile bs=1024 count=1048576

3. Setup the swap file with the command:
root@host # mkswap /PATH/swapfile

4. To enable the swap file immediately (but not automatically at boot time):
root@host # swapon /PATH/swapfile

After adding the new swap file and enabling it, make sure it is enabled by viewing the output of the command "cat /proc/swaps" or free.

To permanently enable the swap file at boot time, edit /etc/fstab to include below line:
 /dev/sda1       swap      swap     defaults  0      0

Post a Comment

Subscribe for Latest Update

Popular Posts

Blog Archives