ハードディスクエラーで起動しなくなった Ubuntu を fsck で誤魔化す

家のキッズたちのために Ubuntu Linux ベースの PC 環境を整備していました。

「Ubuntu で MINECRAFT」
https://qiita.com/nanbuwks/items/df10bba9f0ed473827d7



このうち、HDD で構築した環境があったのですが落としたり落下させたりしていたので起動しなくなってしまいました。


![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/139524/f296c72c-2558-bcb1-abf7-b7f52e9c1548.png)

キッズ用には HDD は避けたほうがいいかな？ とりあえず SSD や USB メモリに換装するまでのつなぎとして、適当に誤魔化して動かすようにしました。

# シングルユーザーモード? 

こういうときはシングルユーザーモードで fsck をしますが、Ubuntu ではどうしていたかな？ 

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/139524/1f94d3dc-42a7-208b-6d50-3faf83b47ecd.png)

ここまで来たけれどもここから fsck が呼べなかったので別 USB メモリから Ubuntu を動かして fsck できるようにしました。

# USBメモリを使って fsck 


![Screenshot from 2021-04-10 04-12-05.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/139524/b8f8cbd5-bf69-c792-47de-1eef6067807f.png)

Ubuntu20.04 で起動してターミナルを開きます。



` $ sudo fdisk -l ` で HDD のアドレスを調べます。

```

$ sudo fdisk -l
Disk /dev/loop0: 1.10 GiB, 2123702272 bytes, 4147856 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/loop1: 29.9 MiB, 31334400 bytes, 61200 sectors
Units: sectors of 1 * 512 = 512 bytes
.
.
.

Disk /dev/sda: 465.78 GiB, 500107862016 bytes, 976773168 sectors
Disk model: HGST HTS725050A7
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0x966ff116

Device     Boot   Start       End   Sectors   Size Id Type
/dev/sda1  *       2048   1050623   1048576   512M  b W95 FAT32
/dev/sda2       1052670 976771071 975718402 465.3G  5 Extended
/dev/sda5       1052672 976771071 975718400 465.3G 83 Linux

Partition 2 does not start on physical sector boundary.




Disk /dev/sdb: 28.66 GiB, 30752636928 bytes, 60063744 sectors
Disk model: Ultra Fit       
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00000000

Device     Boot Start      End  Sectors  Size Id Type
/dev/sdb1  *       32 60063743 60063712 28.7G  c W95 FAT32 (LBA)



```
![Screenshot from 2021-04-10 04-12-35.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/139524/89f03ca4-2124-530d-4e74-5c20f3957afc.png)


 /dev/sda でした。

まず /dev/sda1。


```
$ sudo fsck /dev/sda1
fsck from util-linux 2.34
fsck.fat 4.1 (2017-01-24)
0x41: Dirty bit is set. Fs was not properly unmounted and some data may be corrupt.
1) Remove dirty bit
2) No action
? 1
Perform changes ? (y/n) y
/dev/sda1: 0 files, 1/130812 clusters

```

![Screenshot from 2021-04-10 04-13-00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/139524/22e3c418-2397-6030-191d-5985c300b3a3.png)

ここは恐らく問題が無い。


次に /dev/sda5

```

$ sudo fsck /dev/sda5
fsck from util-linux 2.34
e2fsck 1.45.5 (07-Jan-2020)
ext2fs_open2: Superblock checksum does not match superblock
fsck.ext4: Superblock invalid, trying backup blocks...
Superblock needs_recovery flag is clear, but journal has data.
Recovery flag not set in backup superblock, so running journal anyway.
/dev/sda5: recovering journal
Error reading block 60850860 (Input/output error).  Ignore error<y>? yes
Force rewrite<y>? yes
JBD2: Invalid checksum recovering block 68681781 in log
Error reading block 60851762 (Input/output error).  Ignore error<y>? yes
Force rewrite<y>? yes
JBD2: Invalid checksum recovering block 111149515 in log
Error reading block 60852186 (Input/output error).  Ignore error<y>? yes
Force rewrite<y>? yes
JBD2: Invalid checksum recovering block 7872947 in log
Error reading block 60852663 (Input/output error).  Ignore error<y>? yes
Force rewrite<y>? yes
JBD2: Invalid checksum recovering block 70778988 in log
Error reading block 60856641 (Input/output error).  Ignore error<y>? yes
```

