How to extend & reduce Swap Space on LVM2 Logical Volume

Overview:

By default all Linux(RHEL,CentOS,Fedora & Ubuntu) like operating system uses all available space during installation. If this is the case with your system, then 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):

Output of Free Command before extend :

free-before-extend

Steps to Extend Swap space on an LVM2 Logical Volume :

Step:1 Disable swapping for the associated logical volume:

[root@localhost ~]# swapoff -v /dev/VolGroup/lv_swap 
swapoff on /dev/VolGroup/lv_swap

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

[root@localhost ~]# 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:

[root@localhost ~]# 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:

[root@localhost ~]# 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.

free-after-extend

Steps to Reduce Swap on an 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):

Output of Free Command before reduction :

free-before-reduce

Step:1 Disable swapping for the associated logical volume:

[root@localhost ~]# swapoff -v /dev/VolGroup/lv_swap 
swapoff on /dev/VolGroup/lv_swap

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

[root@localhost ~]# 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

[root@localhost ~]# 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

[root@localhost ~]# 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 to inspect the swap space.

free-after-reduce

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


Leave a Reply

Your email address will not be published. Required fields are marked *