Sub topic 7
Growing the ext4 or XFS filesystem
This chapter assumes you can use the Linux command line with sudo and read lsblk and df output. It also assumes you know what a mount point is and have met ext4 and XFS before. You should have already extended a logical volume (LV) with lvextend. Here you grow the filesystem inside it.
Why the filesystem needs its own step
An LV is a block device. A filesystem lives on top of that device and records its own size in its metadata. When you run lvextend, the block device gets bigger. The filesystem does not notice, so df keeps reporting the old size.
Growing the filesystem means updating its metadata. It adds new block groups (ext4) or allocation groups (XFS) so it can use the added blocks.
Between step B and step C the system is in a consistent but mismatched state. The LV is larger than the filesystem on it. This is harmless, and you can finish the job later. The unused tail of the device is just not available to files yet.
Identify the filesystem type first
The grow command depends on the filesystem type, so check it before you run anything.
sudo pvs
sudo vgs
sudo lvs
lsblk -f
df -hT /srv/dataLook at the Type column in df -hT, or the FSTYPE column in lsblk -f. You will see ext4 or xfs. Do not guess from the distribution. Red Hat family systems default to XFS, and Debian and Ubuntu systems default to ext4. Individual volumes can differ from the default.
You can also confirm it directly:
findmnt -no FSTYPE /srv/data
sudo blkid /dev/vg_data/lv_appfindmnt reads the mount table. blkid reads the superblock on the device, so it also works when the volume is not mounted.
Growing ext4 with resize2fs
resize2fs takes the device path. By default it grows the filesystem to fill the whole device.
# ext4 (works while mounted), takes the device path:
sudo resize2fs /dev/vg_data/lv_appTypical output looks like this:
resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/vg_data/lv_app is mounted on /srv/data; on-line resizing required
old_desc_blocks = 13, new_desc_blocks = 15
The filesystem on /dev/vg_data/lv_app is now 7864320 (4k) blocks long.The words "on-line resizing required" show that the filesystem was mounted and stayed in use. Applications keep reading and writing during the grow. Online growing of ext4 has been supported for a long time on current kernels.
Growing to a specific size
You can pass a size as the second argument. This is useful when you want the filesystem smaller than the LV, for example to leave room for a later change.
sudo resize2fs /dev/vg_data/lv_app 80GWithout a size argument, the tool uses all available space. Sizes without a suffix are counted in filesystem blocks. Add K, M, G, or T for readable units.
ext4 details worth knowing
- The device path can be
/dev/vg_data/lv_appor/dev/mapper/vg_data-lv_app. Both point to the same device. - Online growing works. Online shrinking does not. Shrinking ext4 requires an unmount and a prior
e2fsck -f. - If the filesystem was created with the
resize_inodefeature, the kernel reserves room in the group descriptor table for growth. Very large growth factors, such as growing a 1 GiB filesystem to several terabytes, can exceed that reservation. In that case the online resize may fail. Newermkfs.ext4versions with themeta_bgfeature avoid this limit.
Growing XFS with xfs_growfs
xfs_growfs works differently from resize2fs in two ways. It takes the mount point, not the device. It also requires the filesystem to be mounted.
# XFS (must be mounted), takes the mount point:
sudo xfs_growfs /srv/dataTypical output is a summary of the geometry:
meta-data=/dev/mapper/vg_data-lv_app isize=512 agcount=4, agsize=1310720 blks
= sectsz=512 attr=2, projid32bit=1
data = bsize=4096 blocks=5242880, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=2560, version=2
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 5242880 to 10485760The last line is the one to read. It shows the block count before and after the grow. If both numbers are equal, the LV was not larger than the filesystem, and nothing changed.
XFS details worth knowing
- XFS can grow online only. If you try
xfs_growfson an unmounted device, it fails. Mount the filesystem first. - XFS can never be shrunk. There is no supported tool for it. The only way to reduce an XFS volume is to back up the data, recreate a smaller filesystem, and restore. Treat every XFS grow as permanent.
- If you pass a device path rather than the mount point, current versions of
xfs_growfsmay warn or fail. Use the mount point. - To grow to a specific size instead of the maximum, use
-Dwith a size in filesystem blocks, for examplesudo xfs_growfs -D 10485760 /srv/data.
Comparing the two tools
| Aspect | ext4: `resize2fs` | XFS: `xfs_growfs` |
|---|---|---|
| Argument | Device path | Mount point |
| Mounted required | No, works mounted or unmounted | Yes |
| Online grow | Yes | Yes |
| Shrink | Offline only | Not possible |
| Default target | Whole device | Whole device |
| Check type with | `df -T`, `blkid` | `df -T`, `blkid` |
Doing both steps in one command
The -r (--resizefs) option makes lvextend call the right filesystem tool after it extends the volume. It detects the type and runs resize2fs for ext4 or xfs_growfs for XFS. It uses fsadm internally.
sudo lvextend -r -L +20G /dev/vg_data/lv_app
df -hT /srv/dataThis is the form most administrators use day to day, because it removes the chance of forgetting the second step. Use the two-step form when you want to inspect the LV between the steps, or when a script needs to handle a failure of one step separately.
If the volume group has no free space, lvextend fails before it touches anything. The filesystem step does not run.
A worked example
You have an ext4 volume lv_app on vg_data, mounted at /srv/data. It is 50 GiB and 94% full. The volume group has 40 GiB free.
- Check the starting state.
``bash df -hT /srv/data sudo vgs vg_data ``
- Extend the LV and the filesystem together.
``bash sudo lvextend -r -L +20G /dev/vg_data/lv_app ``
- Confirm the result.
``bash df -hT /srv/data sudo lvs vg_data ``
After step 2, df should report about 70 GiB. vgs should now show 20 GiB free. If df still shows 50 GiB, the filesystem step did not run. Run resize2fs /dev/vg_data/lv_app by hand.
Verification checks
A grow is not done until three layers agree. Compare them:
sudo lvs -o lv_name,lv_size vg_data
lsblk /dev/vg_data/lv_app
df -hT /srv/datalvsshows the LV size, which is the block device size.lsblkshows the same size from the kernel's view.dfshows the filesystem size, which is slightly smaller than the LV because of metadata and the journal.
The df figure will not match the LV exactly. A difference of a few percent is normal. A difference of the full amount you added means the filesystem step is missing.
For a deeper check on ext4, read the block count from the superblock:
sudo tune2fs -l /dev/vg_data/lv_app | grep -E 'Block count|Block size'For XFS, read the data block count:
sudo xfs_info /srv/data | grep dataMultiply the block count by the block size to get bytes. This should match the LV size to within a small margin.
Common errors and what they mean
| Message or symptom | Likely cause | Fix |
|---|---|---|
| `df` shows old size after `lvextend` | Filesystem step skipped | Run `resize2fs` or `xfs_growfs` |
| `xfs_growfs: /dev/... is not a mounted XFS filesystem` | Device path given, or volume unmounted | Mount the volume and pass the mount point |
| `resize2fs: Bad magic number in super-block` | The device holds XFS, not ext4 | Check the type with `blkid`, use `xfs_growfs` |
| `Insufficient free space` from `lvextend` | VG has fewer free extents than requested | Add a PV or request a smaller size |
| `The filesystem is already N blocks long. Nothing to do!` | LV was not enlarged, or it was already grown | Check `lvs`, then `df` |
| `resize2fs: Permission denied` while trying to resize | Missing sudo | Re-run with `sudo` |
The "Bad magic number" error is the most common one when someone copies a command from a different system. It never damages data. The tool simply fails to find an ext4 superblock and stops.
Safety and limits
Growing is among the safer LVM operations. The filesystem tools only add space and update metadata. Even so, keep these points in mind.
- Take a backup or a snapshot of anything important before you start. A power loss or a hardware fault during a metadata update is rare, but recovery from it is costly.
- Do not run
lvreduceon a volume you meant to grow. The two commands differ by one word. Reducing an LV below the filesystem size destroys data. If you ever shrink ext4, shrink the filesystem first and the LV second. XFS cannot be shrunk at all. - Do not run a grow on a volume with filesystem errors. Run a check first. For ext4, unmount and run
e2fsck -f. For XFS, usexfs_repairon an unmounted volume. - Grow the root filesystem the same way. Both tools work on a mounted root. For XFS root, the mount point is
/. - Growing is permanent for XFS and effectively permanent for any filesystem you cannot take offline.
Summary of the procedure
- Run
df -hTon the mount point and note the filesystem type. - Confirm the volume group has free space with
vgs. - Extend the LV with
lvextend, or use-rto grow the filesystem in the same command. - If you did not use
-r, runresize2fs <device>for ext4 orxfs_growfs <mount point>for XFS. - Verify with
lvs,lsblk, anddf.
Exercise
A server has an XFS volume mounted at /srv/data, backed by /dev/vgdata/lvapp. You ran sudo lvextend -L +20G /dev/vg_data/lv_app, but df -h /srv/data still shows the old size. Which command fixes this, and which argument does it take? Would the answer change if the filesystem were ext4?
Show answer
Run sudo xfs_growfs /srv/data. It takes the mount point, and the volume must be mounted. For ext4 you would run sudo resize2fs /dev/vg_data/lv_app, which takes the device path. In both cases lvextend -r would have done the LV and filesystem steps together.
Exercise
A colleague says they will free space by shrinking an oversized XFS volume with lvreduce after growing a different volume. Why is this a problem, and what is the safe alternative?
Show answer
XFS cannot be shrunk. Running lvreduce on it would cut the device below the filesystem size and destroy data. The safe approach is to back up the data, create a new smaller LV with a fresh filesystem, restore the data, and then remove the old volume. If you only need the space in the volume group, this is the only route for XFS.
Exercise
After sudo lvextend -r -L +10G /dev/vg_data/lv_app, lvs shows the LV grew by 10 GiB, but df shows only about 9.8 GiB more. Is something wrong?
Show answer
No. df reports usable filesystem space, which excludes metadata such as the journal and inode tables. A small gap is normal. A gap equal to the full 10 GiB would mean the filesystem step did not run.