|

Extending 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.

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 .

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.

Similar Posts

  • x86-64 ISA / Assembly Programming References

    This post collect the reference resource for x86-64 (also know as Intel 64, AMD 64) ISA / assembly language programming. x86-64 is a 64-bit extension of the Intel x86 instruction set. ==x86-64 Assembly Programming== Introduction to Computer Systems Resources (15-213 Introduction to Computer Systems Resources from CMU) Lots materials for learning machine-level programming on the…

  • | |

    Installing Xen on Fedora 20 as Domain-0

    I ever introduced [[installing-xen-domain-0-on-fedora-17]] as the first try to use the xen package delivered from Fedora and get away from manually compiling Xen and patching the kernel. In this post, I introduce installing Xen Dom0 on Fedora 20. Installing Xen First, install the xen pacakges: # yum install xen The Linux kernel is already ready…

  • Micosoft招聘部分算法题

    Micosoft招聘部分算法题 1.链表和数组的区别在哪里? 2.编写实现链表排序的一种算法。说明为什么你会选择用这样的方法? 3.编写实现数组排序的一种算法。说明为什么你会选择用这样的方法? 4.请编写能直接实现strstr()函数功能的代码。 5.编写反转字符串的程序,要求优化速度、优化空间。 6.在链表里如何发现循环链接? 7.给出洗牌的一个算法,并将洗好的牌存储在一个整形数组里。 8.写一个函数,检查字符是否是整数,如果是,返回其整数值。(或者:怎样只用4行代码编写出一个从字符串到长整形的函数?) 9.给出一个函数来输出一个字符串的所有排列。 10.请编写实现malloc()内存分配函数功能一样的代码。 11.给出一个函数来复制两个字符串A和B。字符串A的后几个字节和字符串B的前几个字节重叠。 12.怎样编写一个程序,把一个有序整数数组放到二叉树中? 13.怎样从顶部开始逐层打印二叉树结点数据?请编程。 14.怎样把一个链表掉个顺序(也就是反序,注意链表的边界条件并考虑空链表)? 来源:·日月光华 bbs.fudan.edu.cn Read more: How to Connect to MySQL in JSP 一个非常优秀的MFC Grid控件 Vim Indenting C/C++ Code GNU glibc Manual How to Generate and Apply Patches using diff and patch on Linux C++ Reference and Styles Push and Pull data…

18 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.

  2. 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.

  3. 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

  4. 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).

  5. 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

  6. 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

  7. “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

Leave a Reply

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