Extending SWAP Space
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:
swapon --show
free -h
lsblkTypical output:
NAME TYPE SIZE USED PRIO
/dev/vg00/lv_swap partition 2G 128M -2Note the device path — the examples below use /dev/vg00/lv_swap. Substitute yours everywhere.
Confirm the volume group has room:
vgsLook 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#
free -hCompare 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#
swapoff -v /dev/vg00/lv_swapThis 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:
lvextend -L +2G /dev/vg00/lv_swapTo consume all remaining free space in the volume group:
lvextend -l +100%FREE /dev/vg00/lv_swapConfirm the new size:
lvs /dev/vg00/lv_swap4. 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:
mkswap /dev/vg00/lv_swapNote 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:
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 onemkswapjust 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#
swapon -v /dev/vg00/lv_swapVerification#
swapon --show
free -hThe swap size should reflect the new total. Confirm the fstab entry is valid so the change survives a reboot:
swapoff -a && swapon -a && swapon --showIf that sequence completes without error, the fstab entry is correct.
Troubleshooting#
| Symptom | Cause and fix |
|---|---|
swapoff hangs for a long time | Normal when a lot is swapped in. Leave it. Do not interrupt it — check progress from another session with free -h. |
swapoff: Cannot allocate memory | Not 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 lvextend | The volume group is full. Add a disk and extend the VG first. |
| Swap missing after reboot | The fstab UUID was not updated after mkswap. Correct it as in step 5. |
Swap shows the old size after lvextend | mkswap was not run, or swap was re-enabled before it. Repeat steps 4 and 6. |