Ext4文件系统修复

Ext4文件系统修复

目录

一、 super block.................................................................................................................................................

二、查看分区设备信息.....................................................................................................................................

三、查看备份块................................................................................................................................................

四、尝试修复超级块.........................................................................................................................................

1. 已知文件系统格式...................................................................................................................................

2. 不确定文件系统.......................................................................................................................................

一、super block

硬盘分区开头、开头的第一个byte是byte0,从byte1024开始往后的一部分数据。由于block size最小时1024bytes,所以superblock在block1中(此时block的大小正好是1024bytes),也可能是在block 0中。

超级块保存了文件系统设定的文件块大小、操作函数、inode链表等重要信息。

二、查看分区设备信息

一般情况下我们是能够通过一些命令查看到分区的一些信息,如果super block有损坏,则该分区设备则不能够正常使用,还有可能不能通过命令查看设备分区的信息。

命令:

dumpe2fs          /dev/sdb1

tune2fs     -h      /dev/sdb1

linux-iu82:/ # dumpe2fs -h /dev/sdb1
dumpe2fs 1.43. (-Jan-)
Filesystem volume name: <none> #文件系统的名称
Last mounted on: /a #是否挂载及挂载点
Filesystem UUID: cd22c2f7-d461-4cbe-973b-16d0b584a7b2
Filesystem magic number: 0xEF53
Filesystem revision #: (dynamic)
Filesystem features: has_journal ext_attr resize_inode dir_index filetype extent 64bit flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
Filesystem flags: signed_directory_hash
Default mount options: user_xattr acl
Filesystem state: clean #正常,异常:clean with errors或not clean whith errors
Errors behavior: Continue
Filesystem OS type: Linux #文件系统类型
Inode count: #inode总的个数
Block count: #block总的个数
Reserved block count:
Free blocks: #空闲的block个数
Free inodes: #空闲的iNode个数
First block: #第一个超级块编号=
Block size: #块大小,这里是4k
Fragment size: #分块大小
Group descriptor size:
Reserved GDT blocks: #保留的GDT块大小
Blocks per group: #每个块组的block的个数
Fragments per group:
Inodes per group: #每个块组的inode个数
Inode blocks per group:
…………

三、查看备份块

mkfs.ext4 -n /dev/sdb1(查看备份块时需要将分区卸载)

linux-iu82:/ # mkfs.ext4 -n /dev/sdb1
mke2fs 1.43. (-Jan-)
/dev/sdb1 contains a ext4 file system
last mounted on /a on Wed Jun ::
Proceed anyway? (y,N) y
Creating filesystem with 4k blocks and inodes #块大小4k
Filesystem UUID: 9e8e093e-183d-42ed-9e1f-b414673add53
Superblock backups stored on blocks: #查看备份超级块
, , , , , , ,

四、尝试修复超级块

  1. 1.   已知文件系统格式

    1.1.    在已知文件系统的情况下可以直接使用:

      mkfs.type -n /dev/sdb1

      进行查看分区的备份块。(注意查看备份块的时候需要将分区卸载)

    1.2.    尝试挂载备份块

    当文件系统出现损坏时,分区是挂载不上去的,这个时候尝试对分区备份块进行挂载,如果该备份超级块块可以被挂载上,并且上面数据没有问题,那么就可以使用该备份超级块进行修复

mount -t ext4 -o sb= /dev/sdb1 /a        #以32768为例,block的大小是4k,而mount的sb参数的块大小是1k,所以挂载时需要进行单位转换,转换成1k则是32768*=。
若挂载成功则可以使用该备份块进行修复

    1.3.    尝试使用备份块修复文件系统

      尝试修复前一定要先卸载该设备,否则会导致部分数据丢失

      且修复过程中需要确认的信息要自己观察分析,避免丢失数据

wyc:/ # fsck.ext4 -b  /dev/sdb1
e2fsck 1.43. (-Jan-)
/dev/sdb1 was not cleanly unmounted, check forced.
Pass : Checking inodes, blocks, and sizes
Pass : Checking directory structure
Pass : Checking directory connectivity
Pass : Checking reference counts
Pass : Checking group summary information
Free blocks count wrong for group # (, counted=).
Fix<y>? yes
Free blocks count wrong for group # (, counted=).
Fix<y>? yes
Free blocks count wrong for group # (, counted=).
Fix<y>? yes
Free blocks count wrong for group # (, counted=).
Fix<y>? yes
Free blocks count wrong (, counted=).
Fix<y>? yes
Free inodes count wrong for group # (, counted=).
Fix<y>? yes
Free inodes count wrong for group # (, counted=).
Fix<y>? yes
Directories count wrong for group # (, counted=).
Fix<y>? yes
Free inodes count wrong for group # (, counted=).
Fix<y>? yes
Directories count wrong for group # (, counted=).
Fix ('a' enables 'yes' to all) <y>? yes
Free inodes count wrong for group # (, counted=).
Fix ('a' enables 'yes' to all) <y>? yes
Directories count wrong for group # (, counted=).
Fix ('a' enables 'yes' to all) <y>? yes
Free inodes count wrong for group # (, counted=).
Fix ('a' enables 'yes' to all) <y>? yes
Directories count wrong for group # (, counted=).
Fix<y>? yes
Free inodes count wrong (, counted=).
Fix<y>? yes /dev/sdb1: ***** FILE SYSTEM WAS MODIFIED *****
/dev/sdb1: / files (0.0% non-contiguous), / blocks

    1.4.    全部super block损坏修复(数据丢失风险大)

      在所有superblock损坏后,只能通过重建进行修复

      命令:  mkfs.ext4 -S /dev/sdb1

