The fsck (stands for File System Consistency Check) is used to check and repair one or more Linux filesystems.

This check will run automatically at boot time when a filesystem inconsistencies detected. Also, can be run manually as needed.

You can use the fsck command to repair corrupted file systems when the system fails to boot, or a partition can’t be mounted, or if it’s become read-only.

In this article, we’ll see how to use the ‘fsck’ or ‘e2fsck’ command in Linux to repair a corrupted file system.

Note:

  • Execute the fsck on an unmounted file systems to avoid any data corruption in the file system.
  • For larger file systems, fsck may take a long time to run depending on system speed and disk sizes.

When the file system check is complete, fsck returns one of the following exit code:

Exit Code Description
0 No errors
1 Filesystem errors corrected
2 System should be rebooted
4 Filesystem errors left uncorrected
8 Operational error
16 Usage or syntax error
32 Checking canceled by user request
128 Shared-library error

Common Syntax:

fsck [option] [device or partition or mount point]

Corrupting EXT4 File System

We are going to intentionally corrupt the EXT4 file system by executing the below command. It trash’s randomly selected file system metadata blocks.

Make a Note: Please don’t test this on Production server, as this may damage your data badly.
sudo umount /data

Corrupting the ext4 file system.

 
sudo dd if=/dev/zero of=/dev/sdb1 bs=10000 skip=0 count=1

1+0 records in
1+0 records out
10000 bytes (10 kB, 9.8 KiB) copied, 0.00394663 s, 2.5 MB/s

When you try to load the file system, you will see the following error message because it was corrupted.

sudo mount /data

mount: /data: wrong fs type, bad option, bad superblock on /dev/sdb1, missing codepage or helper program, or other error.

Repair Corrupted EXT4 & EXT3 File System

You can repair a non-root corrupted ext3 or ext4 file system on a running Linux system. fsck works as a wrapper for the fsck.ext3 and fsck.ext4 commands.

Make a note: If you are not able to unmount some of the Non-root volume due to an issue, boot the system into single user mode or rescue mode to repair it.

Step-1: Unmount the device that you want to run fsck.

sudo umount /dev/sdb1

Step-2: Run fsck to repair the file system:

sudo fsck.ext4 -p /dev/sdb1
  • -p : Automatically repair any issues that can be safely fixed without user intervention.

If the above option doesn’t resolve the issue, run the fsck command in the below format.

sudo fsck.ext4 -fvy /dev/sdb1

e2fsck 1.45.6 (20-Mar-2020)
ext2fs_open2: Bad magic number in super-block
fsck.ext4: Superblock invalid, trying backup blocks...
Resize inode not valid. Recreate? yes 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
Block bitmap differences: -65536 -65538 -(65541--65542) -(65546--65547) -(65549--65550) -(65555--65557)
.
.
Fix? yes Free inodes count wrong for group #0 (8181, counted=8165).
Fix? yes Free inodes count wrong (327669, counted=327653).
Fix? yes Padding at end of inode bitmap is not set. Fix? yes /dev/sdb1: ***** FILE SYSTEM WAS MODIFIED ***** 27 inodes used (0.01%, out of 327680)
0 non-contiguous files (0.0%)
0 non-contiguous directories (0.0%)
# of inodes with ind/dind/tind blocks: 0/0/0
Extent depth histogram: 19
43294 blocks used (3.30%, out of 1310464)
0 bad blocks
0 large files 16 regular files
2 directories
0 character device files
0 block device files
0 fifos
0 links
0 symbolic links (0 fast symbolic links)
0 sockets
------------
18 files

Step-3: Once the file system is repaired, mount the partition.

sudo mount /dev/sdb1

2) Repairing LVM Volume with fsck

fsck can be run on LVM logical volumes just like filesystems on standard partitions. Follow the below procedure for repairing a LVM partition:

You can also restore/recover the lvm volume instead of repairing it as needed.

Step-1: Make sure the specific LVM volume is in active state to run fsck. To check the status of LVM, run:

sudo lvscan

  inactive          '/dev/myvg/vol01' [1.00 GiB] inherit
ACTIVE '/dev/rhel/swap' [2.07 GiB] inherit
ACTIVE '/dev/rhel/root' [<26.93 GiB] inherit

If it’s 'inactive', activate it by running the following command.

sudo lvchange -ay /dev/myvg/vol01 -v

  Activating logical volume myvg/vol01.
activation/volume_list configuration setting not defined: Checking only host tags for myvg/vol01.
Creating myvg-vol01
Loading table for myvg-vol01 (253:2).
Resuming myvg-vol01 (253:2).

Step-2: Unmount the device or filesystem that you want to run fsck.

sudo umount /dev/myvg/vol01

Step-3: Run fsck to repair the file system. You must enter the path of the LVM volume to run fsck and not an actual physical partition.

sudo fsck.ext4 -fvy /dev/myvg/vol01

e2fsck 1.45.6 (20-Mar-2020)
/dev/myvg/vol01: clean, 24/65536 files, 14094/262144 blocks
  • -f : Force checking even if the file system seems clean.
  • -y : Assume an answer of `yes’ to all questions; allows e2fsck to be used non-interactively.
  • -v : Verbose mode

Step-4: Once the file system is repaired, mount the partition.

sudo mount /apps

Conclusion

In this tutorial, we’ve shown you how to repair a corrupted EXT4 filesystems on Linux. You can use the same procedure for EXT3 and other filesystems.

Also, shown you how to run e2fsck on the LVM volumes.

If you have any questions or feedback, feel free to comment below.

转载自:

https://www.2daygeek.com/fsck-repair-corrupted-ext4-file-system-linux/

最新文章

  1. yii笔一----基础,安装,结构,增删改查基本操作
  2. ubuntu14.04上Virtualbox安装win7(使用Ghost镜像安装,启用USB设备支持,设置共享目录)
  3. 用CSS3和Canvas来画网格
  4. jquery mobile界面数据刷新
  5. cassandra中对节点失败与否的探测方法, the Phi accrual Failure Dector,附论文
  6. java判定字符串中仅有数字和- 正则表达式匹配 *** 最爱那水货
  7. Java for LeetCode 140 Word Break II
  8. c语言下的通用数据库接口(之sqlite消化,模拟c#,java的反射)
  9. CSS之拖拽库2
  10. Windows7添加SSD硬盘后重启卡住正在启动
  11. 武汉科技大学ACM:1006: 我是老大
  12. hdu 1009 贪心基础题
  13. WebRTC VoiceEngine使用简单Demo
  14. css2.1实现圆角边框
  15. 简聊iOS支付集成(支付宝和微信支付)
  16. 使用google的pprof工具以及在gin中集成pprof
  17. [转]How to Create an Add-in for Microsoft Outlook
  18. 短信api接口
  19. Qt Creator中使用qss对界面美化没有作用(效果)的问题
  20. mysql+linux 忘记密码

热门文章

  1. Locust fasthttplocust客户端环境搭建
  2. 一篇教会你写90%的shell脚本
  3. Vue-cli JSX踩坑问题!
  4. 3、k8s 核心实战
  5. NXOpen获取UFUN的tag
  6. 1. ansible学习总结: 基础模块
  7. Maven简答题
  8. solve--ifconifg命令提示没有该命令解决方法
  9. Jmeter学习:时间类函数
  10. [部署日记]GO在Visual Studio Code初次运行时提示go: go.mod file not found in current directory or any parent directory; see &#39;go help modules&#39;