GrepBoard/Manuals
605 manuals ·

Check an ext4 image with a damaged superblock using e2fsck

·Tested on Ubuntu 24.04.5 LTS · e2fsprogs 1.47.0 · ext4 image file (300M)·Test scope ↓·8 steps

Practice on an image file: read e2fsck -fn output and exit codes, then find a backup superblock and use -b to check and repair when the primary one is damaged.

Bad magic number in super-block is what you see when an ext4 volume will not mount. Reproduce it on an image file first, so you know how to read the output and what order to repair in. No mount and no loop device: one file only.

[1] Create the practice image. Backup superblocks exist only with two or more block groups (128 MiB per group at 4 KiB blocks), so make it 300M. -d copies a directory into the filesystem.

console
$ mkdir src && for i in 1 2 3; do head -c 1M /dev/urandom > src/f$i.bin; done; echo hello > src/note.txt$ truncate -s 300M disk.img$ mke2fs -F -q -t ext4 -L data -d src disk.img$ cp disk.img clean.img

[2] Record the healthy baseline. -f forces a check even if the filesystem looks clean; -n opens it read-only and answers no to every question, so nothing is changed.

console
$ e2fsck -fn clean.imge2fsck 1.47.0 (5-Feb-2023)Pass 1: Checking inodes, blocks, and sizesPass 2: Checking directory structurePass 3: Checking directory connectivityPass 4: Checking reference countsPass 5: Checking group summary informationdata: 15/76800 files (0.0% non-contiguous), 9755/76800 blocks$ echo $?0

The exit code is a sum of conditions (man e2fsck, EXIT CODE). This page meets four of them: 0 (no errors), 1 (errors corrected), 4 (errors left uncorrected), 8 (operational error).

[3] Wipe the primary superblock: the 1 KiB that starts 1024 bytes into the device. Never do this to a real device.

console
$ dd if=/dev/zero of=disk.img bs=1024 seek=1 count=1 conv=notrunc status=none$ dumpe2fs -h disk.imgdumpe2fs 1.47.0 (5-Feb-2023)dumpe2fs: Bad magic number in super-block while trying to open disk.imgCouldn't find valid filesystem superblock.

[4] Run e2fsck -fn as is. When the primary superblock is unreadable, e2fsck tries backups on its own.

console
$ e2fsck -fn disk.imge2fsck 1.47.0 (5-Feb-2023)ext2fs_open2: Bad magic number in super-blocke2fsck: Superblock invalid, trying backup blocks...Pass 1: Checking inodes, blocks, and sizesPass 2: Checking directory structurePass 3: Checking directory connectivityPass 4: Checking reference countsPass 5: Checking group summary informationPadding at end of inode bitmap is not set. Fix? no Inode bitmap differences: Group 1 inode bitmap does not match checksum.IGNORED.Group 2 inode bitmap does not match checksum.IGNORED. data: ********** WARNING: Filesystem still has errors ********** data: 15/76800 files (0.0% non-contiguous), 9755/76800 blocks$ echo $?4

The file and block counts match the healthy run. -n changed nothing; the exit code is 4.

[5] Find the backup location yourself. It depends on block size and filesystem size, so when the primary is gone, compute it with mke2fs -n, which writes nothing and only prints the layout it would create. Use the block size the filesystem was made with.

console
$ mke2fs -n -F -t ext4 disk.imgmke2fs 1.47.0 (5-Feb-2023)Creating filesystem with 76800 4k blocks and 76800 inodesFilesystem UUID: …Superblock backups stored on blocks: 	32768

[6] Check read-only with that block via -b.

console
$ e2fsck -fn -b 32768 disk.img

The result is the same as step 4 (minus the Superblock invalid line), exit code 4. A wrong number fails to open and exits with 8.

console
$ e2fsck -fn -b 8193 disk.imge2fsck: Attempt to read block from filesystem resulted in short read while trying to open disk.imgCould this be a zero-length partition?$ echo $?8

[7] Repair. For a real device, copy the whole device first. -y answers yes to every question.

console
$ e2fsck -fy disk.imge2fsck 1.47.0 (5-Feb-2023)ext2fs_open2: Bad magic number in super-blocke2fsck: Superblock invalid, trying backup blocks...Pass 1: Checking inodes, blocks, and sizesPass 2: Checking directory structurePass 3: Checking directory connectivityPass 4: Checking reference countsPass 5: Checking group summary informationPadding at end of inode bitmap is not set. Fix? yes  data: ***** FILE SYSTEM WAS MODIFIED *****data: 15/76800 files (0.0% non-contiguous), 9755/76800 blocks$ echo $?1

Per the man page, when a backup superblock is used and the filesystem is not read-only, e2fsck updates the primary superblock when it finishes.

[8] Check again for exit code 0, then confirm the files survived.

console
$ e2fsck -fn disk.img >/dev/null; echo $?0$ dumpe2fs -h disk.img 2>&1 | grep stateFilesystem state:         clean$ cmp <(debugfs -R "cat /f1.bin" disk.img 2>/dev/null) <(debugfs -R "cat /f1.bin" clean.img 2>/dev/null) && echo samesame

Only 1 KiB of superblock was damaged here, so this ended cleanly. If the problem is a full disk, see Find what is filling a full disk.

#e2fsck#ext4#superblock#mke2fs