Friday, May 18, 2012

Add/Remove more swap space in Linux


In Linux, a swap space can be a disk partition or a file. Rather than creating an entire partition for swap space, a swap file offers the ability to vary its size on-the-fly, and is more easy to remove too. But, BTRFS filesystem does not currently support swapfiles.

Let us see how to create multiple swap files(not swap partitions) in this article

To know the existing swap space in the system
# swapon -sh
Filename                                Type            Size    Used    Priority
/dev/sda3                               partition       5119992 0       -1

Let us create multiple swap file spaces, each of size around 128MB.

dd command is a useful utility to achieve this. Let me briefly tell about dd command parameters to be used
if = input file
of = output file
bs = block size(Here, Iam using 1024 bytes as block size)
count = To get the desired size of output file, multiply the bs(1024 bytes, in this case) with this count number.

First create container files
# dd if=/dev/zero of=/opt/swp/swp1 bs=1000 count=128000
# dd if=/dev/zero of=/opt/swp/swp2 bs=1000 count=128000

Let us see what kind of file we have created
# file swp2 swp1
swp2: data
swp1: data

Format as swap
# mkswap /opt/swp/swp1
# mkswap /opt/swp/swp2

Let us see what kind of file we have created
#file swp1 swp2
swp1: Linux/i386 swap file (new style) 1 (4K pages) size 31249 pages
swp2: Linux/i386 swap file (new style) 1 (4K pages) size 31249 pages

Adding the newly created swap spaces to startup:


Currently just a single swap partition exists in /etc/fstab

# grep swap /etc/fstab
UUID=fa0d3137-3f0d-48f6-80bb-abed07c79ee0 swap                    swap    defaults        0 0

Replacing the entry for the swap partition with these three lines in /etc/fstab
UUID=fa0d3137-3f0d-48f6-80bb-abed07c79ee0 swap                    swap    defaults        0 0
/opt/swp/swp1 swap swap defaults 0 0
/var/swp/swp2 swap swap defaults 0 0

In this case, all 3 swap partitions have equal priority. So pages are allocated on a round-robin basis between them.

If priority were to be assigned to each of the swap spaces, have the entry in /etc/fstab as follows
UUID=fa0d3137-3f0d-48f6-80bb-abed07c79ee0 swap                    swap    sw,pri=3        0 0
/opt/swp/swp1 swap swap sw,pri=2 0 0
/opt/swp/swp2 swap swap sw,pri=1 0 0

This configuration would prompt the kernel to use the swap space with highest priority first.
So here the highest priority is 3 and this swap space shall be used first. The maximum priority can be 32767 and the lowest 0. If that swap space were to max out, the kernel would start using /opt/swp/swp1, and on to /opt/swp/swp2 after that.

The entries in /etc/fstab come into effect only after boot. So to enable the newly created swap spaces immediately(without booting):
# swapon /opt/swp/swp1
# swapon /opt/swp/swp2

Check the status of swap
# swapon -sh
Filename                                Type            Size    Used    Priority
/dev/sda3                               partition       5119992 0       -1
/opt/swp/swp1                           file            124992  0       -2
/opt/swp/swp2                           file            124992  0       -3

After rebooting, the entries in /etc/fstab come into effect. After reboot(I have given different priorites for each swap space in /etc/fstab)
# swapon -sh
Filename                                Type            Size    Used    Priority
/dev/sda3                               partition       5119992 0       3
/opt/swp/swp1                           file            124992  0       2
/opt/swp/swp2                           file            124992  0       1

Remove swap file:

# swapoff -a
# rm /opt/swp/swp2
# swapon -a

No comments:

Post a Comment