检查和修复文件系统的 Linux Fsck 命令示例
【摘要】 本文介绍了 10 个有关如何执行 fsck 命令来排除和修复任何文件系统错误的实际示例
Linux fsck 实用程序用于检查和修复 Linux 文件系统(ext2、ext3、ext4等)。
根据上次检查文件系统的时间,系统在引导期间运行 fsck 以检查文件系统是否处于一致状态。当文件系统出现问题时,系统管理员也可以手动运行它。
确保在卸载的文件系统上执行 fsck 以避免任何数据损坏问题。
本文介绍了 10 个有关如何执行 fsck 命令来排除和修复任何文件系统错误的实际示例。
1. 磁盘分区上的文件系统检查
首先,使用parted 命令查看系统上所有可用的分区,如下所示。
# parted /dev/sda 'print'
Number Start End Size Type File system Flags
1 1049kB 106MB 105MB primary fat16 diag
2 106MB 15.8GB 15.7GB primary ntfs boot
3 15.8GB 266GB 251GB primary ntfs
4 266GB 500GB 234GB extended
5 266GB 466GB 200GB logical ext4
6 467GB 486GB 18.3GB logical ext2
7 487GB 499GB 12.0GB logical fat32 lba
您可以检查特定的文件系统(例如:/dev/sda6),如下所示。
# fsck /dev/sda6
fsck from util-linux 2.20.1
e2fsck 1.42 (29-Nov-2011)
/dev/sda6: clean, 95/2240224 files, 3793506/4476416 blocks
以下是 fsck 命令的可能退出代码。
- 0 – 没有错误
- 1 – 文件系统错误更正
- 2 – 系统应该重新启动
- 4 – 文件系统错误未得到纠正
- 8 – 操作错误
- 16 – 用法或语法错误
- 32 – Fsck 被用户请求取消
- 128 – 共享库错误
2. 特定于文件系统类型的 Fsck 命令
fsck 在内部使用相应的文件系统检查器命令进行文件系统检查操作。这些 fsck 检查器命令通常位于 /sbin 下。
以下示例显示了各种可能的 fsck 检查器命令(例如:fsck.ext2、fsck.ext3、fsck.ext4 等)
# cd /sbin
# ls fsck*
fsck fsck.cramfs fsck.ext2 fsck.ext3 fsck.ext4 fsck.ext4dev fsck.minix fsck.msdos fsck.nfs fsck.vfat
当 fsck 命令找不到正在检查的文件系统的文件系统检查器时,它会给您一个错误。
例如,如果您在 ntfs 分区上执行 fsck,您将收到以下错误消息。/sbin 下没有 fsck.ntfs。因此,这会给出以下错误消息。
# fsck /dev/sda2
fsck from util-linux 2.20.1
fsck: fsck.ntfs: not found
fsck: error 2 while executing fsck.ntfs for /dev/sda2
3. 使用选项 -A 在一次运行中检查所有文件系统
您可以使用此选项在一次 fsck 运行中检查所有文件系统。这将按照 /etc/fstab 中每个文件系统提到的 fs_passno 给出的顺序检查文件系统。
请注意,fs_passno 值为 0 的文件系统会被跳过,大于 0 的会按顺序检查。
/etc/fstab 包含如下所列的条目,
# cat /etc/fstab
##
proc /proc proc nodev,noexec,nosuid 0 0
## / was on /dev/sda5 during installation
/dev/sda5 / ext4 errors=remount-ro 0 1
## /mydata was on /dev/sda6 during installation
/dev/sda6 /mydata ext2 defaults 0 2
## /backup was on /dev/sda7 during installation
/dev/sda7 /backup vfat defaults 0 3
在这里,具有相同 fs_passno 的文件系统在您的系统中被并行检查。
# fsck -A
建议您在此全局检查期间通过添加 -R 选项排除根文件系统,如下所示。
# fsck -AR -y
fsck from util-linux 2.20.1
e2fsck 1.42 (29-Nov-2011)
/dev/sda6: clean, 95/2240224 files, 3793506/4476416 blocks
dosfsck 3.0.12, 29 Oct 2011, FAT32, LFN
/dev/sda7: 8 files, 50/1463400 clusters
注意:选项 -y 在以下示例之一中进行了说明。
4. 使用选项 -t 仅检查特定的文件系统类型
使用 fsck -t 选项,您可以指定要检查的文件系统列表。当您使用选项 -A 时,fsck 将仅检查使用此选项 -t 提到的文件系统。注意 fslist 是一个逗号分隔的值。
现在,将 ext2 作为 fslist 值传递给 -t 选项,如下所示:
# fsck -AR -t ext2 -y
fsck from util-linux 2.20.1
e2fsck 1.42 (29-Nov-2011)
/dev/sda6: clean, 11/2240224 files, 70327/4476416 blocks
在此示例中,/dev/sda6 是使用文件系统 ext2 创建的唯一分区,因此会对其进行相应检查。
在文件系统前面使用关键字“no”,您可以检查除特定文件系统之外的所有其他文件系统类型。
在以下示例中,将 ext2 文件系统排除在检查之外。
# fsck -AR -t noext2 -y
fsck from util-linux 2.20.1
dosfsck 3.0.12, 29 Oct 2011, FAT32, LFN
/dev/sda7: 0 files, 1/1463400 clusters
5. 不要使用选项 -M 对挂载的文件系统执行 Fsck
最好将此选项用作所有 fsck 操作的默认选项。这可以防止您在挂载的文件系统上意外运行 fsck。
# mount | grep "/dev/sd*"
/dev/sda5 on / type ext4 (rw,errors=remount-ro)
/dev/sda6 on /mydata type ext2 (rw)
/dev/sda7 on /backup type vfat (rw)
如上图,挂载了/dev/sda7。如果您尝试在这个 /dev/sda7 挂载的文件系统上执行 fsck(连同 -M 选项),fsck 将简单地退出并退出代码 0,如下所示。
# fsck -M /dev/sda7
# echo $?
0
6.使用选项-T跳过显示标题
使用选项 -T,您可以跳过 fsck 命令输出开头显示的标题。
# fsck -TAR
e2fsck 1.42 (29-Nov-2011)
/dev/sda6 is mounted. e2fsck: Cannot continue, aborting.
dosfsck 3.0.12, 29 Oct 2011, FAT32, LFN
/dev/sda7: 8 files, 50/1463400 clusters
请注意,标题类似于“来自 util-linux 2.20.1 的 fsck”。
7. 使用选项 -f 强制文件系统检查,即使它是干净的
默认情况下,fsck 会尝试跳过干净的文件系统以更快地完成工作。
# fsck /dev/sda6
fsck from util-linux 2.20.1
e2fsck 1.42 (29-Nov-2011)
/dev/sda6: clean, 95/2240224 files, 3793503/4476416 blocks
您可以使用 -f 强制它检查文件系统,如下所示。
# fsck /dev/sda6 -f
fsck from util-linux 2.20.1
e2fsck 1.42 (29-Nov-2011)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sda6: 95/2240224 files (7.4% non-contiguous), 3793503/4476416 blocks
8. 尝试使用选项 -y 自动修复检测到的问题
在以下示例中,/dev/sda6 分区已损坏,如下所示。
# mount /dev/sda6 /mydata
# cd /mydata
# ls -li
ls: cannot access test: Input/output error
total 72
49061 -rw-r--r-- 1 root root 8 Aug 21 21:50 1
49058 -rw-r--r-- 1 root root 36864 Aug 21 21:24 file_with_holes
49057 -rw-r--r-- 1 root root 8192 Aug 21 21:23 fwh
11 drwxr-xr-x 2 root root 49152 Aug 19 00:29 lost+found
2060353 ?rwSr-S-wT 16 root root 4096 Aug 21 21:11 Movies
? -????????? ? ? ? ? ? test
如上所示,目录Movies和文件测试属性无效。
在以下示例中,-y 将向所有问题传递“是”以自动修复检测到的损坏。
# fsck -y /dev/sda6
fsck from util-linux 2.20.1
e2fsck 1.42 (29-Nov-2011)
/dev/sda6 contains a file system with errors, check forced.
Pass 1: Checking inodes, blocks, and sizes
Inode 2060353 is a unknown file type with mode 0137642 but it looks
like it is really a directory.
Fix? yes
Pass 2: Checking directory structure
Entry 'test' in / (2) has deleted/unused inode 49059. Clear? yes
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sda6: ***** FILE SYSTEM WAS MODIFIED *****
/dev/sda6: 96/2240224 files (7.3% non-contiguous), 3793508/4476416 blocks
9. 避免修复,但使用选项 -n 向标准输出报告问题
可以在不使用 fsck -n 选项修复文件系统的情况下将此类检测到的问题打印到标准输出中。
首先,您可以注意到/看到分区 /dev/sda6 中的问题,即 Movies 目录(和 fwh 文件)没有有效的属性详细信息。
# mount /dev/sda6 /mydata
# cd /mydata
# ls -lrt
total 64
drwxr-xr-x 2 root root 49152 Aug 19 00:29 lost+found
?--xrwx-wx 16 root root 4096 Aug 21 21:11 Movies
?-----x-wx 1 root root 8192 Aug 21 21:23 fwh
-rw-r--r-- 1 root root 36864 Aug 21 21:24 file_with_holes
-rw-r--r-- 1 root root 8 Aug 21 21:50 1
在 stdout 中显示的特定分区中的上述问题没有对其进行任何修复,如下所示,
以下 fsck 示例在 stdout 中显示问题,但未修复它。(部分输出如下所示)。
# fsck -n /dev/sda6
fsck from util-linux 2.20.1
e2fsck 1.42 (29-Nov-2011)
/dev/sda6 contains a file system with errors, check forced.
Pass 1: Checking inodes, blocks, and sizes
Inode 2060353 is a unknown file type with mode 0173 but it looks
like it is really a directory.
Fix? no
Inode 2060353, i_blocks is 8, should be 0. Fix? no
Pass 2: Checking directory structure
Inode 2060353 (/Movies) has invalid mode (0173).
Clear? no
Inode 49057 (/fwh) has invalid mode (013).
Clear? no
Entry 'fwh' in / (2) has an incorrect filetype (was 1, should be 0).
Fix? no
Pass 3: Checking directory connectivity
Unconnected directory inode 65409 (???)
Connect to /lost+found? no
'..' in ... (65409) is ??? (2060353), should be (0).
Fix? no
Unconnected directory inode 2076736 (???)
Connect to /lost+found? no
Pass 4: Checking reference counts
Inode 2 ref count is 4, should be 3. Fix? no
Inode 65409 ref count is 3, should be 2. Fix? no
Inode 2060353 ref count is 16, should be 15. Fix? no
Unattached inode 2060354
Connect to /lost+found? no
Pass 5: Checking group summary information
Block bitmap differences: -(164356--164357) -4149248
Fix? no
Directories count wrong for group #126 (1, counted=0).
Fix? no
/dev/sda6: ********** WARNING: Filesystem still has errors **********
/dev/sda6: 96/2240224 files (7.3% non-contiguous), 3793508/4476416 blocks
10.使用选项-a自动修复损坏的部分
为了自动修复损坏的部分(无需任何用户交互),请使用选项 -a,如下所示。
# fsck -a -AR
选项 -a 与 e2fsck 实用程序中的 -p 相同。它会导致 e2fsck 修复任何检测到的问题,这些问题必须在没有用户交互的情况下安全修复。
如果 fsck 需要管理员注意,它会在打印问题描述之前简单地以错误代码 4 退出。
# fsck -a /dev/sda6
fsck from util-linux 2.20.1
/dev/sda6 contains a file system with errors, check forced.
/dev/sda6: Inode 2060353 is a unknown file type with mode 0173 but it looks
like it is really a directory.
/dev/sda6: UNEXPECTED INCONSISTENCY; RUN fsck MANUALLY.
(i.e., without -a or -p options)
# echo $?
4
您还记得,这里可以使用 fsck -y 选项自动修复上述问题。
# fsck -y /dev/sda6
fsck from util-linux 2.20.1
e2fsck 1.42 (29-Nov-2011)
/dev/sda6 contains a file system with errors, check forced.
Pass 1: Checking inodes, blocks, and sizes
Inode 2060353 is a unknown file type with mode 0173 but it looks
like it is really a directory.
Fix? yes
Pass 2: Checking directory structure
Inode 49057 (/fwh) has invalid mode (013).
Clear? yes
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
Block bitmap differences: -(164356--164357)
Fix? yes
Free blocks count wrong for group #5 (0, counted=2).
Fix? yes
Free blocks count wrong (682908, counted=682910).
Fix? yes
/dev/sda6: ***** FILE SYSTEM WAS MODIFIED *****
/dev/sda6: 95/2240224 files (7.4% non-contiguous), 3793506/4476416 blocks
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
作者其他文章
评论(0)