How to repair a corrupted file system in Linux

How to Repair a Corrupted File System in Linux File system corruption is one of the most serious issues that Linux administrators and users can encounter. When a file system becomes corrupted, it can lead to data loss, system instability, and boot failures. Understanding how to diagnose and repair corrupted file systems is an essential skill for anyone working with Linux systems. This comprehensive guide will walk you through the process of identifying, diagnosing, and repairing corrupted file systems in Linux using various built-in tools and techniques. Whether you're dealing with ext4, ext3, XFS, or other file system types, this article provides practical solutions for common corruption scenarios. Understanding File System Corruption File system corruption occurs when the metadata or data structures that organize files and directories become damaged or inconsistent. This can happen due to various reasons: - Hardware failures: Bad sectors on hard drives, failing storage devices - Power outages: Sudden system shutdowns during write operations - Software bugs: Kernel panics or application crashes - Improper shutdowns: Force-powering off the system - Memory issues: RAM problems causing data corruption - Cable problems: Loose or damaged SATA/IDE cables Signs of File System Corruption Recognizing the symptoms of file system corruption early can help prevent further damage: - System fails to boot properly - Files or directories become inaccessible - Strange characters in file names - "Input/output error" messages - System crashes or freezes randomly - Slow file operations - Error messages during startup Prerequisites and Safety Measures Before attempting any file system repair, it's crucial to understand the risks and take appropriate precautions. Creating Backups Always create a backup before attempting repairs. File system repair operations can potentially cause data loss if something goes wrong. ```bash Create a disk image backup sudo dd if=/dev/sda1 of=/backup/sda1_backup.img bs=4M status=progress Or use rsync for file-level backup rsync -avH /source/directory/ /backup/directory/ ``` Working with Unmounted File Systems Most file system repair tools require the file system to be unmounted. You cannot repair a mounted file system safely. ```bash Check if file system is mounted mount | grep /dev/sda1 Unmount the file system sudo umount /dev/sda1 For stubborn mounts, use force unmount sudo umount -f /dev/sda1 ``` Essential File System Repair Tools Linux provides several powerful tools for diagnosing and repairing file system corruption. fsck (File System Check) The `fsck` command is the primary tool for checking and repairing file systems. It's a wrapper that calls the appropriate file system-specific checker. ```bash Basic syntax sudo fsck [options] device Check file system without making changes sudo fsck -n /dev/sda1 Automatically fix errors without prompting sudo fsck -y /dev/sda1 Force check even if file system appears clean sudo fsck -f /dev/sda1 ``` e2fsck (for ext2/ext3/ext4 file systems) `e2fsck` is specifically designed for ext family file systems and offers more detailed options. ```bash Interactive repair mode sudo e2fsck /dev/sda1 Automatic repair mode sudo e2fsck -y /dev/sda1 Check bad blocks while repairing sudo e2fsck -c /dev/sda1 Verbose output sudo e2fsck -v /dev/sda1 ``` xfs_repair (for XFS file systems) XFS file systems use their own specialized repair tool. ```bash Check XFS file system without making changes sudo xfs_repair -n /dev/sda1 Repair XFS file system sudo xfs_repair /dev/sda1 Force repair even with dirty log sudo xfs_repair -L /dev/sda1 ``` Step-by-Step Repair Process Step 1: Boot from Live Media If your system won't boot due to file system corruption, you'll need to boot from a live Linux distribution. 1. Create a bootable USB drive with a Linux distribution 2. Boot from the USB drive 3. Access the terminal 4. Install necessary tools if not already available: ```bash Ubuntu/Debian sudo apt update && sudo apt install e2fsprogs xfsprogs CentOS/RHEL/Fedora sudo yum install e2fsprogs xfsprogs or sudo dnf install e2fsprogs xfsprogs ``` Step 2: Identify the Corrupted File System Use various commands to identify which file system is corrupted: ```bash List all block devices lsblk Show partition information sudo fdisk -l Display file system information sudo blkid Check mount status mount ``` Step 3: Unmount the File System Ensure the corrupted file system is not mounted: ```bash Unmount the file system sudo umount /dev/sda1 If processes are using the file system, find and kill them sudo fuser -km /mount/point sudo umount /dev/sda1 ``` Step 4: Run File System Check Start with a read-only check to assess the damage: ```bash Read-only check for ext file systems sudo e2fsck -n /dev/sda1 Read-only check for XFS file systems sudo xfs_repair -n /dev/sda1 Generic file system check sudo fsck -n /dev/sda1 ``` Step 5: Perform the Repair Based on the assessment, proceed with the actual repair: ```bash For ext2/ext3/ext4 file systems sudo e2fsck -f -y /dev/sda1 For XFS file systems sudo xfs_repair /dev/sda1 Generic repair command sudo fsck -y /dev/sda1 ``` Step 6: Verify the Repair After completing the repair, verify that the file system is healthy: ```bash Check the file system again sudo fsck -n /dev/sda1 Mount the file system sudo mount /dev/sda1 /mnt/test Check available space and inodes df -h /mnt/test df -i /mnt/test List files to verify accessibility ls -la /mnt/test ``` Advanced Repair Techniques Dealing with Bad Blocks Bad blocks can cause persistent file system corruption. Use these techniques to handle them: ```bash Check for bad blocks during repair sudo e2fsck -c /dev/sda1 Use badblocks to scan for bad sectors sudo badblocks -v /dev/sda1 Write bad block list to file system sudo badblocks -v /dev/sda1 > badblocks.txt sudo e2fsck -l badblocks.txt /dev/sda1 ``` Recovering the Superblock The superblock contains critical file system metadata. If it's corrupted, you can restore from backup copies: ```bash List backup superblock locations sudo dumpe2fs /dev/sda1 | grep superblock Use backup superblock for repair sudo e2fsck -b 32768 /dev/sda1 Alternative backup superblock locations sudo e2fsck -b 98304 /dev/sda1 ``` Journal Recovery for ext3/ext4 For journaled file systems, you might need to handle journal corruption: ```bash Clear the journal (use with caution) sudo tune2fs -f -j /dev/sda1 Force journal replay sudo e2fsck -fy /dev/sda1 ``` Specific File System Repair Scenarios Repairing ext4 File Systems ```bash Comprehensive ext4 repair sequence sudo umount /dev/sda1 sudo e2fsck -f -v -C 0 /dev/sda1 sudo resize2fs -f /dev/sda1 sudo e2fsck -f -v -C 0 /dev/sda1 ``` Repairing XFS File Systems ```bash XFS repair with log clearing if necessary sudo umount /dev/sda1 sudo xfs_repair -n /dev/sda1 # Check first sudo xfs_repair /dev/sda1 # Repair if needed If log is dirty and preventing repair: sudo xfs_repair -L /dev/sda1 # Clear log and repair ``` Repairing NTFS File Systems For NTFS file systems on Linux: ```bash Install ntfs-3g tools sudo apt install ntfs-3g Check NTFS file system sudo ntfsfix -n /dev/sda1 Repair NTFS file system sudo ntfsfix /dev/sda1 ``` Troubleshooting Common Issues Error: "Device is busy" This error occurs when the file system is still mounted or in use: ```bash Find processes using the file system sudo fuser -v /mount/point sudo lsof +f -- /mount/point Kill processes and unmount sudo fuser -km /mount/point sudo umount /mount/point ``` Error: "Superblock is corrupt" When the primary superblock is damaged: ```bash Find backup superblocks sudo mke2fs -n /dev/sda1 Use backup superblock sudo e2fsck -b 32768 /dev/sda1 ``` Error: "Journal has invalid transaction" For journal-related issues: ```bash Clear journal and replay sudo tune2fs -f -O ^has_journal /dev/sda1 sudo e2fsck -f /dev/sda1 sudo tune2fs -j /dev/sda1 ``` Recovery Mode Boot Issues If the system won't boot to recovery mode: 1. Use a live Linux distribution 2. Mount the root file system: ```bash sudo mkdir /mnt/system sudo mount /dev/sda1 /mnt/system ``` 3. Check for critical system files 4. Repair the file system while unmounted Prevention and Best Practices Regular File System Checks Schedule regular file system checks to catch corruption early: ```bash Set maximum mount count before forced check sudo tune2fs -c 30 /dev/sda1 Set time-based check interval sudo tune2fs -i 30d /dev/sda1 View current settings sudo tune2fs -l /dev/sda1 | grep -i "check" ``` System Monitoring Monitor system health to prevent corruption: ```bash Check system logs for errors sudo journalctl -p err -n 50 Monitor disk health sudo smartctl -a /dev/sda Check for hardware errors sudo dmesg | grep -i error ``` Proper Shutdown Procedures Always shut down systems properly to prevent corruption: ```bash Proper shutdown command sudo shutdown -h now Restart command sudo reboot Emergency sync and unmount sync sudo umount -a ``` When to Seek Professional Help Some situations require professional data recovery services: - Physical hard drive damage - Multiple failed repair attempts - Critical data that cannot be lost - Hardware-level corruption - RAID array failures Conclusion File system corruption in Linux can be a serious issue, but with the right tools and knowledge, most corruption problems can be resolved successfully. The key is to act quickly, always create backups before attempting repairs, and follow systematic troubleshooting procedures. Remember that prevention is always better than cure. Regular system maintenance, proper shutdown procedures, monitoring disk health, and maintaining current backups will help you avoid most file system corruption issues. When corruption does occur, the tools and techniques outlined in this guide will help you restore your system to working condition while minimizing data loss. By understanding the various repair tools like `fsck`, `e2fsck`, and `xfs_repair`, along with their appropriate use cases, you'll be well-equipped to handle file system corruption issues confidently. Always remember to work on unmounted file systems, start with read-only checks, and have backups available before attempting any repairs.