Check and repair a file system

Look for I/O and file system errors with <code>dmesg</code> and <code>journalctl -k</code> first. Never run fsck on a mounted file system: for the root partition on ext4, schedule a check at the next boot with <code>tune2fs -c 1</code> or the <code>fsck.mode=force</code> kernel option. For xfs roots, check online with <code>xfs_scrub</code> where available, copy data off, and restore or rebuild if the damage is real.

Recovery and troubleshooting guide, updated 2026-12-03

How do you know the file system has a problem?

File system trouble rarely announces itself directly. Watch for these signs:

  • Commands fail with Read-only file system even though you are root.
  • Services crash with input/output errors, or files return garbage.
  • ls shows question marks instead of permissions on some entries.
  • Databases complain about corrupted pages after an unclean stop.

The kernel log tells you what happened. Search it before you change anything:

dmesg -T | grep -iE "ext4|xfs|i/o error|blk_update|remount"
journalctl -k -b -p warning
journalctl -k -b -1 -p err     # previous boot, if the journal is persistent

A typical ext4 message looks like EXT4-fs error (device vda1): ext4_lookup:1855: inode #131074: comm nginx: deleted inode referenced. On xfs you may see XFS (vda4): Metadata corruption detected. Note the device name; you will need it.

What does a read-only remount mean?

Ubuntu and Debian mount the root ext4 file system with errors=remount-ro in /etc/fstab. When the kernel detects an inconsistency, it switches the file system to read-only to stop further damage. You can still read files, so this is the moment to copy important data off, as described in copying data off a broken server.

Confirm the state with findmnt -no OPTIONS /; a leading ro confirms it. Forcing it back with mount -o remount,rw / sometimes works, but writing to a file system the kernel considers damaged can make things worse. Copy data first, then schedule a proper check at boot.

A full disk is a different problem that looks similar from the application side: writes fail with No space left on device, and the mount stays read-write. Check with df -h and df -i (the second shows inode exhaustion, common with millions of small cache files).

Why must you never fsck a mounted file system?

fsck rewrites file system metadata directly on the disk. If the kernel has the same file system mounted, it holds its own view of that metadata in memory and keeps writing. The two disagree, and the result can be far worse corruption than you started with. Modern e2fsck refuses to run on a mounted file system unless you force it, and you should treat that warning as final.

The safe options are: unmount it (possible for data partitions), check it in read-only mode with -n for a report only, or let the boot process check the root file system before it mounts read-write.

How do you check an ext4 data partition?

For a partition that is not root, such as a separate /srv, stop the services using it and unmount:

lsblk -f
systemctl stop nginx postgresql
umount /srv
e2fsck -f /dev/vda2
mount /srv

-f forces a full check even if the file system is marked clean. e2fsck asks before each fix; -p fixes safe problems automatically, and -y answers yes to everything, which is fine on a copy but bold on your only data. An exit code of 0 means no errors, 1 means errors were fixed, and 4 or higher means some remain.

If umount says the target is busy, fuser -vm /srv or lsof +f -- /srv shows which processes keep it open.

How do you force a check of the root file system at the next boot?

On systemd distributions, systemd-fsck runs before root is mounted read-write. You can ask it to check unconditionally in two ways.

Option 1: tune2fs mount count (ext4 only)

findmnt -no SOURCE /          # e.g. /dev/vda1
tune2fs -l /dev/vda1 | grep -iE "mount count|state"
tune2fs -c 1 /dev/vda1
reboot

With a maximum mount count of 1, the next mount triggers a check. Afterwards switch the counter off again with tune2fs -c -1 /dev/vda1, or every boot will run a full check.

Option 2: kernel command line

On Ubuntu and Debian, edit /etc/default/grub, add fsck.mode=force fsck.repair=yes to GRUB_CMDLINE_LINUX_DEFAULT, run update-grub and reboot. On Rocky, Alma, CentOS Stream and Fedora, use grubby --update-kernel=ALL --args="fsck.mode=force fsck.repair=yes". Remove the options after the check (grubby --update-kernel=ALL --remove-args=..., or edit the file again).

The old trick of creating /forcefsck is not honoured by current systemd. Expect the boot to take longer than usual; give it several minutes before you assume it is stuck, and check afterwards with journalctl -b -u systemd-fsck-root.

What is different with xfs?

Rocky Linux, AlmaLinux and CentOS Stream use xfs for root by default. xfs keeps a journal and replays it at mount, so a routine boot check is pointless: fsck.xfs is a stub that does nothing, and the fsck.mode=force option does not run a repair on xfs.

The real repair tool is xfs_repair, and it needs the file system unmounted. For a data partition: umount /data, then xfs_repair -n /dev/vdb1 for a dry run, then xfs_repair /dev/vdb1. If it complains about a dirty log, mount and unmount once to replay it. Only as a last resort use -L, which discards the log and may lose the most recent changes.

For the root file system, there is no way to unmount it from inside the running system, and our servers have no rescue system. You can still run an online, read-only check with xfs_scrub -n / (package xfsprogs, marked experimental on some versions) to see the scope. If damage is real, copy the data off while the system runs, then restore a backup or rebuild.

What causes file system damage on a virtual server?

On a KVM guest with NVMe storage, physical disk faults are handled below your server, so most damage comes from inside: a hard power off during heavy writes, a kernel bug, a full disk while a database was writing, or tools that write to raw devices with the wrong target, such as a dd command aimed at the wrong disk. Using reboot instead of a power cycle whenever the system still reacts, and keeping free space above about 15 percent, avoids most of it.

If you see repeated I/O errors that do not match any of those causes, open a ticket with the exact kernel messages and times, so support can look at the host side.

What should you do after a repair?

A repair makes the file system consistent; it does not bring back lost content. After e2fsck, look in lost+found at the root of the partition for orphaned files named by inode number; file tells you what they are. Then check applications:

  • Databases: run their own checks, for example mysqlcheck --all-databases or a test pg_dump to /dev/null.
  • Packages: debsums -s on Debian and Ubuntu, rpm -Va on the dnf family, to find damaged system files.
  • Backups: confirm the newest backup is from before the damage and that it restores, as shown in backups and restore.

Questions about this guide

Can I run fsck on / while the server runs?

Not for repairs. Use e2fsck -n for a read-only report at most, and schedule a real check at boot with tune2fs -c 1 or fsck.mode=force.

Where do I see the result of a boot time check?

In the journal: journalctl -b -u systemd-fsck-root for root and journalctl -b -u "systemd-fsck@*" for other partitions.

Does a boot check run without a console to watch it?

Yes. It runs on its own with fsck.repair=yes; you simply wait longer for SSH to come back.

Why is my disk read-only after a crash?

The kernel remounted it because it found an inconsistency. Copy important data off first, then schedule a check at the next boot.

Is xfs_repair -L safe?

It is a last resort. It throws away the log, which can lose recent writes. Try mounting once to replay the log before you use it.

Is a full disk the same as corruption?

No. A full disk returns "No space left on device" and recovers as soon as you delete files; check with df -h and df -i.