Categories
Linux Tutorials

LVM to the rescue

Root problem

I’ve been running Debian 12 for a while on my Linux desktop and recently ran into a big issue: My root volume ran out of space. Never good.

In the past I would’ve taken the path of least discomfort (for me): Clean install, repartition differently, start fresh. Because I’d used LVM when installing, however, I had another option this time:

On small systems (like a desktop), instead of having to estimate at installation time how big a partition might need to be, LVM allows filesystems to be easily resized as needed.

Wikipedia

Goal

I want to expand the root volume by shrinking the home volume a little bit. Let’s see how easy it really is with LVM!

Prerequisites

My environment was an x64 box running Debian 12 containing a single 1TB SSD. I didn’t add any physical drives, I only adjusted the space allotted to the LVM volumes.

Live image

I used a live install image to boot Linux without installing anything. After downloading the image I used dd to copy it to a USB flash drive. When booting on Debian, you’ll need to select the “Rescue mode” and follow the menus to get to a shell. When asked about using a root filesystem, tell it not to mount anything.

Figure out paths and numbers

To make this tutorial generic, we’re going to define four variables. Two for the volume paths, one for the amount we’re going to grow the smaller volume, and one for the amount we’re going to shrink the bigger volume.

Paths

There are plenty of ways to figure out the paths to the volumes you’ll be using, but lvdisplay works well.

shrinkPath = path of volume to want to shrink
growPath = path of volume to want to grow

Numbers

We’ll also need to define the growth amount of the growing volume and the new size of the shrinking volume.

growthAmount = how much we want the volume at growPath to grow
newShrinkSize = current size of the volume at shrinkPathgrowthAmount

Example

We have a root volume we want to grow by 30GB as well as a home volume (currently 900GB) that we need to shrink.

shrinkPath = /dev/debian-vg/home
growPath = /dev/debian-vg/root
growthAmount = 30G
newShrinkSize = 870G (which is 900-30)

Note: Gigabyte units are assumed in this example, hence the G after the 30 and 870.

Steps

⚠️ Complete these at your own risk! Any time you change filesystems you run the risk of data loss, so backup your stuff!

  1. Boot into a recovery mode without mounting any filesystems.
  2. e2fsck -f {shrinkPath} to check for errors.
    • Example: e2fsck -f /dev/debian-vg/home
  3. resize2fs {shrinkPath} {newShrinkSize} to shrink the filesystem.
    • Example: resize2fs /dev/debian-vg/home 870G
    • Important: Be sure to include the appropriate size unit after the new shrink size, like G for gigabytes. See the resize2fs man page.
  4. lvreduce -L -{growthAmount} {shrinkPath} to shrink the LVM volume.
    • Example: lvreduce -L -30G /dev/debian-vg/home
    • Note: Some tutorials accomplish steps 4 and 5 in one go via lvreduce --resizefs but this did not work for me, so I had to break it out into two steps.
    • Important: Be sure to include the appropriate size unit after the growth amount, like G for gigabytes. See the lvreduce man page.
  5. resize2fs {shrinkPath} to extend the partition to fit the volume.
    • Example: resize2fs /dev/debian-vg/home
  6. vgdisplay -C should show {growthAmount} free.
  7. e2fsck -f {growPath} to check for errors.
    • Example: e2fsck -f /dev/debian-vg/root
  8. lvextend -l +100%FREE {growPath} to grow the volume to 100% of the available Volume Group size.
    • Example: lvextend -l +100%FREE /dev/debian-vg/root
  9. resize2fs {growPath} to grow the filesystem to the available space.
    • Example: resize2fs /dev/debian-vg/root
  10. vgdisplay -C should show no free space
  11. You should be all done and free to reboot!