LVM 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.
Extend vg with sdb ∞
This section follows the tutorial from Extending a LVM Volume Group.
First, create a new partition sdb1 to have all the capacity from sdb by using specific commands in cfdisk:
Then, create a physical volume from /dev/sdb1 and extend the volume group
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 --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 -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 (manual) can resize an ext4 file system on-line to use all available disk capacity. We can resize the
/home mounted by:
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