23

For resizing LVM2 partition, one needs to perform the following 2 commands:

# lvextend -L+1G /dev/myvg/homevol
# resize2fs /dev/myvg/homevol

However, when I perform lvextend, I see that the changes are already applied to the partition (as shown in Gnome Disks). So why do I still need to do resize2fs?

4 Answers 4

39

The lvextend command (without the --resizefs option) only makes the LVM-side arrangements to enlarge the block device that is the logical volume. No matter what the filesystem type (or even whether or not there is a filesystem at all) on the LV, these operations are always similar.

If the LV contains an ext2/3/4 filesystem, the next step is to update the filesystem metadata to make the filesystem aware that it has the more space available, and to create/extend the necessary metadata structures to manage the added space. In the case of ext2/3/4 filesystems, this involves at least:

  • creating new inodes to the added space
  • extending the block allocation data structures so that the filesystem can tell whether any block of the added space is in use or free
  • potentially moving some data blocks around if they are in the way of the previously-mentioned data structure extension

This part is specific to the filesystem type, although the ext2/3/4 filesystem types are similar enough that they can all be resized with a single resize2fs tool. For XFS, filesystems, you would use a xfs_growfs tool instead. Other filesystems may have their own extension tools. And if the logical volume did not contain a filesystem but instead something like a "raw" database or an Oracle ASM volume, a yet another procedure would need to be applied.

Each filesystem has different internal workings and so the conditions for extending a filesystem will be different for each. It took a while until a common API was designed for filesystem extension; that made it possible to implement the fsadm resize command, which provides an unified syntax for extending several filesystem types. The --resizefs option of lvextend just uses the fsadm resize command.

In a nutshell: After lvextend, LVM-level tools such as lvs, vgs, lvdisplay and vgdisplay will see the updated size, but the filesystem and any tools operating on it, like df, won't see it yet.

4
  • 2
    Would it be right to say that after running lvextend and prior to running resize2fs, df would NOT report the updated sizes?
    – Rufus
    Commented Jan 14, 2018 at 8:25
  • Exactly. (Added to my answer.)
    – telcoM
    Commented Jan 14, 2018 at 8:37
  • Thanks. What does "creating new inodes to the added space" mean, given that the added space is unused yet, and inodes exist for existing files only?
    – Tim
    Commented Feb 21, 2019 at 14:22
  • For modern filesystems like XFS it is so. However, the ext2/3/4 filesystems follow a more classic design, with a pre-allocated table of inodes. At filesystem creation time, a ratio of (maximum number of inodes) / (filesystem size) is set, and that cannot be changed as an on-line operation. As a result, the resize2fs needs to extend the inode table to maintain the ratio as the filesystem is extended.
    – telcoM
    Commented Mar 10, 2019 at 8:56
15

The LVM layer is just a container for the filesystem within. You can tell lvextend to resize the filesystem within the logical volume without having to run a separate resize2fs adding the -r (or --resizefs) option:

lvextend -r ...
1
  • 1
    I was searching that option, beaten by seconds. +1 Commented Jan 13, 2018 at 19:27
6

Because filesystems and logical volumes are different abstractions. A volume is a chunk of disk (like a partition) — or at least, the "virtual" equivalent. It's just a block device. A filesystem is a structure that goes inside of that (or, on top of it, if you prefer) and which is used to provide a mapping between files (and directories and so on) to that device. Without resize2fs, the partition is bigger, but the filesystem isn't taking advantage of the available space.

You can actually create a filesystem initially which does not take up the entire partition. From the mke2fs man page:

The file system size is specified by fs-size. If fs-size does not have a suffix, it is interpreted as power-of-two kilobytes, unless the -b blocksize option is specified, in which case fs-size is interpreted as the number of blocksize blocks. If the fs-size is suffixed by 'k', 'm', 'g', 't' (either upper-case or lower-case), then it is interpreted in power-of-two kilobytes, megabytes, gigabytes, terabytes, etc. If fs-size is omitted, mke2fs will create the file system based on the device size.

As you can see, the default is to fill the partition, and there's usually no reason to do otherwise — but you can if you want.

Note that when lvextend has an option --resizefs (or just -r) which takes care of growing the filesystem after the volume has been extended without needing to run a separate command.

1

The resize2fs forces the fs, filesystem to expand or shrink to take up the new space produced by the lvextend command. The processes:

with fs:
        -- shrink --> 1. shrink fs 2. shrink volume
        -- expand --> 1. expand volume, 2. expand fs

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .