What fsck Does
fsck walks through a file system's metadata: the inode tables, directory entries, block bitmaps, and journal. It reports what does not add up and can fix it, for example by freeing blocks that nothing uses or by moving orphaned files to lost+found. fsck itself is a front end: for ext4 it runs e2fsck.
The Safe Order
- Identify the device with
lsblk -f. Device names can change between boots. - Back up what you can still read, or take a snapshot. A repair changes the disk and has no undo.
- Unmount the file system. Never run a repair on a mounted file system: the kernel and fsck would both write to it, and the damage can grow.
- Run a read-only check first, then repair.
Check a Data Disk
lsblk -f
sudo umount /mnt/data
sudo e2fsck -f -n /dev/vdb1
-f forces a full check even when the file system is marked clean, and -n opens it read-only and answers "no" to every question. Read the report. Then repair:
sudo e2fsck -f /dev/vdb1
Without further options, e2fsck asks before each fix, so you see what it changes. -p fixes only problems that are safe to fix without asking. Do not start with -y, which answers yes to everything: use it only after you have seen the problems and have a backup.
Check the Root File System
The root file system cannot be unmounted on a running server. Two ways remain:
- Rescue mode: boot the rescue system from your provider's control panel. The server's disk is then a plain unmounted device, and you run the commands above on it, such as
/dev/vda1. - At boot: on systemd systems, the kernel parameter
fsck.mode=forcemakes the boot check every file system that has a non-zero pass number in/etc/fstab. Add it once at the GRUB menu from the server console: presse, append it to the line that starts withlinux, and boot with Ctrl+x. If the menu is hidden, press Esc or hold Shift early in the boot. By default the boot check repairs only what is safe to fix without asking, and a long check on a large disk delays the boot.
Exit Codes
| Code | Meaning |
|---|---|
0 | No errors |
1 | Errors corrected |
2 | Corrected; reboot needed |
4 | Errors left uncorrected |
8 | Operational error |
16 | Usage or syntax error |
32 | Canceled by the user |
128 | Shared-library error |
The codes add up: 3 means 1 and 2. Read it with echo $? right after the command.
XFS: Use xfs_repair
On XFS, fsck.xfs does nothing in a normal boot and exits successfully; XFS replays its journal when it is mounted, and XFS lines in fstab usually have pass 0. The real tool, xfs_repair, also needs an unmounted file system:
sudo umount /srv
sudo xfs_repair -n /dev/vdc1
sudo xfs_repair /dev/vdc1
-n only reports. If xfs_repair says the log holds changes, mount and unmount the file system once so the log is replayed, then run it again. The -L option throws the log away: changes in progress at the crash are lost, and files can be lost with them. The xfs_repair manual calls it a last resort; use it only after a backup.
After the Repair
- Look in
lost+found: recovered files there have numbers instead of names. - Read the kernel log: repeated errors point to the disk, not the file system. See the dmesg command.
- Check the hardware on a dedicated server with smartctl.
- Restore from backup when the repair leaves files damaged; see snapshot vs backup.