Logical Volume Manager is a Linux tool for managing your partition. LVM allows you to have the ability to play around with partition size and/or combine multiple hard disk to create partition across all the combined hard disk. The LVM-HOWTO explains its function and how you could benefit from it.

Resizing swap partition is a little bit different but a lot easier. Since we are not concern over the swap partition’s content and resize2fs doesn’t support swap partition; we can safely re-size the logical volume and re-format the partition to update its partition size. In the example below, PART 1 shrinks the swap partition from 8GB to 4GB and PART 2 increases the partition back to 8GB.

Why 8GB? It is recommended that swap partition is double the size of installed RAM; In this case, the server’s RAM is 4GB. Although none of my servers has ever used up all the swap storage but its good to be safe and we can always re-size it when ever we want to. It’s the beauty of Logical Volume Manager!

Part 1: To shrink LVM swap partition size

# free -m
             total       used       free     shared    buffers     cached
Mem:          3951        132       3819          0          1         66
-/+ buffers/cache:         63       3887
Swap:         8191          0       8191
# swapoff -a
# free -m
             total       used       free     shared    buffers     cached
Mem:          3951        128       3823          0          1         66
-/+ buffers/cache:         60       3891
Swap:            0          0          0
# lvreduce -L 4G /dev/Inno/swap
  WARNING: Reducing active logical volume to 4.00 GB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce swap? [y/n]: y
  Reducing logical volume swap to 4.00 GB
  Logical volume swap successfully resized
# mkswap /dev/Inno/swap
Setting up swapspace version 1, size = 4294963 kB
# swapon -a
# free -m
             total       used       free     shared    buffers     cached
Mem:          3951        130       3821          0          1         66
-/+ buffers/cache:         62       3889
Swap:         4095          0       4095

Part 2: To increase LVM swap partition size

# free -m
             total       used       free     shared    buffers     cached
Mem:          3951        130       3821          0          1         66
-/+ buffers/cache:         62       3889
Swap:         4095          0       4095
# swapoff -a
# free -m
             total       used       free     shared    buffers     cached
Mem:          3951        128       3823          0          1         66
-/+ buffers/cache:         60       3891
Swap:            0          0          0
# lvextend -L 8G /dev/Inno/swap
  Extending logical volume swap to 8.00 GB
  Logical volume swap successfully resized
# mkswap /dev/Inno/swap
Setting up swapspace version 1, size = 8589930 kB
# swapon -a
# free -m
             total       used       free     shared    buffers     cached
Mem:          3951        132       3819          0          1         66
-/+ buffers/cache:         63       3887
Swap:         8191          0       8191

2 thoughts on “LVM: Resizing swap Logical Volume

Leave a Reply