More Disk Space With LVM

Man holding two floppy disks
I’m starting to really like LVM.

See, the other day I had a problem with a virtual machine I was using, starting to run out of space. I’d only given it 10GB (well it wasn’t very demanding), and the OS auto-installer decided to divide this into ~6.5GB data and ~3GB swap. Sadly, it was a VMDK virtual disk rather than a VDI; VDIs are resizeable on the fly. I was faced the with thought of either cloning it to a larger virtual disk, or worse, rebuilding the whole image from scratch. Not having several hours to spare, I discovered that the OS had installed itself on LVM, and lo, I was saved.

First, I reduced the amount of swap used:

swapoff /dev/vg_clusterhost/lv_swap
lvreduce -L1.5G /dev/vg_clusterhost/lv_swap
lvextend -L+1.42G /dev/vg_clusterhost/lv_root
resize2fs -p /dev/vg_clusterhost/lv_root
mkswap /dev/vg_clusterhost/lv_swap
swapon /dev/vg_clusterhost/lv_swap

swapoff /dev/vg_boxhost/lv_swap

lvreduce -L1.5G /dev/vg_boxhost/lv_swap

mkswap /dev/vg_boxhost/lv_swap

swapon /dev/vg_boxhost/lv_swap

That little bundle of code turns off swap on my LVM partition, shrinks it to 1.5GB, and sets it up as a swap partition again.

Now with all that newfound free space in my volume group, I can stretch out my data volume:


lvextend -l +100%PVS /dev/vg_boxhost/lv_root

resize2fs -p /dev/vg_boxhost/lv_root

First, add 100% of the remaining free space in the physical volume to the logical volume, then stretch out the filesystem to fill the volume.

The whole thing took seconds rather than the hours I’d have needed to clone or rebuild. LVM, eh?

Image courtesy of nhoj leunamme, licensed under CC BY-NC-ND 2.0

Advertisement