e2fsck
Check and repair ext2/3/4 filesystems
TLDR
Check filesystem, reporting any damaged blocks
Check filesystem and automatically repair ([p]reen) any damaged blocks
Check filesystem in read only mode
Force checking even if the filesystem seems clean
Perform an exhaustive, non-destructive read-write test for bad blocks and blacklist them
SYNOPSIS
e2fsck [options] device
PARAMETERS
-p
Automatically repair the filesystem without prompting. This option is generally safe for minor issues.
-a
Same as -p; provided for backward compatibility. This option is deprecated.
-y
Assume 'yes' to all questions, automatically fixing issues. Use with caution as it can make potentially destructive changes.
-n
Assume 'no' to all questions, preventing any modifications. This makes e2fsck run in read-only mode, useful for inspection.
-f
Force checking the filesystem, even if it appears clean (i.e., has a valid 'last check' timestamp).
-v
Produce verbose output, showing more details about the checks being performed.
-c
Check for bad blocks on the device and add them to the bad block inode. This can take a long time.
-b superblock
Use an alternative superblock. This is useful if the primary superblock is corrupted. Common backup superblocks are 8193, 16385, etc.
-D
Optimize directories by reordering directory entries and compacting them. This can improve directory lookup performance.
-E extended_options
Set extended options, such as discard (for SSD TRIM operations) or ea_inode_size.
DESCRIPTION
e2fsck is a utility used to check and optionally repair ext2, ext3, and ext4 filesystems in Linux. It performs a series of comprehensive checks on the filesystem's metadata, including superblocks, inodes, directories, and block bitmaps, to detect inconsistencies. If any errors are found, e2fsck attempts to correct them automatically or interactively, depending on the options provided.
It is crucial to run e2fsck on an unmounted filesystem to prevent data corruption and loss. Running it on a mounted, especially writeable, filesystem can lead to severe damage. The command is often invoked automatically at system boot time if the system detects an unclean shutdown or after a certain number of mounts/time intervals, as configured by tune2fs. While it can resolve many common filesystem issues, heavily corrupted filesystems might require more advanced recovery methods, and data loss can still occur.
CAVEATS
Critical Warning: Always ensure the filesystem is unmounted before running e2fsck. Running it on a mounted filesystem, especially one with write access, can lead to severe data corruption and irreversible data loss. While e2fsck can fix many issues, it is not a data recovery tool for heavily damaged filesystems, and some data loss might still occur. It requires root privileges to execute.
EXIT STATUS CODES
e2fsck returns various exit codes that are crucial for scripting and automated system maintenance:
0 - No errors.
1 - Filesystem errors corrected.
2 - Filesystem errors corrected, and system should be rebooted.
4 - Filesystem errors left uncorrected.
8 - Operational error.
16 - Usage or syntax error.
32 - e2fsck canceled by user request.
128 - Shared library error.
SAFETY PRECAUTION
Before attempting repairs with options like -y on critical filesystems, especially those with unbacked-up data, consider backing up the data first if possible. Alternatively, use a live CD/USB to boot and run e2fsck in a controlled environment, perhaps initially with -n to assess the damage without making changes.
HISTORY
e2fsck is a fundamental component of the e2fsprogs package, which provides a suite of utilities for managing ext2, ext3, and ext4 filesystems. It was primarily developed by Theodore Ts'o, who also spearheaded much of the development of the ext filesystem family.
Its design was heavily influenced by fsck_ffs, the filesystem checker for the Fast File System (FFS) used in BSD operating systems. Over time, e2fsck has evolved significantly to support the added complexities and features of ext3 (journaling) and ext4 (extents, larger filesystems, nanosecond timestamps), maintaining its role as the primary tool for filesystem integrity checks in Linux environments.