查找哪些文件受ext4文件系统上的坏块影响


13

我有一个ext4文件系统,最近开发了一些坏扇区。跑步fsck.ext4 -c发现并记住坏块。我如何找到哪些文件(如果有)包含这些坏块,以便可以从备份中还原它们?

linux  ext4 

当我使用fsck进行交互式修复时,它显示了哪些文件有问题;但是,如果太多,@ Nethox回答的更好。
Aquarius Power

Answers:


11

用的组合dumpe2fsdebugfs被包括在e2fsprogs包沿着fsck.ext*
您必须将命令的输出用作下一个命令的参数。
这些工具会自动检测文件系统的块大小,因此它比直接badblocks调用更为一致和安全。

打印文件系统的已注册坏块:

# dumpe2fs -b DEVNAME

打印使用给定阻止列表的inode:

# debugfs -R "icheck BLOCK ..." DEVNAME

将路径名打印到给定的inode列表:

# debugfs -R "ncheck INODE ..." DEVNAME

debugfs也有一个交互式外壳程序和-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"

只是要指出,在非常大的文件系统上,debugfs可能需要很长时间才能显示某些内容
Aquarius Power
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.