How to Increase Swap Space in Red Hat Linux? Creating a New Swap File and Creating a Swap File on an Existing Logical Volume

How to Increase Swap Space in Red Hat Linux? Creating a New Swap File and Creating a Swap File on an Existing Logical Volume
Page content

Swap Space

It all happens some time: you run out of your main memory and the system turns to the hard disk to use as a temporary memory. What if you come to the limits of your Linux server’s swap space but the system demands more? There are basically four options here:

  1. Create a new swap file
  2. Create a swap file on an existing logical volume
  3. Extend a swap file on an existing logical volume
  4. Create a new swap partition

If you are in need of swap space now and do not think you will need it later, then taking the system down for creating a new swap partition or extending the swap file on the logical volume will be the longer way to take; creating a new swap file and making it available will be the easiest and the quickest solution. So, in our list, creating a new swap file is the easiest, extending swap file on an existing logical volume is a little harder but recommended, and creating a new swap partition is the hardest.

Create a New Swap File

We need to know the size of the additional swap space we will need. Let’s assume that we will need a 512 Megabytes of swap space. To determine the number of blocks, we need to multiply the size with 1024. In our example this gives 512 x 1024 = 524288. Now we issue

dd if=/dev/zero of=/home/swapfile bs=1024 count=524888

to open up our free space (you may argue that making a swap space in the /home directory is questionable in terms of security, but if you’re tight on hard disk space on the server, /home is the directory that you can find some free space). Then setup the file as swap with

mkswap /home/swapfile

The swap space will not be enabled at boot time. You need to add the space to /etc/fstab. Add the following line to your /etc/fstab:

/home/swapfile swap swap defaults 0 0

Creating a Swap File on an Existing Logical Volume

In this case, let’s assume we want to add the swap space on /dev/VolGroup00 and /LogVol02 is the swap space we want to add. Let’s also assume that the swap space we need is 2 Gigabytes. First, we create the logical volume of size 2 GB by

lvm lvcreate VolGroup00 -n LogVol02 -L 2 G

Then we format it by

_mkswap /dev/VolGroup00/_LogVol02

and add it to /etc/fstab by adding this line:

/dev/VolGroup00/LogVol02 swap swap defaults 0 0

Now we activate the swap volume by

swapon -va

To check if the swap is working properly, you can issue cat /proc/swaps or simply free.

Extending Swap File on an Existing Logical Volume

For this case, let’s assume that the swap space we will extend is /dev/VolGroup00/LogVol01 and we want to extend it by 1 Gigabyte. First, we disable the active swap file by

swapoff -v /dev/VolgGroup00/LogVol01

Then we add the swap space by

lvm lvresize /dev/Volgroup00/LogVol01 -L 1G

format it

mkswap /dev/VolGroup00/LogVol01

and finally enable the extended volume by swapon -va. As always, you can check the new space by cat /proc/swaps or free commands.

Create a New Swap Partition

We can go with the parted program here since it is easier than fdisk. Before adding a swap partition, we have to check which hard disk has sufficient space to be used as swap. We can do that by issuing print at the parted prompt. With the information we receive, we determine how much space we will use and at what partition. Assume that we will add 1 GB of swap space to /dev/hdc1. The syntax that we will use in parted is

mkpartfs partition-type filesystem-type start end

where start and end are the megabytes from the beginning of the disk (partition-type can be primary, extended or logical but extended and logical are used only for MS-DOS and MIPS labels). Further assuming that the free space on /dev/hdc1 starts at the 520th megabyte, the ending will be 520 + 1024 (1 Gigabyte of space) = 1544. So our command will be

mkpartfs primary linux-swap 520 1024

After reserving the space, we format it with swap partition type, using

mkswap /dev/hdc1

Then we enable it by swapon /dev/hdc1. And finally, to activate it in boot time, we add it to /etc/fstab by adding the following line:

/dev/hdc1 swap swap defaults 0 0

As we have seen, we check the swap by cat /proc/swaps or free.

Conclusion

With the falling prices of main memory (RAM), usually system administrators do not need the swap space in daily normal operations. But times can be tough and the system may run out of swap space, and then the administrator can have no chance but to increase swap size in order to cope with the peak load. The quickest way could be to add a swap file and the long-term solution is to add a swap partition.

How to Build a Linux Server - In this series we look at building and configuring a Linux server from scratch. We will look at configuration in detail and we will determine ways to keep our server as secure as possible.