12.2. Administering Ext4

Ext4 is a mature in-place filesystem. Its superblock, block group descriptors, inode tables, extent trees, and journal are all familiar structures, and its tools can inspect and modify nearly every one of them. The same flexibility is the reason to be careful: tune2fs and debugfs can change metadata that a running filesystem assumes, and e2fsck should normally see an unmounted filesystem.

12.2.1. Creation Options, Labels, and Reserved Blocks

The practice disk starts blank. Create a GPT with four partitions, each named for its later role. The command is destructive; confirm that /dev/vdb is the disk you intend to erase. parted -s runs the subcommands in order without asking for confirmation, so the command itself is the confirmation:

# parted -s /dev/vdb mklabel gpt mkpart ext4 1MiB 1501MiB mkpart btrfs 1501MiB 3701MiB mkpart quota 3701MiB 4469MiB mkpart xfs 4469MiB 5489MiB

No output. Ask the kernel to reread the table:

# partprobe /dev/vdb

No output. The partition names are labels on the GPT, not filesystem labels; the filesystems do not exist yet:

# lsblk -o NAME,SIZE,PARTLABEL,FSTYPE /dev/vdb
NAME    SIZE PARTLABEL FSTYPE
vdb       8G           
├─vdb1  1.5G ext4      
├─vdb2  2.1G btrfs     
├─vdb3  768M quota     
└─vdb4 1020M xfs       

The FSTYPE column is empty for all four. If it is not, a previous filesystem signature is still present in the partition and should be removed with wipefs -a before formatting. The XFS partition is deliberately smaller than the space after it; Section 12.3.2 uses the unallocated tail to grow the partition and then the filesystem.

Create the ext4 filesystem with a label and a 1% reserved-block percentage. This overwrites any existing filesystem signature in /dev/vdb1; confirm the device before running it. The -q option suppresses the creation report:

# mkfs.ext4 -q -L handbook-ext4 -m 1 /dev/vdb1

No output. Without -q, mke2fs prints the block size, inode count, journal size, and a progress bar. The label is recorded in the superblock and is usable as LABEL=handbook-ext4 in /etc/fstab; the reserved-block percentage leaves 1% of the blocks available only to privileged processes.

mkfs.ext4 option Description
-L LABEL Set the volume label (at most 16 characters).
-m PERCENT Reserve that percentage of blocks for root; default 5%.
-O FEATURE[,^FEATURE] Enable or disable filesystem features at creation.
-N COUNT Set the number of inodes instead of the size heuristic.
-b SIZE Set the block size; 4096 bytes is the usual choice.
-E OPTION Set extended options such as discard, stride, or stripe_width.
-q Suppress the creation report.
-F Force creation over an existing signature; it does not override a mounted-device refusal.

Inspect the result with dumpe2fs -h, which prints the superblock without walking the whole filesystem. Selected output; the version banner (which goes to standard error), the UUID, timestamps, checksums, and the allocation-geometry fields are omitted:

# dumpe2fs -h /dev/vdb1
Filesystem volume name:   handbook-ext4
Filesystem magic number:  0xEF53
Filesystem features:      has_journal ext_attr resize_inode dir_index orphan_file filetype extent 64bit flex_bg metadata_csum_seed sparse_super large_file huge_file dir_nlink extra_isize metadata_csum
Filesystem state:         clean
Errors behavior:          Continue
Inode count:              96000
Block count:              384000
Reserved block count:     3840
Free blocks:              368551
Free inodes:              95988
Block size:               4096
Journal inode:            8
Checksum type:            crc32c
Total journal size:       32M
Total journal blocks:     8192

The feature list explains the behavior you can rely on: has_journal means metadata transactions are logged; extent and 64bit are the modern allocation format; metadata_csum and metadata_csum_seed checksum metadata; dir_index uses hashed directories; flex_bg groups metadata for locality. There are no data checksums. The Reserved block count: value of 3840 is exactly 1% of 384000 blocks.

Change the label with tune2fs -L. It prints its version and returns:

# tune2fs -L handbook-data /dev/vdb1
tune2fs 1.47.2 (1-Jan-2025)

The label change is not echoed, so verify it through blkid or lsblk:

# lsblk -o NAME,LABEL,FSTYPE /dev/vdb1
NAME LABEL         FSTYPE
vdb1 handbook-data ext4

