Ext4文件系統修復
目錄
硬盤分區開頭、開頭的第一個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.8 (1-Jan-2018) Filesystem volume name: <none> #文件系統的名稱 Last mounted on: /a #是否掛載及掛載點 Filesystem UUID: cd22c2f7-d461-4cbe-973b-16d0b584a7b2 Filesystem magic number: 0xEF53 Filesystem revision #: 1 (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: 655360 #inode總的個數 Block count: 2621440 #block總的個數 Reserved block count: 131072 Free blocks: 2554687 #空閑的block個數 Free inodes: 655344 #空閑的iNode個數 First block: 0 #第一個超級塊編號=0 Block size: 4096 #塊大小,這里是4k Fragment size: 4096 #分塊大小 Group descriptor size: 64 Reserved GDT blocks: 1024 #保留的GDT塊大小 Blocks per group: 32768 #每個塊組的block的個數 Fragments per group: 32768 Inodes per group: 8192 #每個塊組的inode個數 Inode blocks per group: 512 …………
三、查看備份塊
mkfs.ext4 -n /dev/sdb1(查看備份塊時需要將分區卸載)
linux-iu82:/ # mkfs.ext4 -n /dev/sdb1 mke2fs 1.43.8 (1-Jan-2018) /dev/sdb1 contains a ext4 file system last mounted on /a on Wed Jun 12 11:03:20 2019 Proceed anyway? (y,N) y Creating filesystem with 2621440 4k blocks and 655360 inodes #塊大小4k Filesystem UUID: 9e8e093e-183d-42ed-9e1f-b414673add53 Superblock backups stored on blocks: #查看備份超級塊 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
四、嘗試修復超級塊
- 1. 已知文件系統格式
1.1. 在已知文件系統的情況下可以直接使用:
mkfs.type -n /dev/sdb1
進行查看分區的備份塊。(注意查看備份塊的時候需要將分區卸載)
1.2. 嘗試掛載備份塊
當文件系統出現損壞時,分區是掛載不上去的,這個時候嘗試對分區備份塊進行掛載,如果該備份超級塊塊可以被掛載上,並且上面數據沒有問題,那么就可以使用該備份超級塊進行修復
mount -t ext4 -o sb=131072 /dev/sdb1 /a #以32768為例,block的大小是4k,而mount的sb參數的塊大小是1k,所以掛載時需要進行單位轉換,轉換成1k則是32768*4=131072。 若掛載成功則可以使用該備份塊進行修復
1.3. 嘗試使用備份塊修復文件系統
嘗試修復前一定要先卸載該設備,否則會導致部分數據丟失
且修復過程中需要確認的信息要自己觀察分析,避免丟失數據
wyc:/ # fsck.ext4 -b 32768 /dev/sdb1 e2fsck 1.43.8 (1-Jan-2018) /dev/sdb1 was not cleanly unmounted, check forced. 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 Free blocks count wrong for group #6 (32254, counted=32253). Fix<y>? yes Free blocks count wrong for group #42 (32254, counted=32252). Fix<y>? yes Free blocks count wrong for group #50 (32254, counted=32253). Fix<y>? yes Free blocks count wrong for group #58 (32254, counted=32253). Fix<y>? yes Free blocks count wrong (2558144, counted=2558139). Fix<y>? yes Free inodes count wrong for group #0 (8181, counted=8180). Fix<y>? yes Free inodes count wrong for group #6 (8192, counted=8191). Fix<y>? yes Directories count wrong for group #6 (0, counted=1). Fix<y>? yes Free inodes count wrong for group #42 (8192, counted=8189). Fix<y>? yes Directories count wrong for group #42 (0, counted=1). Fix ('a' enables 'yes' to all) <y>? yes Free inodes count wrong for group #50 (8192, counted=8191). Fix ('a' enables 'yes' to all) <y>? yes Directories count wrong for group #50 (0, counted=1). Fix ('a' enables 'yes' to all) <y>? yes Free inodes count wrong for group #58 (8192, counted=8191). Fix ('a' enables 'yes' to all) <y>? yes Directories count wrong for group #58 (0, counted=1). Fix<y>? yes Free inodes count wrong (655349, counted=655342). Fix<y>? yes /dev/sdb1: ***** FILE SYSTEM WAS MODIFIED ***** /dev/sdb1: 18/655360 files (0.0% non-contiguous), 63301/2621440 blocks
1.4. 全部super block損壞修復(數據丟失風險大)
在所有superblock損壞后,只能通過重建進行修復
命令: mkfs.ext4 -S /dev/sdb1
wyc:~ # mkfs.ext4 -S /dev/sdb1 mke2fs 1.43.8 (1-Jan-2018) Creating filesystem with 2621440 4k blocks and 655360 inodes Filesystem UUID: 5c9e2db1-5cf6-4353-b051-7eefef7c6018 Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632 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.8 (1-Jan-2018) One or more block group descriptor checksums are invalid. Fix<y>? yes Group descriptor 0 checksum is 0x20a2, should be 0x8d20. FIXED. Group descriptor 1 checksum is 0x1342, should be 0xdec3. FIXED. Group descriptor 2 checksum is 0x1038, should be 0xddb9. FIXED. Group descriptor 3 checksum is 0xa34e, should be 0x6ecf. FIXED. Group descriptor 4 checksum is 0x1db9, should be 0xd038. FIXED. Group descriptor 5 checksum is 0xa2de, should be 0x6f5f. FIXED. Group descriptor 6 checksum is 0xf83c, should be 0x35bd. FIXED. Group descriptor 7 checksum is 0x4b4a, should be 0x86cb. FIXED. Group descriptor 8 checksum is 0x973c, should be 0x5abd. FIXED. Group descriptor 9 checksum is 0xe044, should be 0x2dc5. FIXED. Group descriptor 10 checksum is 0xe33e, should be 0x2ebf. FIXED. Group descriptor 11 checksum is 0xcf59, should be 0x02d8. FIXED. Group descriptor 12 checksum is 0xeebf, should be 0x233e. FIXED. Group descriptor 13 checksum is 0x9e92, should be 0x5313. FIXED. Group descriptor 14 checksum is 0xe851, should be 0x25d0. FIXED. Group descriptor 15 checksum is 0xc436, should be 0x09b7. FIXED. Group descriptor 16 checksum is 0x516a, should be 0x9ceb. FIXED. Group descriptor 17 checksum is 0xe962, should be 0x24e3. FIXED. Group descriptor 18 checksum is 0x3bc2, should be 0xf643. FIXED. Group descriptor 19 checksum is 0x75a2, should be 0xb823. FIXED. Group descriptor 20 checksum is 0xde81, should be 0x1300. FIXED.
修復完成直接進行掛載即可。
- 2. 不確定文件系統
在不知道文件系統的情況下,可以使用fsck -r /dev/sdb1 命令進行修復(注意提示內容),這種修復可能會導致文件正常恢復,但是文件系統發生改變,比如原本是ext4,修復后變成了ext3或ext2,需要手動升級一下。(一般不建議此方法)
命令:Fsck -r /dev/sdb1
wyc:~ # fsck -r /dev/sdb1 fsck from util-linux 2.29.2 e2fsck 1.43.8 (1-Jan-2018) ext2fs_open2: Bad magic number in super-block fsck.ext2: Superblock invalid, trying backup blocks... /dev/sdb1 was not cleanly unmounted, check forced. 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: -(2138112--2162687) Fix? yes Free blocks count wrong for group #16 (24544, counted=24543). Fix? yes Free blocks count wrong for group #48 (24544, counted=24542). Fix? yes Free blocks count wrong for group #64 (24544, counted=24543). Fix? yes Free blocks count wrong (2554687, counted=2554683). Fix? yes Free inodes count wrong for group #0 (8181, counted=8177). Fix? yes Free inodes count wrong for group #16 (8192, counted=8191). Fix? yes Directories count wrong for group #16 (0, counted=1). Fix? yes Free inodes count wrong for group #48 (8192, counted=8190). Fix? yes
修復后正常掛載即可。