きりがないので一旦キャンセル。

```

 
/dev/sda5: e2fsck canceled.

/dev/sda5: ***** FILE SYSTEM WAS MODIFIED *****
```

改めて y オプションをつけて実行

```

$ sudo fsck /dev/sda5 -y
fsck from util-linux 2.34
e2fsck 1.45.5 (07-Jan-2020)
/dev/sda5 was not cleanly unmounted, check forced.
Pass 1: Checking inodes, blocks, and sizes
Error reading block 55581559 (Input/output error) while getting next inode from scan.  Ignore error? yes

Force rewrite? yes

Error reading block 55581780 (Input/output error) while getting next inode from scan.  Ignore error? yes

Force rewrite? yes

Error reading block 55582001 (Input/output error) while getting next inode from scan.  Ignore error? yes

Force rewrite? yes

.
.
.

Error reading block 61873469 (Input/output error) while getting next inode from scan.  Ignore error? yes

Force rewrite? yes

Error reading block 61873703 (Input/output error) while getting next inode from scan.  Ignore error? yes

Force rewrite? yes

Error reading block 61873938 (Input/output error) while getting next inode from scan.  Ignore error? yes

Force rewrite? yes

Deleted inode 23593234 has zero dtime.  Fix? yes

Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
Error reading block 57671691 (Input/output error) while reading inode and block bitmaps.  Ignore error? yes

Force rewrite? yes

Block bitmap differences:  +(4096000--4097083) -(6060032--6060543) -(9281024--9281463) +(20480000--20481083) +(23887872--23888955) +(71663616--71664699) +(78675968--78677051) +(102400000--102401083) -(120523409--120523413)
Fix? yes

Free blocks count wrong for group #0 (23454, counted=2984).
Fix? yes

Free blocks count wrong for group #1 (31684, counted=963).
Fix? yes

Free blocks count wrong for group #2 (32768, counted=0).
Fix? yes

Free blocks count wrong for group #3 (31684, counted=964).
Fix? yes



.
.
.

Free blocks count wrong for group #3709 (32768, counted=20737).
Fix? yes

Free blocks count wrong for group #3710 (32768, counted=32738).
Fix? yes

Free blocks count wrong (119769515, counted=113199036).
Fix? yes

Inode bitmap differences:  -1444172 -23593234
Fix? yes

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

Free inodes count wrong for group #48 (8192, counted=6150).
Fix? yes

Directories count wrong for group #48 (0, counted=679).
Fix? yes
.
.
.

Free inodes count wrong for group #3680 (8192, counted=5923).
Fix? yes

Directories count wrong for group #3680 (0, counted=670).
Fix? yes

Free inodes count wrong for group #3696 (8192, counted=3957).
Fix? yes

Directories count wrong for group #3696 (0, counted=499).
Fix? yes

Free inodes count wrong (30498805, counted=30126918).
Fix? yes

Padding at end of inode bitmap is not set. Fix? yes


/dev/sda5: ***** FILE SYSTEM WAS MODIFIED *****
/dev/sda5: 371898/30498816 files (0.9% non-contiguous), 8765764/121964800 blocks
ubuntu@ubuntu:~$ 


```


何とか終わりました。

# 復旧

HDD だけで起動してみる

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/139524/318b854f-e437-6843-104e-b95447c649c9.png)

結構エラー出てますが、しばらく待っていると


![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/139524/d815aeff-fb65-e907-81da-13855717730e.png)

起動しました。