Skip to main content
RunBook Academy

LinuxXIV · Filesystemsext4

ext4 deep dive - features, tuning, and operations

Intermediate⏱ ~12 minbashmkfs.ext4tune2fse2fsckdfdu

What you'll learn

  • Explain ext4 extents, journal modes, and mount options
  • Tune ext4 for performance and durability
  • Run online defragmentation and resize safely
  • Use tune2fs and e2fsck effectively

Prerequisites

Verified against Ubuntu 24.04 LTS · Debian 12 (Bookworm) · RHEL 9.x · Rocky Linux 9.x · AlmaLinux 9.x · Linux kernel 6.1 LTS / 6.6 LTS · systemd 255+ · OpenSSH 8.7p1 (RHEL 9) / 9.6p1 (Ubuntu 24.04) · nftables 1.0.x · chrony 4.x · Pacemaker 2.1.x · Corosync 3.1.x · 2026-08-09

Not yet marked complete on this device.

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 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
$ mount -o noatime,errors=remount-ro /dev/sda1 /data

Operations

Resizing

Configuration changeresize2fs
$ 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.

  1. Take (or verify) a backup. Shrink is the one resize direction with no undo.
  2. Check how much space is actually in use with df -h. The new size must be comfortably larger than the used space.
  3. Unmount the filesystem: umount /data. ext4 cannot shrink while mounted, in any mode.
  4. Run a forced check: e2fsck -f /dev/vg0/data. resize2fs refuses to shrink a filesystem that has not just been checked.
  5. Shrink the filesystem: resize2fs /dev/vg0/data 50G. Wait for it to finish and read the reported new block count.
  6. Only now shrink the volume: lvreduce -L 50G /dev/vg0/data. Never give lvreduce a size smaller than the new filesystem size.
  7. 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
# 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 /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 /dev/sda1
e2fsck 1.47.0 (5-Feb-2023)
/dev/sda1: clean, 12345/6553600 files, 234567/26214400 blocks

Tuning with tune2fs

Read-only / Safetune2fs -l
$ tune2fs -l /dev/sda1 | grep -E 'Block size|Mount count|Check interval|Reserved'
Block size:               4096
Mount count:              47
Maximum mount count:      -1
Check interval:           0 (disabled)
Reserved block count:     1310720 (5.00%)

Knowledge check

Knowledge check · 3 questions

  1. Q1. What does the noatime mount option do?

  2. Q2. ext4 can be grown while mounted, but shrinking it requires unmounting first.

  3. Q3. Which of the following are correct for ext4 production tuning? Select all that apply.

Passing score: 75%. Answers are checked in this browser.