How to extend and reduce Swap Space on LVM2 Logical Volume

By default, all Linux(RHEL, CentOS, Fedora, and Ubuntu) like operating system uses all available space during installation. If this is the case with your system, then a swap partition will be created on LVM and if you want to resize the swap space then you must first add a new physical volume to the volume group used by the swap space.

After adding additional storage to the swap space’s volume group, it is now possible to extend it. To do so, perform the following steps (assuming /dev/VolGroup/lv_swap is the volume you want to extend by 2 GB):

The output of Free Command before extend:

Steps to extend swap space on an LVM2 logical volume

Step 1: Disable swapping for the associated logical volume:

# swapoff -v /dev/VolGroup/lv_swap 
swapoff on /dev/VolGroup/lv_swap

Step 2: Resize the LVM2 logical volume by 2 GB.

# lvresize /dev/VolGroup/lv_swap -L +2G 
Extending logical volume lv_swap to 3.97 GiB 
Logical volume lv_swap successfully resized

Step 3: Format the new swap space.

# mkswap /dev/VolGroup/lv_swap 
mkswap: /dev/VolGroup/lv_swap: warning: don't erase bootbits sectors 
on whole disk. Use -f to force. 
Setting up swapspace version 1, size = 4161532 KiB 
no label, UUID=14df63cb-5e3b-42c3-911d-2016fb771804

Step 4: Enable the extended logical volume.

# swapon -v /dev/VolGroup/lv_swap 
swapon on /dev/VolGroup/lv_swap 
swapon: /dev/mapper/VolGroup-lv_swap: found swap signature: version 1, page-size 4, same byte order 
swapon: /dev/mapper/VolGroup-lv_swap: pagesize=4096, swapsize=4261412864, devsize=4261412864

To test if the logical volume was successfully extended, use cat /proc/swaps or free to inspect the swap space.

Steps to reduce swap on LVM2 logical volume

To reduce an LVM2 swap logical volume (assuming /dev/VolGroup/lv_swap is the volume you want to reduce by 512 MB):

The output of Free Command before reduction:

Step 1: Disable swapping for the associated logical volume:

# swapoff -v /dev/VolGroup/lv_swap 
swapoff on /dev/VolGroup/lv_swap

Step 2: Reduce the LVM2 logical volume by 512 MB:

# lvreduce /dev/VolGroup/lv_swap -L -512M 
WARNING: Reducing active logical volume to 3.47 GiB 
THIS MAY DESTROY YOUR DATA (filesystem etc.) 
Do you really want to reduce lv_swap? [y/n]: y 
Reducing logical volume lv_swap to 3.47 GiB 
Logical volume lv_swap successfully resized

Step 3: Format the new swap space.

# mkswap /dev/VolGroup/lv_swap 
mkswap: /dev/VolGroup/lv_swap: warning: don't erase bootbits sectors 
on whole disk. Use -f to force. 
Setting up swapspace version 1, size = 3637244 KiB 
no label, UUID=7f8f11de-5bc3-4b9c-b558-471fc540fa9b

Step 4: Enable the resized logical volume.

# swapon -v /dev/VolGroup/lv_swap 
swapon on /dev/VolGroup/lv_swap 
swapon: /dev/mapper/VolGroup-lv_swap: found swap signature: version 1, page-size 4, same byte order 
swapon: /dev/mapper/VolGroup-lv_swap: pagesize=4096, swapsize=3724541952, devsize=3724541952

To test if the swap’s logical volume size was successfully reduced, use cat /proc/swaps or free command to inspect the swap space.

Note: We can also create a temporary swapfile on a filesystem with dd & mkswap prior to doing swapoff, for cases where ram+swap usage exceeds ram size. +Later on swapoff from that temp swapfile & removing it of course.

Related Post