LVM Walkthrough

Created Thursday 11 April 2019

For this example, I have used a thumb drive that manifests as /dev/sdd. It's 8G (shows up as 7.5 because marketing).

For starters, I created a partition table that has:

A primary partition of 1G of type 0c (Windows)
An extended partition of 2G which is an LVM Physical Volume that has:
A volume group containing only this physical, that has:
Two 1000M logical volumes:
testvg-test1, 1000MB, mounted at /mnt/test1
testvg-test2, 1000MB, mounted at /mnt/test2

When you df these, you get this:

# df -h /mnt/test?
Filesystem                Size  Used Avail Use% Mounted on
/dev/mapper/testvg-test1  969M  1.3M  902M   1% /mnt/test1
/dev/mapper/testvg-test2  969M  1.3M  902M   1% /mnt/test2

Now, at this point, the logical partition of 2G is more-or-less fully occupied (there might be a few MB unused in there, but that's not important). I modified the logical partition so that it now occupies the entire extended partition, making it 6.5GB.

We need, at this point, to grow the physical partition. This command
will grow it to fill the available space:

# pvresize /dev/sdd5
  Physical volume "/dev/sdd5" changed
  1 physical volume(s) resized / 0 physical volume(s) not resized

When we do this, there's no need to grow the volume group, because it will grow to the size of the physical volumes that make it up. As such, no need to do anything with it.

Next, we need to grow the logical volume. This will grow test1 to fill all of the available free space in the volume group:

# lvresize /dev/testvg/test1 -l+100%FREE
  Size of logical volume testvg/test1 changed from 1000.00 MiB (250 extents) to 5.49 GiB (1405 extents).
  Logical volume test1 successfully resized.

As you might imagine, you can change that number from 100% to whatever other percentage you want, and also, you can specify the amount of space in other terms. Look at the lvresize man page if your're curious.

Now, at this point, df will still show the same sizes, because the fielsystem is still only occupying the first 1000M of the logical volume. Just to prove the point:

# df -h /mnt/test?
Filesystem                Size  Used Avail Use% Mounted on
/dev/mapper/testvg-test1  969M  1.3M  902M   1% /mnt/test1
/dev/mapper/testvg-test2  969M  1.3M  902M   1% /mnt/test2

The good news here is that we don't even need to unmount it to resize it. Just do this:

# resize2fs /dev/mapper/testvg-test1
resize2fs 1.42.13 (17-May-2015)
Filesystem at /dev/mapper/testvg-test1 is mounted on /mnt/test1; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/mapper/testvg-test1 is now 1438720 (4k) blocks long.

And just like that, we now have all the space:

# df -h /mnt/test?
Filesystem                Size  Used Avail Use% Mounted on
/dev/mapper/testvg-test1  5.4G  2.0M  5.2G   1% /mnt/test1
/dev/mapper/testvg-test2  969M  1.3M  902M   1% /mnt/test2