Extending Mounted Ext4 File System on LVM in Linux
Posted on In Linux, TutorialLVM is a great tool to manage hard disks on Linux—you can abstract the hard drives away and manage logical volumes from volume groups, you can dynamically add or remove hard drives while the file systems on the logical volumes need not to backed up and recovered, and you may create many snapshots of the logical volumes as you like. In this post, I will introduce how to extend a mounted ext4 file system on a LVM logical volume on Linux.
This is a common situation that we may face: the file system such as the ext4 file system mounted to /home on a logical volume we allocated is almost used up and we may want to add a new hard drive to make it larger. LVM allows us achieve this easier.
Please note that the methods here only works with a mounted filesystem if the resize_inode
option active. The following command gives a line of output containing the resize_inode
if it is enabled using /dev/vg/lv_home
as an example (replace it with the path to you logical volume):
# tune2fs -l /dev/vg/lv_home | grep resize_inode
If your filesystem does not have the resize_inode
option active (a bit unusual, but possible), the filesystem needs to be unmounted first to be resized. (Thanks to Edward Kroeze for this note.)
In this post, I will use this example: we have a volume group vg
from which a logical volume lv_home
is mounted to /home
as an ext4 file system. Now, we have bought a new 1TB hard drive and installed it (assume /dev/sdb) to the computer, and are to extend the capacity of the /home
. The steps are followed. All commands are executed as root or by sudo.
Before you move on following the steps, make sure you know what you are doing and back up all you data.
Table of Contents
Extend vg with sdb
This section follows the tutorial from
First, create a new partition sdb1 to have all the capacity from sdb by using specific commands in cfdisk:
cfdisk /dev/sdb
Then, create a physical volume from /dev/sdb1 and extend the volume group vg
by vgextend
(vgextend manual):
pvcreate /dev/sdb1
vgextend vg /dev/sdb1
You can check the volume groups by vgdisplay
and see that vg
is extended with the addition capacity of around 931GB if everything goes well.
Extend the LV and filesystem: the quick and automatic way
You can extend the logical volume and let the LVM tool automatically resize the file system by lvresize
(lvresize manual):
lvresize --resizefs --size +931GB /dev/vg/lv_home
Note that this method also works for shrink the LV and file system size (bug with caution since it is a more danger operation than extending the size). But for shrinking a LV, it is better to umount the file system first to avoid data loss.
Extend the LV and filesystem: the manual way
In this manual way, we do it step by step.
Extend the logical volume
Now, let’s extend the logical volume lv_home with the capacity from the new hard drive by lvextend
(lvextend manual):
lvextend -L+931GB /dev/vg/lv_home
If it prints “Logical volume lv_home successfully resized”, it should be good now. You can use lvdisplay
to check the capacity of the logical volume.
Extend the ext4 file system
The resize2fs (resize2fs manual) can resize an ext4 file system on-line to use all available disk capacity. We can resize the /home
mounted by:
resize2fs /dev/vg/lv_home
It will prints output like follows if it executes successfully
resize2fs 1.42.9 (4-Feb-2014)
Filesystem at /dev/vg/lv_home is mounted on /home; on-line resizing required
old_desc_blocks = 18, new_desc_blocks = 77
The filesystem on /dev/vg/lv_home is now 319283200 blocks long.
You can use df -hT
to check the file system’s available capacity and start to saving more files under /home
now.
I’m very sorry for the noise, please remove my comment above, your example uses –resizefs option which does the trick. Thank you.
No worry. Glad you noticed the `–resizefs`. Yes, that’s the important option.
Worked great, thanks!
This only works online/with a mounted filesystem if the following command gives a line of output:
tune2fs -l /dev/vg/lv_home | grep resize_inode
If your filesystem does not have the resize_inode option active (a bit unusual, but possible), the filesystem needs to be unmounted first to be resized.
Great note. Thanks Edward.
thanks for this. tried this at first with system-config-lvm but it hung and one of the hard drives made a really scary noise. managed to salvage the data and the drives both check out ok with SMART, but as always i should have just started with the command line.
your guide worked. no bad noises. losts more room. thanks again
Great to know this helps!
You can achieve same in just one step;)
man lvextend | grep resizefs$ -A1
-r, –resizefs
Resize underlying filesystem together with the logical volume using fsadm(8).
Great steps
Thanks
Will that work with the root partition? ( / ) without a reboot?
Thanks
Yes. But, as always, backup first.
You could replace the extend command with this to take advantage of all the available space no matter how much it really is :
lvresize –resizefs -l +100%FREE /dev/vg/lv_home
Good tip!
Thank you for this mate! Was racking my head as to why my partition had not resized despite changing size of LV, turns out had not used resize2fs to extend the Filesystem as well. Cheers!
Razi
“Then, create a physical volume from /dev/sdb1”
WHY ?!?!?!?
why not using directly the entire disk and not create a new volume, JUST BE CUZ
also, that partition should be WHAT TYPE ?!?
we are not here to scratch your partitioning fetish
https://unix.stackexchange.com/questions/76588/what-is-the-best-practice-for-adding-disks-in-lvm