我有一个 ext4 文件系统,最近出现了一些坏扇区。运行fsck.ext4 -c
会发现并记住坏块。如何找到哪些文件(如果有)包含这些坏块,以便我可以从备份中恢复它们?
答案1
dumpe2fs
与的组合debugfs
,它们包含在e2fsprogs
软件包中fsck.ext*
。
您必须使用命令的输出作为下一个命令的参数。
这些工具会自动检测文件系统块大小,因此比直接badblocks
调用一致且更安全。
打印文件系统已注册的坏块:
# dumpe2fs -b DEVNAME
打印使用给定块列表的索引节点:
# debugfs -R "icheck BLOCK ..." DEVNAME
打印给定 inode 列表的路径名:
# debugfs -R "ncheck INODE ..." DEVNAME
debugfs
还有一个交互式 shell 和-f cmd_file
选项,但对于这种情况,它们的功能并不强大或有用。
-R 选项允许更多自动化脚本,如下所示:
#!/bin/sh
# Finds files affected by bad blocks on ext* filesystems.
# Valid only for ext* filesystems with bad blocks registered with
# fsck -c [-c] [-k] or -l|-L options.
# Can be extremely slow on damaged storage (not just a corrupt filesystem).
DEVNAME="$1"
[ -b "$DEVNAME" ] || exit 1
BADBLOCKS="$(dumpe2fs -b "$DEVNAME" | tr '\n' ' ')"
[ -n "$BADBLOCKS" ] || exit 0
INODES="$(debugfs -R "icheck $BADBLOCKS" "$DEVNAME" | awk -F'\t' '
NR > 1 { bad_inodes[$2]++; }
END {
for (inode in bad_inodes) {
if (inode == "<block not found>") {
printf("%d unallocated bad blocks\n", bad_inodes[inode]) > "/dev/stderr";
continue;
}
printf inode OFS;
}
}
')"
[ -n "$INODES" ] || exit 0
debugfs -R "ncheck -c $INODES" "$DEVNAME"