Enlarge LUKS encrypted logical volume
Use case: Having one LVM2 logical volume encrypted with LUKS, that needs to be enlarged.
There is a pretty detailed manual on enlarging encrypted partition here. However that manual is for enlarging LUKS encrypted physical volume, not logical volume, thus some of the steps are a bit different.
- Backup your data.
- To have more space for the logical volume enlargement, I used gparted tool where I enlarged the LVM2 physical volume (PV) that holds my LVM2 logical volume (LV) with ext4 filesystem which I want to enlarge. I just right-clicked on the partition (/dev/sda2 – the PV with the encrypted LV) and chose Resize/Move. It enlarged both the PV and volume group (VG) which was in my case the solely VG within the PV. This was the easiest solution for me, but you can use pvresize and vgresize commands to do it in terminal.
- Prepare live USB/CD, e.g. per instructions here https://fedoraproject.org/wiki/How_to_create_and_use_Live_USB and boot to it.
- I chose Fedora Live which has the needed tools (cryptsetup and lvm2) preinstalled, but if your live system doesn’t have these then install them.
- Open terminal and switch to root.
- Identify your encrypted LV by running lvdisplay. For me it is /dev/mbocek_lvm/home (or /dev/mapper/mbocek_lvm-home or /dev/dm-4, all three are equivalent). To complete the picture, mbocek_lvm is my VG. Run ‘cryptsetup isLuks /dev/mbocek_lvm/home -v’ to make sure this device (logical volume) is the one encrypted – it should return Command succesful.
- Decrypt the LV and map the LUKS encrypted counterpart of the LV to whatever name, e.g. crypt1. For that, run ‘cryptsetup luksOpen /dev/mbocek_lvm/home crypt1’. This creates mapping /dev./mapper/crypt1.
- Per the abovementioned Ubuntu manual, run ‘vgscan –mknodes’ and ‘vgchange -ay’. I’m not sure whether it’s needed but I did it and no harm done.
- Now, because the VG mbocek_lvm that contains the decrypted LV has bigger size with unused space (because of step 2), we can run a command to enlarge the decrypted LV to fill all the unused space. Run ‘lvresize -l +100%FREE /dev/mbocek_lvm/home’. If you need a specific size to be added, consult lvresize man page.
- As the LV is resized now we need to resize its LUKS counterpart. Run ‘cryptsetup resize crypt1’.
- You can check the new LV size by running ‘lvdisplay’ and the size of the LV’s LUKS counterpart by running ‘cryptsetup status crypt1’ – both should be the same. cryptsetup returns the size in sectors. To find out the size of a sector, run ‘fdisk -l /dev/mapper/crypt1 | grep “Sector size”‘.
- Now the PV, VG and LV have the correct size, but one another step is needed – to let the LV’s ext4 filesystem know that the LV is bigger. For that run ‘e2fsck -f /dev/mapper/crypt1’ and then ‘resize2fs -p /dev/mapper/crypt1’.
- Hopefully you’re done now 😉