Now change the reserved percentage to 2% and inspect the count:

# tune2fs -m 2 /dev/vdb1
tune2fs 1.47.2 (1-Jan-2025)
Setting reserved blocks percentage to 2% (7680 blocks)

dumpe2fs -h now reports Reserved block count: 7680. Reserved blocks protect against fragmentation and let root-owned daemons continue to write when an ordinary user has filled the filesystem. On a large data volume that only ordinary users write, 1% or 2% is common; on the root filesystem, the default 5% is a useful safety margin.

12.2.2. Inspecting Features and Tuning with tune2fs

tune2fs changes superblock parameters. It can also set the mount-count and time-based check policy, which determines when the boot-time fsck will force a full check. Set a maximum of 30 mounts and an interval of six months:

# tune2fs -c 30 -i 6m /dev/vdb1
tune2fs 1.47.2 (1-Jan-2025)
Setting maximal mount count to 30
Setting interval between checks to 15552000 seconds

Read the result with tune2fs -l, which prints the same superblock information as dumpe2fs -h. Selected lines:

# tune2fs -l /dev/vdb1
Filesystem volume name:   handbook-data
Filesystem state:         clean
Reserved block count:     7680
Mount count:              0
Maximum mount count:      30
Last checked:             Fri Nov 20 07:30:18 2026
Check interval:           15552000 (6 months)
Next check after:         Wed May 19 07:30:18 2027

The check interval is a maximum, not a schedule: the next mount after the interval or mount count is exceeded marks the filesystem for a full check. A server that reboots rarely may prefer -c 0 -i 0 and an explicit maintenance window; a desktop may keep the defaults. Do not disable checks on a filesystem whose health is unknown.

tune2fs option Description
-l Print the superblock (like dumpe2fs -h).
-L LABEL Set or change the volume label.
-m PERCENT Set the reserved-block percentage.
-r COUNT Set the reserved-block count explicitly.
-c COUNT Set the maximum mount count between checks; 0 disables it.
-i INTERVAL Set the maximum time between checks (d, m, w, or seconds); 0 disables it.
-O FEATURE[,^FEATURE] Add or remove a supported feature.
-Q usrquota,grpquota,prjquota Enable quota inodes in the superblock.
-U UUID Set or randomize the filesystem UUID.
-e with continue, remount-ro, or panic Set the kernel behavior after a filesystem error.

Changing a feature with -O is not a casual operation. Clearing has_journal converts the filesystem to an ext2-style layout with no crash journal; enabling metadata_csum on an existing filesystem rewrites every metadata block, which needs time and free space. Create the feature set you need at mkfs time, and test a feature change on a copy before applying it to production.

Mount the filesystem and check capacity from both sides:

# mkdir -p /mnt/ext4
# mount /dev/vdb1 /mnt/ext4

Both commands are silent. findmnt shows the device, filesystem type, and options:

# findmnt /mnt/ext4
TARGET    SOURCE    FSTYPE OPTIONS
/mnt/ext4 /dev/vdb1 ext4   rw,relatime

df -h reports data-block capacity; df -i reports inode capacity. They answer different questions:

# df -h /mnt/ext4
Filesystem      Size  Used Avail Use% Mounted on
/dev/vdb1       1.5G  396K  1.4G   1% /mnt/ext4
# df -i /mnt/ext4
Filesystem     Inodes IUsed IFree IUse% Mounted on
/dev/vdb1       96000    12 95988    1% /mnt/ext4

This filesystem has 96000 inodes and about 1.4 GiB free. A workload that creates many small files can exhaust the inode count while df -h still shows free space. The inode count is fixed at creation; -N or a larger -i bytes-per-inode value changes it, but only on a new filesystem.

12.2.3. Offline Checks and Recovery with e2fsck

e2fsck checks inode tables, directory connectivity, block and inode bitmaps, reference counts, and the journal. Run it on an unmounted filesystem. The -n option answers "no" to every repair question and opens the filesystem read-only; -f forces a check even if the superblock says the filesystem is clean.

e2fsck option Description
-n Open read-only and answer "no" to every repair question.
-f Force a check even when the filesystem is marked clean.
-y Answer "yes" to every repair question; the repair mode.
-p Preen: automatically fix safe problems; exit nonzero when more is needed.
-b BLOCK Read the backup superblock at BLOCK instead of the primary.

