Introduction
Filesystem corruption is one of the scariest moments for any self-hoster. A power outage during a write, a failing SSD with silent bit flips, or a kernel panic mid-transaction can leave your filesystem in an inconsistent state. When that happens, the only thing standing between you and data loss is the filesystem check and repair tool specific to your filesystem.
Linux supports three major local filesystems — ext4 (default on most distributions), XFS (default on RHEL/Fedora), and Btrfs (gaining adoption with Fedora and openSUSE). Each has its own dedicated repair tool with different capabilities, safety guarantees, and recovery strategies. This article compares fsck.ext4 (e2fsck), xfs_repair, and btrfs check with practical recovery scenarios.
| Feature | fsck.ext4 (e2fsck) | xfs_repair | btrfs check |
|---|---|---|---|
| File System | ext2/3/4 | XFS | Btrfs |
| Checksum Verification | Metadata only (ext4) | Metadata only | Metadata + Data (with csum) |
| Online Check | No (unmount required) | Yes (xfs_scrub) | Yes (btrfs scrub) |
| Journal Replay | Automatic | Automatic (mount) | N/A (CoW) |
| Repair Mode | Interactive + Auto (-p) | Auto (-L for log) | Manual (–repair) |
| Inode Repair | Yes | No (must wipe) | Yes (reflink based) |
| Snapshot Support | N/A | N/A | Yes (repair from snapshot) |
| Dry Run | Yes (-n) | Yes (-n) | Yes (default, –readonly) |
| Recovery Time | ~1 min/TB | ~30 sec/TB | ~3 min/TB (metadata only) |
| Risk of Data Loss | Low (journaled) | Medium (log zeroing) | Medium (experimental repair) |
fsck.ext4: The Time-Tested Standard
fsck.ext4 (linked from e2fsck) is the most mature filesystem checker in the Linux ecosystem, with over 25 years of development. It understands ext2/3/4 on-disk structures intimately and can recover from a wide range of corruption scenarios.
| |
Key options explained:
| |
The five-pass structure covers inode integrity, directory structure, path connectivity, reference counts, and block group accounting. A clean run means the filesystem is internally consistent — but it does NOT detect silent data corruption (bit rot) unless you use ext4 with the metadata_csum feature enabled:
| |
For automated maintenance, add periodic fsck checks to your boot process:
| |
When fsck finds an inode with corrupted block pointers, it can attempt recovery by moving the orphaned blocks to the lost+found directory:
| |
xfs_repair: Enterprise Recovery Speed
XFS was designed for massive parallel I/O and metadata-intensive workloads. Its journaling is more aggressive than ext4’s — XFS journals metadata changes only, relying on write-ahead logging for consistency. xfs_repair reflects this design: it is fast (often completing in seconds on multi-TB filesystems) but less forgiving than e2fsck. If the journal is corrupt, xfs_repair will zero it, potentially losing recent writes.
| |
The xfs_scrub tool complements xfs_repair by providing online checks:
| |
WARNING: The -L flag on xfs_repair is destructive. It zeroes the journal log, meaning any writes that were in-flight when the system crashed are permanently lost. Only use -L when xfs_repair refuses to proceed without it and you have confirmed backups.
btrfs check: CoW Filesystem Recovery
Btrfs uses copy-on-write (CoW) and checksumming at the block level, which changes how corruption detection and repair work. Instead of a single linear process like ext4’s five passes, btrfs check verifies the metadata trees (extent tree, chunk tree, root tree) and their cross-references.
| |
The --repair flag is explicitly labeled experimental in the man page. Unlike e2fsck and xfs_repair, which have decades of battle testing, btrfs repair is still maturing. The recommended recovery path for btrfs is:
- Run
btrfs check --readonlyto diagnose the issue - If possible, recover data by mounting read-only and copying it elsewhere
- Use
btrfs check --repaironly as a last resort - If you have btrfs snapshots, recover individual files from them
For ongoing integrity monitoring, btrfs includes a built-in scrub feature that runs on a mounted filesystem:
| |
Scrub reads every data and metadata block, verifies checksums, and automatically repairs correctable errors if you have a redundant profile (RAID1, RAID10, DUP metadata):
| |
Why Self-Host Filesystem Maintenance
Cloud block storage handles filesystem repair transparently — until it does not. When AWS EBS or Google Persistent Disk has an internal error, you have no visibility into what the repair tool did, what data was lost, or whether orphaned inodes ended up in lost+found. Self-hosting gives you full control: you decide whether to run a dry-run check, interactively repair specific inodes, or zero the journal and accept data loss for in-flight writes.
Regular filesystem checks also catch hardware degradation before it becomes data loss. A failing SATA cable manifests as checksum errors in btrfs scrub or unexpected read errors in e2fsck. Catching these during a scheduled maintenance window lets you replace the hardware while your data is intact. Without proactive checking, the first sign of failure is often a mount that fails with “structure needs cleaning” — at which point your service is already down.
For more on Linux storage management, see our guide on disk health monitoring with smartd and NVMe tools. For filesystem snapshot strategies, check our Btrfs snapshot management guide. If you are dealing with data that may already be lost, our data recovery tools guide covers forensic-level recovery.
FAQ
How often should I run filesystem checks?
For ext4, the default tune2fs settings run a check every 30 mounts or 180 days. For XFS, schedule xfs_scrub -n weekly via cron. For btrfs, run btrfs scrub monthly. Production servers should automate checks during low-I/O windows — a 2 AM cron job is standard. Filesystems with critical data (databases, financial records) benefit from weekly scrubs. Archive storage with infrequent writes can run quarterly.
Can I run fsck on a mounted filesystem?
Generally no, and attempting to do so is dangerous. ext4 and XFS require unmounted filesystems for fsck and xfs_repair respectively. btrfs check can run on a mounted filesystem in read-only mode with --force. The online alternatives are xfs_scrub (XFS) and btrfs scrub (btrfs). For ext4, there is no online equivalent — you must unmount to check. Plan for downtime during ext4 fsck, which can take 10+ minutes on large filesystems.
Will fsck recover deleted files?
Only in limited cases. If an inode was unlinked but the blocks were not yet overwritten, e2fsck places the recovered data in the lost+found directory. The file will have its inode number as the filename (e.g., #12345). xfs_repair does not recover deleted files — once unlinked in XFS, the space is freed immediately. btrfs does not have a lost+found concept — use btrfs restore to recover files from a mountable snapshot instead. For intentional file recovery, use forensic tools like testdisk and photorec.
What should I do if fsck repeatedly finds errors?
If e2fsck finds new errors on every run, you likely have failing hardware. Check SMART data with smartctl -a /dev/sdb and look for Reallocated_Sector_Ct, Pending_Sector, or UDMA_CRC_Error_Count values. A non-zero pending sector count strongly suggests drive failure. Replace the drive, clone the data with ddrescue, and retire the old device. Running fsck repeatedly on a failing drive accelerates data loss — the repair writes stress already-failing sectors.
How do I automate filesystem integrity monitoring?
Set up a cron job or systemd timer for each filesystem type:
| |
Combine with monitoring: if xfs_scrub or btrfs scrub exits with a non-zero status, the cron job should trigger an alert via your monitoring stack (Prometheus Alertmanager, Gotify, Ntfy). The check tools return exit codes: 0 for clean, 1 for corrected errors, 4 for uncorrected errors.
💡 Want to test your market judgment? I use Polymarket for prediction market trading — it is the world’s largest prediction market platform, where you can wager on everything from election outcomes to AI regulation timelines. Unlike gambling, this is a genuine information market: the more you know, the higher your win rate. I have made solid returns predicting AI-related events. Sign up with my invite link: Polymarket.com