系統有一顆硬碟壞了,抽出來到別台 RedHat RHEL 6.5 檢查得到一個無情的結果
[root@localhost Desktop]# fsck -y /dev/sdb1 fsck from util-linux 2.20.1 e2fsck 1.42.9 (4-Feb-2014) ext2fs_open2: Bad magic number in super-block fsck.ext2: Superblock invalid, trying backup blocks... fsck.ext2: Bad magic number in super-block while trying to open /dev/sdb1 The superblock could not be read or does not describe a valid ext2/ext3/ext4 filesystem. If the device is valid and it really contains an ext2/ext3/ext4 filesystem (and not swap or ufs or something else), then the superblock is corrupt, and you might try running e2fsck with an alternate superblock: e2fsck -b 8193 <device> or e2fsck -b 32768 <device>
無效的 Superblock 是代表?
先來看看 Linux 下的檔案儲存是採用什麼方式,他採用了 superblock / inode /data block 的方式來儲存資料.
data block
資料在儲存時不太可能每一次都有連續的空間可供資料的儲存,所以資料會切割成固定大小分開存放,這大小的空間就是 data block 但在談 data block 大小前,先來說說硬碟的儲存最小單位 block size ,通常硬碟在出廠前都先經過低階格式化,而預設的大小就是 512bytes , 但512bytes 真的太小所以在 Linux 下我們用的是另外一種單位就是 data block ,而他的大小必須為 512bytes 的倍數. 512,1024,2048,4096 bytes 通常在 Linux 下為 4096bytes.Data, Data block 與 block 的相關如下所是.
Data –> Data block(s) –> block(s)
inode
剛剛說到 data block 的資料是一塊塊分散的儲存(索引式檔案系統),所以此時必須要一塊資料要紀錄哪些 data block 是屬於哪一個 data 的, 而記錄了這些資料的就叫做 inode ,每一個檔案都會對應到一個 inode 他的大小為 128bytes 除了紀錄 data block 的位置外還儲放了檔案的權限與相關屬性.
superblock
那剛剛 inode/block 的使用情況(使用量,剩餘…)是由誰來紀錄,就是透過 superblock 基本上他記錄該檔案系統所有的資訊. 所以我們可以很清楚了解 superblock / inode / data block 的關係.
superblock –>inode –> data block
更多有關於 Linux 的檔案系統可以參考 https://benjr.tw/93162
那 無效的 Superblock 應該就 GG 了.
不過我們還是來看一下正常情況要怎麼救.
被掛載的磁區是不能做 fsck ,如果是系統碟那就要用其他方式開機再來下 fsck 指令.
[root@localhost Desktop]# fsck /dev/sdb7 fsck from util-linux 2.20.1 e2fsck 1.42.9 (4-Feb-2014) /dev/sdb7 is mounted. e2fsck: Cannot continue, aborting.
常用的兩個參數 -y , -a 都是可以做自動修復,不需要一一輪詢.
- -y
For some filesystem-specific checkers, the -y option will cause the fs-specific fsck to always attempt to fix any detected filesystem corruption automatically. - -a
Automatically repair the filesystem without any questions.
沒有下參數 -y 或是 -a 系統會一直詢問你是否修復.
[root@localhost Desktop]# fsck /dev/sdb6 fsck from util-linux 2.20.1 e2fsck 1.42.9 (4-Feb-2014) Superblock has an invalid journal (inode 8). Clear<y>? yes *** ext3 journal has been deleted - filesystem is now ext2 only *** /dev/sdb6 was not cleanly unmounted, check forced. Pass 1: Checking inodes, blocks, and sizes Journal inode is not in use, but contains data. Clear<y>? yes yInode 10000, i_blocks is 249376, should be 224800. Fix<y>? yes Inode 49537, i_blocks is 10280, should be 112. Fix<y>? yes Inode 49541, i_blocks is 2080, should be 104. Fix<y>? yes Inode 49549, i_blocks is 160, should be 104. Fix<y>? yes
[root@localhost Desktop]# fsck -a /dev/sdb6 fsck from util-linux 2.20.1 /dev/sdb6 was not cleanly unmounted, check forced. /dev/sdb6: Inode 270816, i_blocks is 24296, should be 16512. FIXED. /dev/sdb6: Inode 270611, i_blocks is 15560, should be 8304. FIXED. /dev/sdb6: Inode 270818, i_blocks is 1512, should be 104. FIXED.
[root@localhost Desktop]# fsck -y /dev/sdb6 ... /dev/sdb6: ***** FILE SYSTEM WAS MODIFIED ***** /dev/sdb6: 340410/655776 files (0.6% non-contiguous), 2374655/2622603 blocks
常用來修復硬碟指令 fsck 與 e2fsck ,他們的差別是!!
- fsck is used to check and optionally repair one or more Linux filesys‐tems.
- e2fsck is used to check the ext2/ext3/ext4 family of file systems.
e2fsck 主要是針對 ext2/ext3/ext4 而 fsck 支援多種檔案格式.
[root@localhost Desktop]# ll /sbin/fsck* -rwxr-xr-x. 1 root root 32088 2018-01-26 15:41 /sbin/fsck -rwxr-xr-x. 1 root root 12408 2018-01-26 15:41 /sbin/fsck.cramfs -rwxr-xr-x. 5 root root 190352 2018-06-19 23:42 /sbin/fsck.ext2 -rwxr-xr-x. 5 root root 190352 2018-06-19 23:42 /sbin/fsck.ext3 -rwxr-xr-x. 5 root root 190352 2018-06-19 23:42 /sbin/fsck.ext4 -rwxr-xr-x. 5 root root 190352 2018-06-19 23:42 /sbin/fsck.ext4dev lrwxrwxrwx. 1 root root 7 2019-03-14 17:01 /sbin/fsck.msdos -> dosfsck lrwxrwxrwx. 1 root root 7 2019-03-14 17:01 /sbin/fsck.vfat -> dosfsck
那如果是 VLM 的檔案格式要怎麼處理? 什麼是 LVM 請參考 – https://benjr.tw/174
我們要先找出來當初訂的 LV (LVM 的組成為 PV – VG – LV)
[root@localhost Desktop]# pvscan PV /dev/sda2 VG VolGroup lvm2 [19.51 GiB / 0 free] Total: 1 [19.51 GiB] / in use: 1 [19.51 GiB] / in no VG: 0 [0 ]
[root@localhost Desktop]# vgscan Reading all physical volumes. This may take a while... Found volume group "VolGroup" using metadata type lvm2
[root@localhost Desktop]# lvscan ACTIVE '/dev/VolGroup/home' [9.76 GiB] inherit ACTIVE '/dev/VolGroup/lv_root' [7.81 GiB] inherit ACTIVE '/dev/VolGroup/lv_swap' [1.94 GiB] inherit
知道 LVM 路徑後就可以使用 fsck 來修復 LVM 的檔案系統.
[root@localhost Desktop]# umount /dev/VolGroup/home
[root@localhost Desktop]# e2fsck -f /dev/VolGroup/home e2fsck 1.41.12 (17-May-2010) 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 /dev/VolGroup/home: 11/639584 files (0.0% non-contiguous), 78540/2557952 blocks
[root@localhost Desktop]# mount /dev/VolGroup/home /home