geoffwilliams@home:~$

Shrinking LUKS encrypted LVM2 EXT4 filesystem

Sometimes you need to shrink EXT4 filesystem hosted on LVM + LUKS to free up a bit of space for an additional filesystem.

This can be vital in some situations:

  • /boot too small and blocks kernel upgrades… but almost the whole drive is taken by LUKS - I give myself at least 2G these days
  • swap partition too small. In this case consider using a swap file instead
  • Shrinking one big LV for an entire Linux install into / + /home partitions, so that you can install Debian 12 without wiping the disk.

Its impossible to shrink EXT4 online and with LVM + LUKS there are a few additional steps. Heres how to do it:

  1. Boot into a live Linux install USB. Ubuntu 22.04+ is best, Debian 12 Live will work as well but needs internet access to install gparted with apt
  2. Backup the whole drive to be shrunk! Cannot emphasise this enough…
  3. Unlock the encrypted volume
# find the partition using `blkid`
cryptsetup luksOpen /dev/sda5 rootfs
  1. Find the LVM LV you want to shrink
lvdisplay
  1. Shrink the EXT4 filesystem and the LVM LV all in one go, eg to shrink ~1650G to ~1500G to give us some room back:
lvresize --resizefs --size 1500G /dev/vgubuntu/os

Using the newly freed space

New filesystem inside LVM/LUKS

Allocate all free space to a new LV, eg:

lvcreate -l 100%FREE -n root vgubuntu
  • Size: Whatever is left in the VG (100 % of free space remaining)
  • LV name: root
  • VG name: vgubuntu

Fix LV/VG names

Now is a good time to fix the VG and LV names using vgrename and lvrename respectivly. This is worth doing to avoid any confusion when doing further meddling.

If your not planning to reinstall the OS, check and fix as needed:

  • Filestem names in /etc/fstab
  • GRUB_CMDLINE_LINUX root= argument in /etc/default/grub

If changing grub settings, rerun grub setup

Renaming PVs isnt a thing in LVM since they have no name associated with them beyond the VG.

Example:

Say you currently have:

  • /dev/vgubuntu/os - complete old Ubuntu installation
  • /dev/vgubuntu/root - created a moment ago to hold Debian 12 installation

These can be renamed to make more sense, eg:

vgrename vgubntu vg
lvrename vg os home

To give:

  • /dev/vg/home - complete old Ubuntu installation (the only thing we care about is the homedirs)
  • /dev/vg/root - created a moment ago to hold Debian 12 installation

Now just create the filesystem:

# check LVM looks as expected before running mkfs!
lvscan
mkfs.ext4 /dev/vg/root

Shrink the LUKS volume

Easiest way is to use gparted. You will have to right click “unlock volume” on the LUKS partition. Modern gparted is able to do all of the PV resizing for you, its really great and easy.

With the shrunken LUKS volume you can do all the usual gparted tasks:

  • Move partitions left/right
  • Shrink/grow existing partitions
  • Add new partitions

Post comment