The filesystem is still mounted from the previous section. Create a practice directory and file so that the checks below have something to inspect. As root, create the directory and give it to john:

# mkdir -p /mnt/ext4/projects
# chown john:john /mnt/ext4/projects

Both commands are silent. As john, write a file and confirm it:

$ printf "%s\n" "storage plan" > /mnt/ext4/projects/plan.txt

The redirection writes the file without terminal output. cat confirms the content:

$ cat /mnt/ext4/projects/plan.txt
storage plan

Unmount the filesystem and run a read-only check:

# umount /mnt/ext4

The unmount is silent. Then run the read-only check:

# e2fsck -fn /dev/vdb1
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
handbook-data: 14/96000 files (0.0% non-contiguous), 15451/384000 blocks

The exit status is 0. e2fsck writes its version banner to standard error and the pass lines to standard output. A clean check does not prove that file contents are correct; it proves that the metadata structures are consistent.

To practice recovery, damage the file's inode with debugfs while leaving the directory entry in place. This is a deliberate corruption on a disposable filesystem; the file's data is not recoverable through this procedure. Clear the inode:

# debugfs -w -R "clri /projects/plan.txt" /dev/vdb1
debugfs 1.47.2 (1-Jan-2025)

clri clears the inode, including its block references. The -w option opens the filesystem read-write - without it debugfs opens the filesystem read-only and the request fails - and -R runs the single quoted request and exits. The directory entry remains, pointing at a now-free inode. A read-only check detects the inconsistency and exits 4:

# e2fsck -fn /dev/vdb1
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Entry 'plan.txt' in /projects (13) has deleted/unused inode 14.  Clear? no

Entry 'plan.txt' in /projects (13) has an incorrect filetype (was 1, should be 0).
Fix? no

Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
Block bitmap differences:  -33280
Fix? no

Inode bitmap differences:  -14
Fix? no


handbook-data: ********** WARNING: Filesystem still has errors **********

handbook-data: 14/96000 files (0.0% non-contiguous), 15451/384000 blocks

Repair it with -y, which answers "yes" to every repair question:

# e2fsck -fy /dev/vdb1
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Entry 'plan.txt' in /projects (13) has deleted/unused inode 14.  Clear? yes

Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
Block bitmap differences:  -33280
Fix? yes

Free blocks count wrong for group #1 (32578, counted=32579).
Fix? yes

Free blocks count wrong (368549, counted=368550).
Fix? yes

Inode bitmap differences:  -14
Fix? yes

Free inodes count wrong for group #0 (7986, counted=7987).
Fix? yes

Free inodes count wrong (95986, counted=95987).
Fix? yes


handbook-data: ***** FILE SYSTEM WAS MODIFIED *****
handbook-data: 13/96000 files (0.0% non-contiguous), 15450/384000 blocks

The exit status is 1 when errors were corrected. plan.txt is gone: the inode was already cleared, and e2fsck removed the dangling directory entry rather than reconstructing the lost inode. A second read-only check returns 0 and reports a clean filesystem:

# e2fsck -fn /dev/vdb1
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
handbook-data: 13/96000 files (0.0% non-contiguous), 15450/384000 blocks
e2fsck exit status Meaning
0 No errors found.
1 Errors were corrected.
2 Errors were corrected; reboot is required.
4 Errors remain uncorrected.
8 Operational error, such as a mounted filesystem or a failed open.
16 Usage or configuration error.
32 Check cancelled by the user.

e2fsck refuses a read-write repair on a mounted filesystem and exits 8. A read-only -n check can be started while mounted, but the filesystem is changing underneath it, so the result is unreliable. Unmount the filesystem, or boot from rescue media, before a repair. e2fsck -p is the preen mode used by boot scripts: it fixes only problems it can resolve safely and exits nonzero when a full interactive check is needed.

If the primary superblock is damaged, mke2fs -n lists the backup superblock locations, and e2fsck -b BLOCK uses one. The backup superblock is a recovery aid, not a substitute for a known-good filesystem image or backup; a repair that starts from a stale backup can lose recent metadata. On a production system, make an image or snapshot before trying alternate superblocks.