Increasing SWAP

how to adjust logical volume sizes (LVM on LUKS)

Created: | 566 words | 3 minutes

I use dual-booted arch/windows setup on my laptop. The systems share a common EFI partition, windows has its own crappy NTFS and Arch has a LUKS-encrypted block device and LVM logical partitioning within.

lsblk looked as follows:

NAME                 MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINTS
<device>             259:0    0 476.9G  0 disk
├─<deviceNp1>        259:1    0     1G  0 part  /boot
├─<windows crap>     259:N    0   <N>G  0 part
└─<deviceNpN>        259:N    0 219.9G  0 part
  └─<lvm name>       253:0    0 219.9G  0 crypt
    ├─<vg name>-root 253:1    0   128G  0 lvm   /
    ├─<vg name>-swap 253:2    0     2G  0 lvm   [SWAP]
    └─<vg name>-home 253:3    0  89.9G  0 lvm   /home

An experienced eye can notice how unexperienced my eye was when I configured SWAP spacing...

Idk why, but I only preserved 2G instead of 8G or even more, but changing it without proper understanding of what’s going on seemed shivery since the whole system runs there. I didn’t want to risk the data at /home, but I did lowkey risk it anyway.

Instruction

  1. Back-up data in /home (that’s for pussys ngl)

  2. Boot a live environment (Arch ISO) and unlock the encrypted volume

cryptsetup open /dev/<deviceNpN> <lvm-name>
vgchange -ay <volume-group-name>
  1. Check how much data is used in /home
mkdir /mnt/home
mount /dev/<vg name>/home /mnt/home
df -h /mnt/home
umount /mnt/home
  1. Force-check filesystem health and fix errors if prompted
e2fsck -f /dev/<vg name>/home

Make sure the occupied space is comfortably under the desired end volume. I had much less than \(89.9G-6G=84.9G\) in use. Don’t shrink to the bare minimum, leave at least a couple GB to wiggle around.

  1. Shrink the filesystem - make it 1-2G smaller than your final LV size
resize2fs /dev/<vg name>/home 83G

This is the step that actually protects your data — ext4 rewrites its metadata/block layout to fit the new smaller size before you touch the LV.

  1. Shrink LV (you can cross your fingers at this point)
lvreduce -L 84G /dev/<vg name>/home
  1. Grow the ext4 filesystem to fill the LV again
resize2fs /dev/<vg name>/home

Note there’s no size argument - fiesystem will grow to fill all the LV space available

  1. Extend SWAP
lvextend -L +6G /dev/<vg name>/swap
mkswap /dev/<vg name>/swap

Note that mkswap regenerates UUID, so you’ll have to adjust your /etc/fstab in root afterwards.

  1. Verify and reboot
e2fsck -f /dev/<vg name>/home
lsblk

Important notes

  • If you use XFS instead of ext4, forget about shrinking volumes - they can only be grown. Check your filesystem type with lsblk -f before proceeding. I suppose on XFS you can recreate a smaller filesystem somehow and then restore it’s size to your desired value.
  • Do it all in one live-boot session, so you do not mount /home in between fs-shrink and lv-shrink steps (better hold your breath during the process).

Conclusion

Now I’m able to hibernate and not worry about the keys sitting in RAM when I go away.

lsblk now looks like this:

NAME                 MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINTS
<device>             259:0    0 476.9G  0 disk
├─<deviceNp1>        259:1    0     1G  0 part  /boot
├─<windows crap>     259:N    0   <N>G  0 part
└─<deviceNpN>        259:N    0 219.9G  0 part
  └─<lvm name>       253:0    0 219.9G  0 crypt
    ├─<vg name>-root 253:1    0   128G  0 lvm   /
    ├─<vg name>-swap 253:2    0   7.9G  0 lvm   [SWAP]
    └─<vg name>-home 253:3    0    84G  0 lvm   /home

Special thanks to Sonnet 5 medium effort: vibecoded root manipulations are definitely the way to go from now on!