Resizing Encrypted LVM Partitions on Linux
I found recently that I needed to resize some partitions on my new laptop as the Ubuntu installer helpfully decided to create only a 1GB swap partition, which is nowhere near enough for hibernation (you need a swap partition that's at least as big as your computer's RAM in order to hibernate). Unfortunately resizing my swap partition didn't allow me to hibernate successfully in the end, but I thought I'd still document the process here for future reference should I need to do it again in the future.
The key problem with resizing one's root partition is that you can't resize it without unmounting it, and you can't unmount it without turning off your computer. To get around this, we need to use a live distribution of Ubuntu. It doesn't actually matter how you boot into this - personally my preferred method is by using a multiboot USB flash drive, but you could just as well flash the latest ubuntu ISO to a flash drive directly.
Before you start though, it's worth mentioning that you really should have a solid backup strategy. While everything will probably be fine, there is a chance that you'll make a mistake and wind up loosing a lot of data. My favourite website that illustrates this is The Tao of Backup. Everyone who uses a computer (technically minded or not) should read it. Another way to remember it is the 3-2-1 rule: 3 backups, in 2 locations, with 1 off-site (i.e. in a different physical location).
Anyway, once you've booted into a live Ubuntu environment, open the terminal, and start a root shell. Your live distribution should come with LUKS and LVM already, but just in case it doesn't execute the following:
sudo apt update && sudo apt install -y lvm2 cryptsetup
I've talked about LVM recently when I was setting up an LVM-managed partition on an extra data hard drive for my research data. If you've read that post, then the process here may feel a little familiar to you. In this case, we're interacting with a pre-existing LVM setup that's encrypted with LUKS instead of setting up a new one. The overall process look a bit like this:
With this in mind, let's get started. The first order of business is unlocking the LUKS encryption on the drive. This is done like so:
sudo modprobe dm-crypt
sudo cryptsetup luksOpen /dev/nvme0n1p3 crypt1
The first command there ensures that the LUKS kernel module is loaded if it isn't already, and the second unlocks the LUKS-encrypted drive. Replace /dev/nvme0n1p3
with the path to your LVM partition - e.g. /dev/sda1
for instance. The second command will prompt you for the password to unlock the drive.
It's worth mentioning here before continuing the difference between physical partitions and LVM partitions. Physical partitions are those found in the partition table on the physical disk itself, that you may find in a partition manage like GParted.
LVM partitions - for the purpose of this blog post - are those exposed by LVM. They are virtual partitions that don't have a physical counterpart on disk and are handled internally by LVM. As far as I know, you can't ask LVM easily where it stores them on disk - this is calculated and managed automatically for you.
In order to access our logical LVM partitions, the next step is to bring up LVM. To do this, we need to get LVM to re-scan the available physical partitions since we've just unlocked the one we want it to use:
sudo vgscan --mknodes
Then, we activate it:
sudo vgchange -ay
At this point, we can now do our maintenance and make any changes we need to. A good command to remember here is lvdisplay
, which lists all the available LVM partitions and their paths:
sudo lvdisplay
In my case, I have /dev/vgubuntu/root
and /dev/vgubuntu/swap_1
. tldr-pages (for which I'm a maintainer) has a number of great LVM-related pages that were contributed relatively recently which are really helpful here. For example, to resize a logical LVM partition to be a specific size, do something like this:
sudo lvresize -L 32G /dev/vgubuntu/root
To extend a partition to fill all the remaining available free space, do something like this:
sudo lvextend -l +100%FREE /dev/vgubuntu/root
After resizing a partition, don't forget to run resize2fs
. It ensures that the ext4 filesystem on top matches the same size as the logical LVM partition:
sudo resize2fs /dev/vgubuntu/root
In all of the above, replace /dev/vgubuntu/root
with the path to your logical LVM partition in question of course.
Once you're done making changes, we need to stop LVM and close the LUKS encrypted disk to ensure all the changes are saved properly and to avoid any issues. This is done like so:
sudo vgchange -an
sudo cryptsetup luksClose crypt1
With that, you're done! You can now reboot / shutdown from inside the live Ubuntu environment and boot back into your main operating system. All done!
Found this helpful? Encountering issues? Comment below! It really helps my motivation.