Linux‎ > ‎

Increase Swap

Increase swap space Linux (LVM)


Overview

The purpose of this document is to outline the procedure required to resize an ext2/ext3 filesystem in LVM to make room for more swap space. For the purpose of this document, we will assume that we will be resizing /usr, and will be adding 8GB of swap.

This procedure ONLY works for an ext2/ext3 filesystem!

The basic procedure consists of the following steps

  1. Identify the /usr LVM logical volume
  2. Shrink the /usr filesystem
  3. Shrink the /usr LVM logical volume
  4. Expand the /usr filesystem
  5. Create a new LVM logical volume for swap
  6. Format and activate the new swap volume

Technically, Step 4 is not necessary if you are careful to calculate the exact size of the filesystem and LVM volumes. However, this is succeptible to calculation errors, so the best practice is to shrink the filesystem more than is necessary, resize the logical volume to the desired final size, and then grow the filesystem to fill that logical volume. Identify the /usr LVM logical volume

  1. df -h /usr

Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup_ID_26663-LogVol5

                      89G  8.8G   76G  11% /usr

The logical volume that contains the /usr filesystem is LogVol5 which belongs to the VolGroup_ID_26663 volume group. Keep note of this, you will need it later. Also, be sure that there is enough free space to reallocate to the new swap volume. In this case, there is 76GB free, so there is plenty of extra space. Shrink the /usr filesystem

In order to shrink an ext2 or ext3 filesystem, it must be unmounted. Because /usr is generally needed for normal system operation, you will need to boot into a LiveCD of some sort. Some popular choices are Knoppix or the Gentoo Minimal CD. For the sake of this document, we will assume the use of the Gentoo Minimal CD because it is fast to boot, it drops you right to a root shell, and includes all of the filesystem and LVM tools that you will need.

Once booted, make sure LVM is working

  1. lvscan
 inactive          '/dev/VolGroup_ID_26663/LogVol1' [10.00 GB] inherit
 inactive          '/dev/VolGroup_ID_26663/LogVol2' [8.00 GB] inherit
 inactive          '/dev/VolGroup_ID_26663/LogVol5' [89.94 GB] inherit
 inactive          '/dev/VolGroup_ID_26663/LogVol4' [10.00 GB] inherit
 inactive          '/dev/VolGroup_ID_26663/LogVol0' [8.00 GB] inherit
 inactive          '/dev/VolGroup_ID_26663/LogVolHome' [9.53 GB] inherit

You will need to activate the logical volumes before you can work with the /usr filesystem.

  1. vgchange -ay
 6 logical volume(s) in volume group "VolGroup_ID_26663" now active

Now that the volume groups are active, we can resize the /usr filesystem. Recall that we want to add 8GB of swap, and that we want to use the shrink-too-much trick as described above, so we will shrink the filesystem by 10GB to make sure we leave enough padding. The filesystem is currently 89GB, so we will resize it to 79GB.

  1. resize2fs /dev/VolGroup_ID_26663/LogVol5 79G

resize2fs 1.40.8 (13-Mar-2008) Please run 'e2fsck -f /dev/VolGroup_ID_26663/LogVol5' first.

Note, resize2fs did not allow us to resize the filesystem without checking it first. This is a built-in safety feature of resize2fs so we do not modify a filesystem that already has problems. Doing so would likely lead to corruption. This feature can be overridden, but it is highly recommended against. So, just like the warning says, let's run e2fsck

  1. e2fsck -f /dev/VolGroup_ID_26663/LogVol5

As long as e2fsck finished without errors, we can now actually resize the filesystem

  1. resize2fs /dev/VolGroup_ID_26663/LogVol5 79G

Depending on how big /usr is, this may take a few minutes. Shrink the /usr LVM logical volume

Now that the /usr filesystem has been reduced, we can resize the LVM logical volume to the desired final size of /usr. Again, recall that we want an additional 8GB of swap space, and that the size of the /usr logical volume is 89.94GB. We will round this off, and make the new size of the logical volume 81GB.

Before we can resize the logical volume, we must deactivate it. The easiest way to do this is to just deactivate all of the logical volumes

  1. vgchange -an
 0 logical volume(s) in volume group "VolGroup_ID_26663" now active

Now, we can resize the LVM to the desired size

  1. lvresize -L 81G /dev/VolGroup_ID_26663/LogVol5

Expand the /usr filesystem

Now that the /usr LVM logical volume is the desired size, we can expand the /usr filesystem to fill it. Before we do, we need to reactivate the LVM logical volumes

  1. vgchange -ay
 6 logical volume(s) in volume group "VolGroup_ID_26663" now active

And then we can resize the filesystem

  1. resize2fs /dev/VolGroup_ID_26663/LogVol5

At this point, it is safe to reboot the server, and load the installed operating system. The rest of the steps can be done while the server is live. Create a new LVM logical volume for swap

At this point, we have made space in LVM for an additional logical volume that will house our additinal swap space. The name of the new logical volume is completely arbitrary, but to remain consistent with the current naming scheme, we will name it LogVol6.

  1. lvcreate -L 8G -n LogVol6 VolGroup_ID_26663

This will create a new LVM logical volume device at /dev/VolGroup_ID_26663/LogVol6 where we will put our new swap space. Format and activate the new swap volume

At this point, all of the hard work is done, and all we have to do is format the swap space

  1. mkswap /dev/VolGroup_ID_26663/LogVol6

and activate it

  1. swapon /dev/VolGroup_ID_26663/LogVol6

To verify that the new swap is active, look at /proc/swaps.

Don't forget to add the new swap volume to /etc/fstab so it is available at the next boot

/dev/VolGroup_ID_26663/LogVol6 swap swap defaults 0 0


Comments