How to Add Swap Space

How To Add Swap Space to a Linux System

There are two ways to go about adding swap space to a linux system.

  1. Use of a hard disk partition
  2. Create a swap file on an existing file system

To view current swap utilization, a user can use the commands free or swapon. Swap utilization may also be viewed within the /proc file system:

[root@linux01 ~]# free -m
total used free shared buffers cached
Mem: 48297 34139 14157 0 556 25966
-/+ buffers/cache: 7616 40681
Swap: 10236 49 10187

[root@linux01 ~]# swapon -s
Filename Type Size Used Priority
/dev/sda3 partition 10482404 50364 -1

[root@linux01 ~]# cat /proc/swaps
Filename Type Size Used Priority
/dev/sda3 partition 10482404 50364 -1

Clearly, we see swap -s and /proc/swaps provides the same output

Method 1

Disk partition

Use of a hard disk partition is very simple to setup. If a spare hard disk is available, or there’s unpartitioned space available on an existing hard disk, use fdisk to create a partition. In my case, I had neither immediately available to me, but what I did have was plenty of EMC SAN, so I created a LUN, presented it to the server, and created a partition via fdisk just as if it were a regular disk. While creating the partition, don’t forget to set the partition type to Linux swap ==Hex code 82== via the ==t== option in fdisk:

t change a partition's system id

In the case of VMware, a virtual disk can be added. In this case, or in the case of adding a new physical disk to a server, here’s how to add the disk to the system without rebooting, minus the need to create the ext3 filesystem of course.

Here is what mine looked like after creating the partition via fdisk:

[root@linux01 ~]# fdisk -l /dev/emcpoweraj

Disk /dev/emcpoweraj: 32.2 GB, 32212254720 bytes
64 heads, 32 sectors/track, 30720 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes

Device Boot Start End Blocks Id System
/dev/emcpoweraj1 1 30720 31457216 82 Linux swap

Next, use the mkswap command to set up a Linux swap area, and swapon to enable the device for swapping

[root@linux01 ~]# mkswap /dev/emcpoweraj1
[root@linux01 ~]# swapon /dev/emcpoweraj1

In order to make this swap partition available across reboots, don’t forget to add it to the /etc/fstab file.

!!! note “fstab entry”

## xtra swap via EMC LUN
/dev/emcpoweraj1 swap swap defaults 0 0

Verify the new swap space is available to the system:

[root@linux01 ~]# cat /proc/swaps 
Filename Type Size Used Priority
/dev/sda3 partition 10482404 50360 -1
/dev/emcpoweraj1 partition 31457208 0 -2

[root@linux01 ~]# free -m
total used free shared buffers cached
Mem: 48297 32580 15717 0 562 26390
-/+ buffers/cache: 5626 42671
Swap: 40956 49 40907

Method 2

Free disk space

If you don’t have additional, physical hardware or non-partitioned space available, but have plenty of free disk space within an existing filesystem, you can create a file and use that for swap space. Here’s an example of adding 2 GB of swap via a swap file:

[root@linux01 ~]# dd if=/dev/zero of=/root/xtraswap bs=1M count=2048
2048+0 records in
2048+0 records out

[root@linux01 ~]# ls -lh /root/xtraswap
-rw-r--r-- 1 root root 2.0G Jul 23 14:08 /root/xtraswap

Permissions should be changed such that only root has access, then setup the linux swap area via mkswap and enable it

[root@linux01 ~]# chmod 600 /root/xtraswap
[root@linux01 ~]# mkswap /root/xtraswap
Setting up swapspace version 1, size = 2147479 kB
[root@linux01 ~]# swapon /root/xtraswap

Just as in method 1 above, verify the extra swap space has been added by looking at /proc/swaps or using the swap -s command. In addition, add the newly created swap space to /etc/fstab to ensure it’s available on reboot.

!!! note “fstab entry”

## xtra swap via swap file
/root/xtraswap swap swap defaults 0 0