ext4 is the default filesystem for most Debian-family
distributions and a reliable choice for production. Understanding
its features, tuning, and operations makes the difference between
a healthy filesystem and a chronic performance problem.
Creating an ext4 filesystem
Read-only / Safemkfs.ext4— mkfs.ext4 -L sets a label; -m 0 reserves 0% for root (only safe when the filesystem is dedicated to data, not shared with system); -O enables features. The superblock backup list at the end shows the locations used for crash recovery.
$ mkfs.ext4 -L data -m 0 -O has_journal,extent,huge_file,dir_nlink,extra_isize,metadata_csum /dev/sda1
mke2fs 1.47.0 (5-Feb-2023)
Creating filesystem with 26214400 4k blocks and 6553600 inodes
Filesystem UUID: 1a2b-3c4d-5678-9abc-def012345678
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000
Allocating group tables: done
Writing inode tables: done
Creating journal (65536 blocks): done
Writing superblocks and filesystem accounting information: done
Mount options
Configuration changeext4 mount options— noatime disables access-time updates (3-5% throughput win on workloads with many small reads). errors=remount-ro remounts the filesystem read-only on errors - safer than continuing with a damaged filesystem.
$ mount -o noatime,errors=remount-ro /dev/sda1 /data
Operations
Resizing
Configuration changeresize2fs— resize2fs enlarges or shrinks an ext2/3/4 filesystem, and the order of operations is different for each direction. GROW is online: extend the block device first (lvextend, or grow the partition), then run resize2fs against the mounted filesystem. SHRINK is offline and the order is the exact opposite: umount, run e2fsck -f (resize2fs refuses to shrink without a clean forced check), run resize2fs DEVICE NEWSIZE, and only then lvreduce to a size equal to or larger than the new filesystem. Shrinking the block device before the filesystem truncates live data and is unrecoverable without a restore.
$ resize2fs /dev/sda1
resize2fs 1.47.0 (5-Feb-2023)
Resizing the filesystem on /dev/sda1 to 524288 (4k) blocks.
The filesystem on /dev/sda1 is now 524288 (4k) blocks long.
Shrinking safely
Growing is forgiving. Shrinking is not. The filesystem records
its own size in the superblock and in the block group
descriptors. If the block device becomes smaller than the size
the filesystem believes it has, every block group past the new
end of the device is gone. The filesystem will not mount,
e2fsck reports superblock and group descriptor corruption, and
the only recovery is a restore from backup.
Take (or verify) a backup. Shrink is the one resize direction with no undo.
Check how much space is actually in use with df -h. The new size must be comfortably larger than the used space.
Unmount the filesystem: umount /data. ext4 cannot shrink while mounted, in any mode.
Run a forced check: e2fsck -f /dev/vg0/data. resize2fs refuses to shrink a filesystem that has not just been checked.
Shrink the filesystem: resize2fs /dev/vg0/data 50G. Wait for it to finish and read the reported new block count.
Only now shrink the volume: lvreduce -L 50G /dev/vg0/data. Never give lvreduce a size smaller than the new filesystem size.
Remount and verify: mount /data, then df -h /data.
If you would rather not drive the sequence by hand, let LVM do it
for you:
Data-loss risklvreduce -r— The -r (--resizefs) flag makes lvreduce call fsadm, which unmounts, checks, and shrinks the filesystem before it touches any extents. This is the safest way to shrink ext4 because the ordering is enforced for you. fsadm refuses to do this on XFS, because XFS cannot be shrunk at all.
# lvreduce -r -L 50G /dev/vg0/data
Do you want to unmount /data ? [Y|n] y
fsck from util-linux 2.39.3
/dev/mapper/vg0-data: 12345/6553600 files (0.1% non-contiguous), 234567/52428800 blocks
resize2fs 1.47.0 (5-Feb-2023)
Resizing the filesystem on /dev/mapper/vg0-data to 13107200 (4k) blocks.
The filesystem on /dev/mapper/vg0-data is now 13107200 (4k) blocks long.
Size of logical volume vg0/data changed from 200.00 GiB to 50.00 GiB.
Logical volume vg0/data successfully resized.
Illustrative output
Online defragmentation
Read-only / Safee4defrag— e4defrag is the online defragmenter for ext4. Most workloads do not need it - ext4's extent-based allocation fragments less than ext3. Use it when you have measured fragmentation causing performance issues.
$ e4defrag /data
e4defrag 1.47.0 (5-Feb-2023)
ext4 file system health check: 95% fragmented
extent-size size extents before after
/data/bigfile 1.0G 1 1024K 1
/data/other 2.0G 200 ...
...
Fragmented files: 12
Total extents: 12345
Improved extents: 5000
Files defragmented: 12
Filesystem check
Read-only / Safee2fsck -n— e2fsck -n is a read-only filesystem check. Use this on a live filesystem to verify metadata is consistent. -p is automatic repair. Run only on unmounted or read-only-mounted filesystems for full safety.
Read-only / Safetune2fs -l— tune2fs prints filesystem metadata. Check interval 0 disables forced fsck (production: keep at 0 for most hosts - rely on journaling). Maximum mount count -1 disables forced check at boot. Reserved 5% is the default for root, 0 for data.