Skip to content

Checking and Repair

This chapter is the sick-filesystem chapter: how to find out what is wrong, what the repair tools can and cannot do, and how to get your files out when the answer is "start over". Uniquely in this guide, most of it the lab ran for real — against image copies it damaged on purpose (transcripts t04 and t05), because offline checking is where the evidence could be earned honestly. The rule for the whole chapter, learned by every filesystem community and printed here first: work on a copy, or do not work at all.

First, look: check, read-only

sudo btrfs check --readonly /dev/sdX

btrfs check walks the trees and reports inconsistencies. --readonly is the default and the only sane first move: it changes nothing. Add --check-data-csum and it also reads the file data and verifies every checksum — slower, and the version that catches rot: in transcript t04 the lab flipped four bytes inside a file's data on a copy, and the plain check stayed silent while the checksum-verifying run named the exact byte range, the stored sum, and the sum it expected. On a mounted filesystem, the running tally of the same events is:

sudo btrfs device stats /mnt/vault

Any corruption_error above zero is the filesystem raising its hand; the scrub in chapter 9 is what then goes looking.

The superblock is gone

If a device will not open at all — No valid Btrfs found — the primary superblock may be damaged. Backup superblocks live further into each device, and the tools can open those instead. Transcript t05 is this whole story, run: the lab zeroed a copy's superblock magic, watched check refuse the image, then opened the backup copy —

btrfs check --readonly --super 1 dead.img

— and got a clean report. One honest footnote from that transcript: the dedicated btrfs rescue super-recover, which writes a good backup back over the bad primary, refused the lab's image under btrfs-progs 6.6.3. On a mounted system, the equivalent rescue is the mount option usebackuproot. Tools have moods; backups of the superblock are forever.

--repair, and why it is the last resort

sudo btrfs check --repair /dev/sdX

The tool itself prints a ten-second countdown and a warning before it starts — transcript t08 records it verbatim — and the warning means what it says. That transcript also shows repair's shape honestly: run on the lab's data-damaged copy, it found the trees sound, fixed nothing, and left the rotten file data exactly as rotten as it found it, because repair rebuilds structure and cannot invent a file's lost bytes. On tree damage it makes choices a human would want to supervise; on some damage it guesses, and a wrong guess is worse than the damage. The order of operations on a sick filesystem is: image it byte-for-byte if you possibly can (ddrescue exists for dying disks), run read-only checks, salvage files (below), and only then, on the copy or with nothing left to lose, consider --repair.

Salvage: btrfs restore

The chapter's hero is the tool from chapter 4:

mkdir salvage
sudo btrfs restore -i /dev/sdX salvage/

restore walks whatever trees survive and writes the files out to ordinary storage, ignoring (-i) the errors it meets. In the lab it recovered every file from the image with the destroyed superblock — TREES IDENTICAL, transcript t05 — and from the image with the rotten data block, where cmp proved the salvaged file differed from the original at exactly the byte that had been flipped (transcript t04). Salvage first, mourn later: a filesystem you can read files from is a bad week, not a lost archive — and what makes the difference, again, is whether a copy of the data exists somewhere the damage did not reach.