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/

How to Check and Repair EXT4 Filesystem in Linux的更多相关文章

  1. linux操作系统故障处理-ext4文件系统超级块损坏修复

    linux操作系统故障处理-ext4文件系统超级块损坏修复   背景 前天外面出差大数据测试环境平台有7台服务器挂了,同事重启好了五台服务器,但是还有两台服务器启动不起来,第二天回来后我和同事再次去机 ...

  2. Linux filesystem detection

    16 down vote accepted The reason you can't find it is because, for the most part, it's not in the ke ...

  3. 恢复ext4文件系统superblock

    恢复ext4文件系统superblock 1. Create ext4 文件系统. [root@localhost ~]# mkfs.ext4 /dev/vdb1 [root@localhost ~] ...

  4. [ext4]010 磁盘布局 - 如何查找inode的磁盘位置

    在linux系统中,任何一个文件,都有一个inode与其对应,也就是说,在一个文件系统中,一个文件都有唯一的ino来标示他,那么在ext4系统中,ino是如何确定的哪? 当我们新创建的文件或目录时,会 ...

  5. dumpe2fs 命令的使用,转储 ext2/ext3/ext4 文件系统信息

    使用man 命令可以查看 dumpe2fs 命令具体的使用的方法: NAME dumpe2fs - dump ext2/ext3/ext4 filesystem information SYNOPSI ...

  6. linux 文件系统 xfs、ext4、ext3 的区别

    前言 centos7.0开始默认文件系统是xfs,centos6是ext4,centos5是ext3 ext3介绍 ext3和ext4的最大区别在于,ext3在fsck时需要耗费大量时间(文件越多,时 ...

  7. 一个版本烧录过程中记录:fdisk、mkfs.ext4、make_ext4fs、img2simg、simg2img

    关键词:dd.fdisk.mkfs.ext4.make_ext4fs.img2simg.simg2img等等. 一个典型的嵌入式系统是由uboot+kernel+rootfs组成的,其中uboot和k ...

  8. XFS VS EXT4

    ext4是第四代扩展文件系统(英语:Fourth EXtended filesystem,缩写为ext4)是linux系统下的日志文件系统,是ext3文件系统的后继版本ext4的文件系统容量达到1EB ...

  9. fdisk、mkfs.ext4、make_ext4fs、img2simg、simg2img

    一个典型的嵌入式系统是由uboot+kernel+rootfs组成的,其中uboot和kernel都是二进制,rootfs存在文件系统. 二进制在烧录的时候比较简单,将二进制数据写入存储设备固定地址: ...

  10. Linux 文件恢复(XFS & EXT4)

    在Linux中,删除rm命令使用需谨慎,有时候可能由于误操作,导致重要文件删除了,这时不要太紧张,操作得当的话,还是可以恢复的. EXT 类型文件恢复 删除一个文件,实际上并不清除inode节点和bl ...

随机推荐

  1. 课程表及事件提醒app-界面原型设计

    前端设计: 暂定为8个主要界面:程序初始界面.主界面(首页).课表界面."我的"界面.登录界面.注册界面.创建事件界面.新建课表界面 设计思路: 项目结构 三个主要界面 首页: 课 ...

  2. Java-获取真实用户ip

    1 import common.util.StringUtil; 2 import org.springframework.web.bind.annotation.ModelAttribute; 3 ...

  3. 小程序微信支付完整demo,包含退款

    最近刚完成一个商场小程序(http://market.zhenzikj.com/detail/121.html), 使用到了微信支付功能,其中遇到了很多的抗,所以,我把支付这块摘出来,以免大家少走弯路 ...

  4. 关于视频加密ts格式转MP4的方法

    copy /b "%~dp0"\*.ts "%~dp0"\new.ts 最近在手机浏览器上下载了钢铁侠等漫威电影,觉得不错想保存到手机上,却都又是m3u8格式, ...

  5. 环保行业ERP主要的几大治理区域?

    环保行业是指在国民经济结构中,以防治环境污染.改善生态环境.保护自然资源为目的而进行的技术产品开发.商业流通.资源利用.信息服务.工程承包等活动的总称. 哲讯环保ERP行业产业链的上游主要是钢铁.有色 ...

  6. stl仿函数集合

  7. JS 代码片段 / 预编译/预解析 /执行上下文/ECG/EC/ECS/GO/VO/AO

    代码段概念 一个 script 就是一个代码段 在一个页面中可以有多个代码段 每一个代码段, 彼此独立的, 如果上面的代码段报错了, 不会影响下一个代码段 referenceError 引用错误 下面 ...

  8. 【java数据结构与算法】直接戳中"希尔排序"要点,学不会你打我~

    希尔排序: 也叫增量递减排序 是插入排序的优化,分组+排序 Get到重点,那么我们开剖析一下 结合图表和代码,总结如下~public class ShellSort { public static v ...

  9. 【记录】 iSCSI服务器的搭建与使用[Debian]

    序言 更换系统后需要一个网络文件存储用于备份文件,本想用NFS多方便,但是timeshift不支持网络存储,备份路径必须是一个块存储设备, 但是你还必须分好文件系统,这不是多此一举???反正我只用rs ...

  10. Java除法运算精度问题

    结论:不要将两个int类型的变量直接相除,会出现精度丢失问题,最好将其中一个变量强制转化成浮点型 现象: 两个int类型相除的结果要取整,如果相除得到0.53,那么结果会变成0,出现精度丢失的问题 D ...