KB
Storage & Filesystems

Extending SWAP Space

4 min read773 words13 code blocks

At a glance#

  • Purpose: Increase the size of an existing LVM-backed swap volume.
  • Applies to: Any Linux distribution where swap lives on an LVM logical volume.
  • Risk: Medium — swap is disabled during the procedure; the machine can run out of memory if it is under pressure.
  • Time: About 15 minutes.

Overview#

Swap is disk space the kernel uses when physical memory runs low. When a server is persistently swapping, or when an application requires more swap than is configured, the swap volume needs to grow.

If swap sits on an LVM logical volume, extending it is straightforward: turn swap off, grow the volume, rebuild the swap signature, turn it back on. No reboot is required.

Warning: Step 2 disables swap entirely. The kernel must move everything currently in swap back into RAM to do this. If the amount in use is larger than the free physical memory, processes will be killed by the OOM killer. Always check free memory first, and prefer a maintenance window on a busy server.

Before you start#

Confirm all of the following:

  • The swap device is an LVM logical volume, not a partition or a swap file.
  • The volume group has free space, or you have added a new disk to it.
  • You have root access and console access, not just SSH, in case the machine becomes unresponsive.

Identify the current swap device and its size:

bash
swapon --show
free -h
lsblk

Typical output:

text
NAME           TYPE      SIZE   USED PRIO
/dev/vg00/lv_swap partition   2G   128M   -2

Note the device path — the examples below use /dev/vg00/lv_swap. Substitute yours everywhere.

Confirm the volume group has room:

bash
vgs

Look at the VFree column. If it shows 0, you must add capacity first — see Extending partition size by adding new disk LVM Linux.

Procedure#

1. Check that it is safe to disable swap#

bash
free -h

Compare the used figure on the Swap row against free on the Mem row. Free memory must comfortably exceed swap in use. If it does not, stop non-essential services first or schedule a reboot instead.

2. Disable the swap volume#

bash
swapoff -v /dev/vg00/lv_swap

This can take anything from a second to several minutes depending on how much is in use. The command does not return until the kernel has moved every page back into RAM.

3. Extend the logical volume#

To add a fixed amount:

bash
lvextend -L +2G /dev/vg00/lv_swap

To consume all remaining free space in the volume group:

bash
lvextend -l +100%FREE /dev/vg00/lv_swap

Confirm the new size:

bash
lvs /dev/vg00/lv_swap

4. Rebuild the swap signature#

Extending the volume does not extend the swap area inside it. The signature has to be written again across the new size:

bash
mkswap /dev/vg00/lv_swap

Note the UUID that mkswap prints — mkswap assigns a new UUID, which matters for the next step.

5. Update /etc/fstab if it references a UUID#

Check how swap is declared:

bash
grep swap /etc/fstab
  • If the line uses the device path (/dev/vg00/lv_swap), no change is needed.
  • If it uses UUID=..., replace the old UUID with the one mkswap just printed.
Warning: Skipping this step on a UUID-based entry means swap will not mount at boot, and you will not notice until the machine is under memory pressure weeks later.

6. Re-enable swap#

bash
swapon -v /dev/vg00/lv_swap

Verification#

bash
swapon --show
free -h

The swap size should reflect the new total. Confirm the fstab entry is valid so the change survives a reboot:

bash
swapoff -a && swapon -a && swapon --show

If that sequence completes without error, the fstab entry is correct.

Troubleshooting#

SymptomCause and fix
swapoff hangs for a long timeNormal when a lot is swapped in. Leave it. Do not interrupt it — check progress from another session with free -h.
swapoff: Cannot allocate memoryNot enough free RAM to hold what is in swap. Stop services to free memory, or reboot and run the procedure early in the boot when swap is empty.
Insufficient free space from lvextendThe volume group is full. Add a disk and extend the VG first.
Swap missing after rebootThe fstab UUID was not updated after mkswap. Correct it as in step 5.
Swap shows the old size after lvextendmkswap was not run, or swap was re-enabled before it. Repeat steps 4 and 6.