How to increase swap by taking the space from a different logical volume

The automatic installer of my current Debian 11 installation chose to use only 1 GB of swap for some reason, which is far too little to receive the entire RAM contents for hibernation.

This blog post is based on https://www.rootusers.com/lvm-resize-how-to-decrease-an-lvm-partition/

Because of this, I got the error message Failed to suspend system. System resumed again: No space left on device in my syslog. But, because all space of the physical storage was already used up by several logical volumes, I needed to ‘steal’ some space from the home LV, which was 200GB.

Doing so is quite easy. The following example steps, ran in the ‘rescue mode’ of Debian, will give 50 GB from the home LV to swap space.

# Activate all logical volumes (makes them available in the /dev/VGname/LVname)
vgchange -a y

# Un-mount the /home directory. For this to succeed, all programs are not allowed to have open files there. This may require some kind of 'rescue' environment of your OS.
umount /home

# Check that the file system is in order, so that it is safe to shrink it in the next step.
e2fsck -fy /dev/vgname/home

# Shrink the file system to a size a bit smaller than the target size. There are only 20 GB of data. The partition is 200 GB.
resize2fs /dev/vgname/home 140G

# Reduce the logical volume to the target size of 150 GB. This is likely to lose data if the file system is larger than the target size. Please double check your numbers; lvreduce will ask you the same thing and then ask for a confirmation.
lvreduce -L 150G /dev/vgname/home

# Expand the file system to auto-fit exactly into the logical volume (grows from 140G to 150G)
resize2fs /dev/vgname/home

# Now there is some space left on the physical device. Next, expand the swap space to use the freshly made available space.
swapoff -a
lvextend -L 40G /dev/vgname/swap_1
mkswap /dev/vgname/swap_1
swapon -a
If you found a mistake in this blog post, or would like to suggest an improvement to this blog post, please me an e-mail to michael@franzl.name; as subject please use the prefix "Comment to blog post" and append the post title.
 
Copyright © 2023 Michael Franzl