Shrink an Encrypted LVM

Posted by zbrom on June 27, 2021 · 2 mins read

Recently while preparing a persistent USB Linux OS, I found difficulty in removing an encrypted swap and shrinking the size of the encrypted LVM. This guide should provide some clarification.


Remove Swap

If you previously had a swap partition, ensure it is turned off

sudo swapoff -a

Remove your particular cryptswap partition.

sudo cryptsetup remove /dev/mapper/cryptswap1

Remove your logical volume.

lvremove /dev/VolGroup00/LogVol01

Resize Volumes

Display the size of your logical volume(s)

lvdisplay | grep Size

If desired, feel free to resize a logical volume (choose your desired size e.g. 50G)

lvresize /dev/VolGroup/LogVol00 --size 50G

Resize your physical volume to match the logical volume (choose your desired size e.g. 50G)

pvresize /dev/sda2 --setphysicalvolumesize 50G

Display Volume Sizes

Display the size of your logical volume(s)

lvdisplay | grep Size

Display the PV sizes in SECTORS and take note of the smaller PSize:

lvm pvs --units s

Display the partition Size in SECTORS using parted and take note of the Start sector and partition Number of the partition with “lvm” in the Flags column (I recommend you save the full output of the following command):

parted /dev/sda unit s print

POINT OF NO RETURN – HAVE BACKUPS

Now we need to nuke the old, large partition from the partition table, and immediately recreate it with the exact same Start sector, but with a newer, shorter End sector. To calculate the new End sector, ADD the partition Start sector to the PV PSize, and then add a safety margin of around 131072 sectors (64MB) to that. When I attempted this, it required my to do five times this amount (655360 sectors). The number 2 should correspond to your particular lvm partition. Again, make sure your data is backed up.

parted /dev/sda rm 2

Recreate it smaller + safety margin (as explained above). NOTE: remember to append the letter “s” to your values to qualify them as sectors. (e.g. “131072s”)

parted /dev/sda mkpart primary 401625s 105390297s parted /dev/sda set 2 lvm on parted /dev/sda print fdisk -l /dev/sda

Hopefully this offers some clarification of how to accomplish this cumbersome task. For further information, check out these references.

References:

https://centoshelp.org/resources/post-install-options/shrink-a-default-install-lvm-pv-to-create-another-partition/

https://www.logilab.org/blogentry/29155