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.

  1. Backup your data.
  2. 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.
  3. Prepare live USB/CD, e.g. per instructions here https://fedoraproject.org/wiki/How_to_create_and_use_Live_USB and boot to it.
  4. 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.
  5. Open terminal and switch to root.
  6. 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.
  7. 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.
  8. 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.
  9. 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.
  10. As the LV is resized now we need to resize its LUKS counterpart. Run ‘cryptsetup resize crypt1’.
  11. 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”‘.
  12. 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’.
  13. Hopefully you’re done now 😉

Leave a Reply

Your email address will not be published. Required fields are marked *