twitter

How To's Tutorials

How to extend & reduce Swap Space on an LVM2 Logical Volume

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 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 :

 

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.

 

 

 

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 :

 

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.


 

comments powered by Disqus