When a file system starts throwing I/O errors or refusing to mount, the temptation is to run the first repair command you remember and hope for the best. That impulse often makes things worse. We've seen teams accidentally zero out superblocks, misinterpret warning messages, or overwrite recoverable data—all because they skipped a few minutes of careful diagnosis. This guide walks through the essential principles of file system repair, the mistakes that commonly derail recovery, and a methodical workflow that keeps your data intact.
Who Needs This and What Goes Wrong Without It
File system repair isn't just for storage administrators managing petabytes of SAN arrays. Anyone who works with data on a daily basis—developers, DevOps engineers, data scientists, even power users running local databases—will eventually face a corrupted volume. The question isn't if, but when. Without a solid repair approach, the most common outcomes are: silent data corruption that propagates to backups, extended downtime while someone frantically Googles error codes, or permanent data loss from a wrong flag passed to a repair tool.
Consider a typical scenario: a PostgreSQL server on ext4 starts logging "structure needs cleaning" errors. The on-call engineer runs fsck -y without taking a filesystem-level snapshot first. The tool finds several inodes with link counts that don't match and deletes them automatically. The database becomes inconsistent, and the only recovery path is a point-in-time restore—which loses six hours of transactions. That mistake is avoidable. The core problem is that most repair tools default to "fix everything automatically" when the operator hasn't specified a safe mode. The operator assumed the tool would be conservative, but it wasn't.
Another common failure mode involves RAID or LVM layers. A disk fails in a RAID 5 array; the administrator replaces it and starts a rebuild. During rebuild, a second disk develops read errors. The file system, already under stress, starts reporting corruption. The admin runs fsck on the logical volume, not realizing that the underlying RAID array is still degraded. The repair tool tries to read blocks that the RAID layer can't reconstruct, causing it to mark those blocks as bad and remove references to them. The result is data loss that could have been avoided if the array had been rebuilt first or if a proper backup had been verified before touching the file system.
Prerequisites and Context to Settle First
Before you run any repair command, you need to understand a few things about your environment. First, what file system type are you dealing with? ext4, XFS, Btrfs, and ZFS each have their own repair tools, quirks, and safe modes. Running fsck.ext4 on an XFS volume will fail, but running xfs_repair on a mounted XFS filesystem will corrupt it. Know your fstab and your kernel module support.
Second, what is the mount state? Most repair tools require the filesystem to be unmounted or mounted read-only. For root filesystems, that means booting from a live USB or a rescue environment. If you run fsck on a mounted read-write filesystem, you risk overwriting the very structures you're trying to fix. Many modern distributions automatically run fsck at boot after a certain number of mounts or a forced interval, but that doesn't cover external drives, NFS mounts, or virtual disks.
Third, do you have a recent, verified backup? This is the single most important prerequisite. No repair tool can recover data that was never written, and some repairs inevitably sacrifice data to restore structural consistency. If you haven't tested a restore recently, you don't have a backup—you have a hope. For critical systems, take a block-level snapshot (using LVM snapshots, ZFS snapshots, or a tool like dd) before any repair operation. Even if the repair goes wrong, you can roll back to the pre-repair state.
Fourth, understand the difference between journal replay and full consistency check. Many file systems (ext4, XFS, NTFS) maintain a journal that records pending metadata operations. If the system crashes, the journal is replayed on next mount to bring the filesystem to a consistent state. That's fast and usually safe. A full fsck or xfs_repair checks every inode and block allocation, which is slow and more invasive. If the journal replay succeeds, you may not need a full check. But if the filesystem reports corruption even after journal replay, a deeper repair is necessary.
Finally, document the current state. Before touching anything, capture the output of dmesg, fsck -n (dry run), and the filesystem's superblock information (tune2fs -l for ext4, xfs_info for XFS). This gives you a baseline and helps you detect if the repair itself introduces new issues.
Core Workflow: A Methodical Repair Sequence
Here is the step-by-step workflow we recommend for any file system repair, whether it's a laptop drive or a production database volume.
Step 1: Stop Writes and Assess
If the filesystem is still mounted read-write, unmount it immediately if possible. If you can't unmount (e.g., root filesystem), remount read-only. Then capture diagnostic information: check syslog, run fsck -n (or equivalent dry-run) to see what errors are reported without making changes. Note any patterns—are the errors in a specific directory? Are they metadata errors or data corruption? This helps you decide whether to proceed with repair or restore from backup.
Step 2: Choose the Right Tool and Mode
- ext2/3/4: Use
fsck.ext4 -nfor dry-run, thenfsck.ext4 -pfor automatic safe repair (preen mode). Avoid-yunless you understand the risks—it answers "yes" to every prompt, including deleting orphaned inodes. - XFS: Use
xfs_repair -nfor dry-run. For repair, runxfs_repairwithout options on an unmounted filesystem. XFS repair is more conservative than fsck; it won't delete data unless absolutely necessary, but it can be slow on large volumes. - Btrfs: Use
btrfs check --readonlyfor dry-run, thenbtrfs check --repair(unmounted). Note that Btrfs repair is still considered experimental for some corruption types; having a backup is critical. - ZFS: ZFS handles most corruption through redundancy and scrubs. Use
zpool scrubto detect and repair checksum errors. If a device fails, replace it and let the pool resilver. Avoid raw device repair tools on ZFS vdevs.
Step 3: Perform the Repair in Stages
Start with the least invasive repair. For ext4, that means fsck -p (preen) which automatically fixes common issues like incorrect block counts and orphaned inodes without asking. If that succeeds, remount and verify data integrity. If preen fails or reports more serious errors, run an interactive fsck (fsck without -p or -y) and answer each prompt carefully. When fsck asks "Delete inode NNN?", consider what that inode might contain. If it's a file in /lost+found, it's likely safe to delete. If it's a directory with a name you recognize, you may want to examine it first.
Step 4: Verify and Document
After repair, run a dry-run again to confirm no errors remain. Then mount the filesystem read-only and check critical directories. Try to access known files. If the filesystem is a database store, run the database's consistency check (e.g., pg_checksums for PostgreSQL, DBCC CHECKDB for SQL Server). Document the repair actions taken, the original error messages, and any data that was lost or moved to lost+found. This documentation is invaluable if you need to escalate to a vendor or restore from backup.
Tools, Setup, and Environment Realities
Your choice of repair tools depends heavily on your operating system, file system type, and whether you have access to a rescue environment. Here we compare the most common scenarios.
Linux Rescue Environments
For ext4 and XFS, the standard tools are part of e2fsprogs and xfsprogs. In a pinch, you can boot any Linux live CD (Ubuntu, SystemRescue) and install the appropriate packages. For Btrfs, you need btrfs-progs. For ZFS on Linux, you need the ZFS utilities from your distribution or the OpenZFS project. One reality: live CD environments may have older versions of these tools. If you're dealing with a feature like ext4's metadata checksums (enabled by default on newer kernels), an old fsck may not recognize them and may report false errors. Always use a live environment with a kernel and tools that match or are newer than the filesystem's creation.
Windows and NTFS
On Windows, chkdsk is the primary repair tool. It has a dry-run mode (chkdsk /scan on recent Windows 10/11) and a repair mode (chkdsk /f). Important: chkdsk on a live system will schedule a check on next reboot. For offline repair, boot from Windows Recovery Environment or a WinPE disk. NTFS also has a journal ($LogFile) that is replayed automatically. If chkdsk reports corruption that it can't fix, you may need third-party tools, but always verify with a second opinion before paying for recovery.
Network and Cloud Storage
Modern workflows often involve NFS, CIFS, or cloud object stores. File system repair at the local level doesn't apply directly to these, but the underlying local filesystem on the server or the hypervisor does. If you're using a NAS, the repair is done on the NAS OS—usually a customized Linux with its own toolset. For cloud volumes (EBS, persistent disks), you detach the volume from the instance, attach it to a rescue instance, and run repair there. One common mistake: running fsck on a mounted cloud volume that is still attached to the original instance, causing metadata corruption from concurrent writes.
Hardware Considerations
Repair tools work at the logical level. If you have bad sectors, the underlying block device will report read errors, and fsck will mark those blocks as bad. But a failing disk can cause the repair to hang or crash. Before repairing, check S.M.A.R.T. status (smartctl -a /dev/sda). If the disk has pending or reallocated sectors, replace it first, then repair the filesystem on the new disk (or restore from backup). Running fsck on a dying disk can push it over the edge.
Variations for Different Constraints
Not every repair scenario fits the same workflow. Here are common variations and how to adapt.
Root Filesystem Corruption
If your root filesystem (/) won't boot, you need a rescue environment. Boot from a live USB or the distribution's rescue mode. Mount the root filesystem read-only (mount -o ro /dev/sda1 /mnt), run fsck, then remount read-write if repairs succeed. If the corruption is severe, you may need to chroot into the mounted system to reinstall the bootloader or fix fstab. One pitfall: forgetting to mount /proc, /sys, and /dev before chrooting—many repair tools rely on them.
Large Filesystems (Multiple TB)
Full fsck on a 10 TB ext4 volume can take hours or days. For such cases, consider alternative approaches. XFS repair is generally faster than ext4 fsck because it uses a different metadata structure. Btrfs has btrfs check --repair which is also relatively fast. If downtime is unacceptable, you might opt to restore from a recent backup rather than repair in place. Another option: use a filesystem that supports online repair (ZFS scrub, Btrfs scrub) to detect and fix corruption without unmounting, though these don't fix all types of corruption.
Databases and Application-Specific Filesystems
Databases often use raw devices or files with direct I/O. If the underlying filesystem becomes corrupted, the database may have inconsistent pages. After filesystem repair, always run the database's own integrity check. For MySQL/InnoDB, that's mysqlcheck or innochecksum. For PostgreSQL, pg_checksums and pg_verify_checksums. For MongoDB, mongod --repair (though that should be a last resort). Never assume the filesystem repair is sufficient—the database may have its own corruption that fsck can't see.
Virtual Machine Disk Images
If a VM's virtual disk (qcow2, vmdk) is corrupted, you have two layers to consider. First, try to repair the image format itself (e.g., qemu-img check -r all for qcow2). Then, attach the image to a rescue VM and run filesystem repair inside the guest. A common mistake: running fsck on the host's raw device that contains the image file, rather than on the guest filesystem inside the image. That will corrupt the image format.
Pitfalls, Debugging, and What to Check When It Fails
Even with careful planning, repairs can fail or introduce new problems. Here are the most common pitfalls and how to diagnose them.
Pitfall 1: Using -y Blindly
The -y flag to fsck answers "yes" to every question. That includes deleting inodes that could be recovered, moving files to lost+found unnecessarily, and even removing the journal. Always start with -n to see what would be changed, then use -p (preen) for safe automatic fixes, and only use interactive mode for the rest. If you must use -y (e.g., in a script), take a snapshot first.
Pitfall 2: Forgetting About the Journal
If a filesystem was not unmounted cleanly, the journal is replayed automatically on mount. If you run fsck before that replay, you may see spurious errors that disappear after a mount. Always try mounting first (read-only) to trigger journal replay. If the mount succeeds without errors, the filesystem is likely consistent. If it fails, then run fsck.
Pitfall 3: Ignoring Underlying Hardware Errors
If fsck reports a large number of errors in diverse locations, suspect hardware. Check dmesg for I/O errors, S.M.A.R.T. attributes, and RAID controller logs. A single bad disk in a RAID array can cause widespread filesystem corruption that looks like a software problem. Replace the failing hardware before attempting repair, or at least image the drive with ddrescue first.
Pitfall 4: Not Verifying After Repair
A successful fsck exit code (0) doesn't guarantee data integrity. It only means the metadata is consistent according to the tool's checks. Data corruption in file contents may go undetected. Always verify critical files by checksum (if you have known good hashes) or by application-level validation. For databases, run the DBCC or equivalent. For archives, try to extract them.
Pitfall 5: Running Repair on a Live Filesystem
Some tools (like fsck.ext4 -n) are safe on a read-only mount, but fsck -y on a read-write mount is a disaster waiting to happen. The filesystem can change while fsck is reading it, leading to false positives and incorrect fixes. Always unmount or remount read-only. For root filesystems, use a rescue environment.
Debugging When Repair Fails
If fsck or xfs_repair crashes or hangs, check for hardware issues first. If the tool reports "superblock is corrupt" and can't find an alternative superblock, use mke2fs -n to list backup superblock locations (for ext4) and try fsck -b to use a specific backup. For XFS, you can try xfs_repair -o force_geometry if the superblock is damaged but the geometry is known. If all else fails, consider professional data recovery—but only if the data is worth more than the cost of recovery.
Common Mistakes FAQ in Prose
We've compiled the most frequent questions and misconceptions we encounter. Think of this as a troubleshooting companion to the workflow above.
Should I run chkdsk /f on my Windows system drive?
Only if you have a recent backup and you've scheduled it during a maintenance window. Chkdsk on a boot drive requires a reboot and can take hours on large drives. If you just have a few bad sectors, chkdsk /scan (Windows 10/11) is safer—it checks for errors without fixing them, and you can decide later if repair is needed.
Can I repair a ZFS pool with fsck?
No. ZFS manages its own data integrity and has no standard fsck utility. Use zpool scrub to detect and repair checksum errors. If a device fails, replace it and let the pool resilver. Never run fsck on a ZFS vdev directly—it will destroy the pool.
What does lost+found mean?
When fsck finds inodes that are not referenced by any directory, it moves them to the lost+found directory. The files are renamed to their inode number. You can often recover data from these files by examining them with file or strings. If the filesystem was heavily corrupted, lost+found may contain many files; you'll need to manually sort through them. This is why you should always take a snapshot before repair—you can mount the snapshot and compare.
Is it safe to cancel a running fsck?
No. If you interrupt fsck in the middle of writing changes, you can leave the filesystem in an inconsistent state that may be worse than the original corruption. If you must cancel, try to let it finish the current pass (fsck prints a message when starting a new pass). If you can't wait, reboot and hope the journal replay recovers. Better to plan for a maintenance window long enough for the repair.
How often should I run fsck proactively?
For ext4, the default is every 30 mounts or 180 days, whichever comes first. For XFS, there is no automatic fsck; use xfs_repair -n periodically (e.g., quarterly) on critical volumes. For ZFS, scrubs should be scheduled monthly. For Btrfs, scrubs are also recommended monthly. Proactive checks catch issues before they cause downtime. But remember that a full fsck on a large volume is disruptive; schedule it during low-usage periods.
What to Do Next: Specific Actions
You've repaired the filesystem (or decided not to). Now what? Here are five concrete next steps to harden your environment and prevent future corruption.
- Set up monitoring for filesystem health. For Linux, use
smartdto monitor disk health ande2scrub(for ext4) orbtrfs scrubto periodically check filesystem integrity. For Windows, enable the Storage Health Monitoring feature in Server Manager. Configure alerts for any filesystem errors or S.M.A.R.T. warnings. - Implement backup verification. Don't just back up—test a restore at least quarterly. For databases, restore to a staging environment and run consistency checks. For file servers, restore a random set of files and verify their checksums. Document the procedure so it's not forgotten.
- Review your filesystem choices. If you're running ext4 on a large volume with critical data, consider migrating to XFS (better performance for large files) or ZFS (built-in checksumming and self-healing). For new deployments, choose a filesystem that matches your workload: XFS for large file storage, ZFS for data integrity, Btrfs for snapshots and compression.
- Create a runbook for repair. Document the exact steps for your environment: how to boot into rescue mode, which commands to run, where to find backup superblocks, and who to contact if the repair fails. Include the output of
tune2fs -lorxfs_infofor each volume. Test the runbook during a maintenance window. - Schedule regular integrity checks. For ext4, use
e2scrub(part of e2fsprogs 1.45+) to check metadata checksums without unmounting. For XFS, schedulexfs_repair -nquarterly. For ZFS, runzpool scrubmonthly. For Btrfs, runbtrfs scrubmonthly. Automate these with cron or systemd timers, and send alerts on failure.
File system repair is a skill that improves with methodical practice. By understanding the tools, respecting the prerequisites, and avoiding the common mistakes outlined here, you can recover from corruption with minimal data loss and downtime. The next time you see that dreaded "structure needs cleaning" message, you'll know exactly what to do—and what not to do.
Comments (0)
Please sign in to post a comment.
Don't have an account? Create one
No comments yet. Be the first to comment!