How to resize LVM logical volumes with ext4 as filesystem
Ever been in the situation where you needed to save some important files to a server and your greeted with “Not enough space left on device”-kind messages? No? Well, as that happened too often too me for my liking, I decided to do it right this time when I set up my home server and use logical volume manager (LVM) straight from the start. So basically, all I had to do was to shrink a filesystem that had free space in it and its partition (logical volume (LV) to be precisely) afterwards and then to resize the logical volume/filesystem where I needed the space.
As all the necessary tools are available normally on a system with LVM support, I could dive right in:
# Unmount the filesystem and check its' LV umount /mnt/foo e2fsck -f /dev/mapper/vg0-foo # Shrink ext4 and then the LV to the desired size resize2fs -p /dev/mapper/vg0-foo 40G lvreduce -L 40G /dev/mapper/vg0-foo # Before continuing, run e2fsck. If it bails because the partition # is too small, don't panic! The LV can still be extended with # lvextend until e2fsck succeeds, e.g.: # lvextend -L +1G /dev/mapper/vg0-foo e2fsck -f /dev/mapper/vg0-foo # Resize the filesystem to match the LVs size, check and mount it resize2fs -p /dev/mapper/vg0-foo e2fsck -f /dev/mapper/vg0-foo mount /mnt/foo
That was the tricky part. The rest is pretty straight forward:
- unmount the filesystem,
- extend the logical volume and
- expand the filesystem afterwards.
umount /mnt/bar # Extend the LV to use all free space lvextend -l +100%FREE /dev/mapper/vg0-bar e2fsck -f /dev/mapper/vg0-bar # Resize the partition to fill the LV resize2fs -p /dev/mapper/vg0-bar e2fsck -f /dev/mapper/vg0-bar mount /mnt/bar
References
- http://tldp.org/HOWTO/LVM-HOWTO/reducelv.html
- man(8) resize2fs
- man(8) lvextend
- man(8) lvreduce
This guide was really simple and really helpful. I used it to enlarge my / partition using the Ubuntu Rescue Mode. Thanks for the help!
Thxs buddy…
Try this link, very easy to understand
http://www.redhatlinux.info/2010/11/lvm-logical-volume-manager.html
Many thanks for this tips !!!
Handy reference to do the exact thing I’m doing now.
Thanks.
Steps 16/17 aren’t necessary if line 13 is successful no? Thx
Well that depends. If you _don’t_ want your partition to fill the LV completely, then yes, they are not necessary.
That’s peculiar to LVM somehow measuring the closest size differently from ext4, actually; and it doesn’t explain all the fscking when ext4 can be resized live (subject to programs and scripts that freak right out at something as pedestrian now as volume/partition/lvm/filesystem/cloud changes. Are you hardness testing versus filesystem fuzzing?) I don’t even get a certificate of having defragged twice for all that wear ‘n tear down.
Part 2’s lines 5 and 9 (fsck, as if lvm cared or resize2fs was inadequate,) are pure garbage (better than tainted garbage, but meh.)
Steve,
resize2fs (at least in some environments) forces you to do a fsck -f beforehand. The one after isn’t absolutely necessary, but as it’s EXT4 it’s a quick procedure and certainly isn’t going to hurt anything.
Thank you so much. Super painless.
I think there is a small typo in your second command snippet:
Should not this line
# Extend the LV to use all free space
lvextend -l +100%FREE /dev/mapper/vg0-bar
be instead like this?
lvextend -L +100%FREE /dev/mapper/vg0-bar
No,
-l
is just fine because I want the LV to use the remaining space and that is only possible with that parameter. From the man page:{-l|--extents
[+]LogicalExtentsNumber[%{VG|LV|PVS|FREE|ORIGIN}] | -L|--size
[+]LogicalVolumeSize[bBsSkKmMgGtTpPeE]}
Why not just use lvreduce/lvextend with -r key, which auto-resizes fs to match new LV size?