wyc:~ # mkfs.ext4 -S /dev/sdb1
mke2fs 1.43. (-Jan-)
Creating filesystem with 4k blocks and inodes
Filesystem UUID: 5c9e2db1-5cf6--b051-7eefef7c6018
Superblock backups stored on blocks:
, , , , , , , Allocating group tables: done
/dev/sdb1 may be further corrupted by superblock rewrite
Proceed anyway? (y,N) y
Skipping journal creation in super-only mode
Writing superblocks and filesystem accounting information: done

      重建完成后进行修复:fsck.ext4 -y /dev/sdb1

wyc:~ # fsck.ext4 -y /dev/sdb1
e2fsck 1.43. (-Jan-)
One or more block group descriptor checksums are invalid. Fix<y>? yes
Group descriptor checksum is 0x20a2, should be 0x8d20. FIXED.
Group descriptor checksum is 0x1342, should be 0xdec3. FIXED.
Group descriptor checksum is 0x1038, should be 0xddb9. FIXED.
Group descriptor checksum is 0xa34e, should be 0x6ecf. FIXED.
Group descriptor checksum is 0x1db9, should be 0xd038. FIXED.
Group descriptor checksum is 0xa2de, should be 0x6f5f. FIXED.
Group descriptor checksum is 0xf83c, should be 0x35bd. FIXED.
Group descriptor checksum is 0x4b4a, should be 0x86cb. FIXED.
Group descriptor checksum is 0x973c, should be 0x5abd. FIXED.
Group descriptor checksum is 0xe044, should be 0x2dc5. FIXED.
Group descriptor checksum is 0xe33e, should be 0x2ebf. FIXED.
Group descriptor checksum is 0xcf59, should be 0x02d8. FIXED.
Group descriptor checksum is 0xeebf, should be 0x233e. FIXED.
Group descriptor checksum is 0x9e92, should be 0x5313. FIXED.
Group descriptor checksum is 0xe851, should be 0x25d0. FIXED.
Group descriptor checksum is 0xc436, should be 0x09b7. FIXED.
Group descriptor checksum is 0x516a, should be 0x9ceb. FIXED.
Group descriptor checksum is 0xe962, should be 0x24e3. FIXED.
Group descriptor checksum is 0x3bc2, should be 0xf643. FIXED.
Group descriptor checksum is 0x75a2, should be 0xb823. FIXED.
Group descriptor checksum is 0xde81, should be 0x1300. FIXED.

    修复完成直接进行挂载即可。

  1. 2.   不确定文件系统

    在不知道文件系统的情况下,可以使用fsck -r /dev/sdb1 命令进行修复(注意提示内容),这种修复可能会导致文件正常恢复,但是文件系统发生改变,比如原本是ext4,修复后变成了ext3或ext2,需要手动升级一下。(一般不建议此方法)

    命令:Fsck -r /dev/sdb1

wyc:~ # fsck -r /dev/sdb1
fsck from util-linux 2.29.
e2fsck 1.43. (-Jan-)
ext2fs_open2: Bad magic number in super-block
fsck.ext2: Superblock invalid, trying backup blocks...
/dev/sdb1 was not cleanly unmounted, check forced.
Pass : Checking inodes, blocks, and sizes
Pass : Checking directory structure
Pass : Checking directory connectivity
Pass : Checking reference counts
Pass : Checking group summary information
Block bitmap differences: -(--)
Fix? yes Free blocks count wrong for group # (, counted=).
Fix? yes Free blocks count wrong for group # (, counted=).
Fix? yes Free blocks count wrong for group # (, counted=).
Fix? yes Free blocks count wrong (, counted=).
Fix? yes Free inodes count wrong for group # (, counted=).
Fix? yes Free inodes count wrong for group # (, counted=).
Fix? yes Directories count wrong for group # (, counted=).
Fix? yes Free inodes count wrong for group # (, counted=).
Fix? yes

    修复后正常挂载即可。

上一篇:在CentOS6或RHEL6恢复上ext4文件系统误删除的文件


下一篇:ext4文件系统特性浅析