ブートローダーの修復
はじめに
先日、ゼロからのOS自作入門をやってたらブートローダーを消し飛ばしてしまいました。具体的に言うと /boot/efi
配下を書き換えちゃったってことです。
これによりしばらく、プライベート用の PC が動かなくなっていたのですが、無事修復できたので、後世のために記録を残しておきます。
準備
まず、以下を用意します。
- 別の PC(今回は MacBook Pro)
- USB メモリ(OS が入るくらいのサイズ)
テキトーな Linux ディストロを USB にインストールします。
macOS だと BalenaEtcher が便利です。
手順
USB ブートで Linux を立ち上げて以下を実行していきます。
調査
まずはディスクの状況を確認します。
$ sudo fdisk -l Disk /dev/loop0: 1.2 GiB, 1277792256 bytes, 2495688 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/sda: 931.5 GiB, 1000204886016 bytes, 1953525168 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 Disklabel type: gpt Disk identifier: C4D9398D-765B-4BCE-9CA3-8084194B8DB8 Device Start End Sectors Size Type /dev/sda1 2048 1023999 1021952 499M Windows recovery environment /dev/sda2 1024000 1226751 202752 99M EFI System /dev/sda3 1226752 1259519 32768 16M Microsoft reserved /dev/sda4 1259520 840779272 839519753 400.3G Microsoft basic data /dev/sda5 840779776 842031103 1251328 611M Windows recovery environment /dev/sda6 842033152 1945744089 1103710938 526.3G Linux filesystem /dev/sda7 1945745408 1953523711 7778304 3.7G Linux swap Disk /dev/sdb: 14.8 GiB, 15871246336 bytes, 30998528 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 Disklabel type: dos Disk identifier: 0xad92e0dc Device Boot Start End Sectors Size Id Type /dev/sdb1 * 64 2904959 2904896 1.4G 0 Empty /dev/sdb2 3532 9035 5504 2.7M ef EFI (FAT-12/16/32)
/dev/sda2
がブートローダー用のパーティションで、/dev/sda6
ルートパーティションです。
環境の再現
/dev/sda2
や /dev/sda6
、その他諸々のデバイスをマウントして環境を再現し chroot
します。
$ sudo mount /dev/sda6 /mnt/ $ sudo mount /dev/sda2 /mnt/boot/efi $ sudo for i in /sys /proc /run /dev; do sudo mount --bind "$i" "/mnt$i"; done $ sudo chroot /mnt
ブートローダーの再インストール
念願のブートローダーインストールです。
$ grub-install /dev/sda $ upgrade-grub
あとは、blkid
コマンドで調べた UUID と /etc/fstab
にある EFI パーティションの UUID が異なっていたら、/etc/fstab
を書き換えます。
$ blkid | grep EFI
あとは再起動して終わり
おわりに
これでいくらブートローダーをふっ飛ばしても怖くない