Jeff Stonacek, Principal Architect
There are two common ways to increase the amount of swap space in Linux, by either adding a storage device or adding a swap file on a filesystem. In certain situations however, it would be cleaner to merely increase the size of the existing swap device. This last method is the one that we will be discussing in this blog topic.
For this example we will be using a VMware virtual machine. The concepts are the same for a physical server, but the implementation steps would be slightly different. In this scenario, swap was created as a logical volume in the same volume group as the root filesystem, which is not an uncommon situation with today’s Linux builds. The specifics about the starting configuration are:
- One disk device attached to the machine
- Two partitions on the disk
- sda – /boot
- sdb – root volume group (rootvg)
- Swap is located on an 8 GB logical volume, swaplv
- There is zero free space in the rootvg volume group
The goal is to increase swap space to 16 GB by increasing swaplv in rootvg. However, rootvg has no free space, so there will be several steps involved in making this work. The high level steps are:
- Increase the size of the disk device for the machine
- Increase the size of the /dev/sda2 partition (rootvg)
- Make LVM aware of the new size of /dev/sda2
- Increase the size of the swaplv logical volume
- Make Linux aware of the new swap size
Increase the Disk Size
The focus of this blog is on the Linux steps needed to increase swap space in an existing device, not a VMware tutorial. So, we’ll gloss over the steps to increase the size of the virtual disk. In VMware, this step can be done with the virtual machine running. Modify the virtual machine properties and increase the size of the VMDK in question.
Increase the Size of the Disk Partition
In our scenario, increasing the size of the disk partition is tricky as the partition in question contains the root filesystem. This means we will have to boot from alternative media to perform this task. There are many options for this, including the Linux system rescue CD (http://www.sysresccd.org). In our example we will simply boot from the Red Hat install ISO into rescue mode and drop into a shell prompt as shown below.
Boot into rescue mode. Do not start networking, or mount the local installation.
This will drop you into a shell logged in as user root.
The underlying disk has increased in size to 80 GB, so there is now free space on the disk. But, the partition is still sized at 70 GB. To change the size, delete the partition and re-add it with the same starting sector. This may seem a bit scary, but none of the data will be lost from the partition.
Reboot the machine as normal.
Increase LVM and Linux Components
Now we need to increase the size of the LVM physical volume (PV). When we list to attributes of the PV it still shows 70 GB for a size and zero free space.
The command to resize a PV is, ironically, pvresize.
Now our PV and VG show 10 GB of free space.
Next we need to increase the size of our logical volume (LV). Unfortunately, the LV in question is currently being used by the OS for swap space. We need to “swapoff” the LV first and then resize.
Swaplv is our only swap device and zero bytes are currently in use, probably because we just booted the machine.
In this blog we have demonstrated the procedure for increasing a partition as well as the underlying LVM components and swap device. This procedure does require an outage, and provides the cleanest approach for resizing the main system swap device with no data loss.