brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: e2fsck -n checks an ext4 filesystem without changing anything

Linux
~/tips/tip-disk-e2fsck-n-read-only-check

sudo e2fsck -fn /dev/sdb1

Checks an ext4 filesystem for errors and only reports them, without repairing anything.

A filesystem check that repairs things straight away is great, until it “fixes” something you’d rather have looked at first. For ext2, ext3 and ext4 there’s a dry run: e2fsck opens the disk read-only and answers no to every repair question.

sudo umount /dev/sdb1
sudo e2fsck -fn /dev/sdb1
echo $?

-n opens the filesystem read-only and assumes no to all questions, so e2fsck tells you what it would fix without touching a single block. -f forces a full check, even when the filesystem is marked clean, because otherwise e2fsck may just say it’s clean and stop. Replace /dev/sdb1 with your own partition; lsblk -f shows which one has the ext4 filesystem.

The exit code is worth reading. 0 means no errors, 4 means errors were found and left uncorrected, which is what you’ll get with -n when something is wrong. 8 is an operational error, often a wrong device name. When the dry run looks sane, you can run the real repair with plain e2fsck -f and answer the questions yourself, preferably with a backup of anything important first.

The caveat is mounting. The manual says that with -n it’s safe to run on a mounted filesystem, but the results aren’t valid then, because the kernel keeps changing things underneath. So unmount first, or boot from a live USB stick for the root filesystem. And this tool is for the ext family only. The Unraid array uses XFS by default, and for XFS the read-only check is xfs_repair -n; for btrfs it’s a different tool again.

Further reading: e2fsck(8) manual page, xfs_repair(8) manual page

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link