如何修復被破壞的分區文件系統

· 準備階段

因爲條件緣由,我只能在VMware虛擬機上進行模擬實驗,開機的時候,發現sdb磁盤正常加載node

而後經過lsblk命令查看掛載狀況linux

[root@localhost ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part 
  ├─centos-root 253:0    0   17G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0    5G  0 disk 
└─sdb1            8:17   0    5G  0 part /data
sr0              11:0    1 1024M  0 rom 

進入/data目錄,查看內容,並新建文件test,寫入一句話:「this is test file.」,查看test內容ios

[root@localhost ~]# cd /data/
[root@localhost data]# ls
lost+found
[root@localhost data]# echo "this is test file." > test
[root@localhost data]# ls
lost+found  test
[root@localhost data]# cat test 
this is test file.

發現能夠查看並正常寫入文件,如今進入破壞階段centos

· 破壞階段

硬盤進行分區格式化(ext4)會建立硬盤內的文件存取系統表(superblock、block、inode信息),文件新建、文 件寫入到分區內,正常存取block塊。測試

使用dd命令給硬盤寫入文件,破壞硬盤內的文件存取系統表,給硬盤直接寫入空文件。直接將文件寫入給磁盤隨機的扇 區裏面。

[root@localhost /]# dd if=/dev/zero of=/dev/sdb bs=1 count=2048
2048
+0 records in 2048+0 records out 2048 bytes (2.0 kB) copied, 0.00410516 s, 499 kB/s

破壞磁盤後,進入/data目錄,寫入文件test2this

[root@localhost data]# touch test2
touch: cannot touch ‘test2’: Input/output error

發現寫入文件報錯,reboot重啓機器報錯,輸入密碼直接進入單用戶系統spa

 

· 修復階段

使用fsck修改文件系統code

[root@localhiost ~]# fsck -t ext4 -y /dev/sdb
fsck from util-linux 2.23.2
e2fsck 1.42.9(28-DEC-2013)
ext2fs_open2:Bad magic number in super-block
fsck.ext4:Superblock invalid,trying backup blocks...
/dev/sdb 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 #1 (31740, counted=31739).
Fix? yes

Free blocks count wrong (5116558, counted=5116557).
Fix? yes

Free inodes count wrong for group #0 (8181, counted=8180).
Fix? yes

Free inodes count wrong (1310709, counted=1310708).
Fix? yes

/dev/sdb : ***** FILE SYSTEM WAS MODIFIED *****
/dev/sdb : 12/1310720 files (0.0% non-contiguous), 126323/5242880 blocks

修復完成後reboot,重啓系統。orm

從新進入系統,而後從新進行寫入文件測試。blog

[root@localhost ~]# cd /data 
[root@localhost data]# ls 
lost+found test
[root@localhost data]# cat test
this is test file.
[root@localhost deta]# echo "this is test2 file." > test2
[root@localhost data]# ls 
lost+found test test2
[root@localhost data]# cat test2
this is test2 file.

至此,分區文件系統修復完成。

相關文章
相關標籤/搜索