Extending a Mounted Ext4 File System on LVM in Linux

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.

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:

cfdisk /dev/sdb

Then, create a physical volume from /dev/sdb1 and extend the volume group vg by 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 (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 (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 (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.

Eric Zhiqiang Ma

Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

3 comments:

  1. I’m very sorry for the noise, please remove my comment above, your example uses –resizefs option which does the trick. Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *