Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

GRUB 引導選單弄丟了?用 Live CD 重建 menu.lst

重灌系統把 GRUB 引導選單搞不見,是很多人遇到過的窘境。還好修復並不難,用一條 Live CD 就能搞定。

修復步驟

用 Live CD(Ubuntu 使用者即 Desktop CD 或 DVD)開機,打開終端機(Applications → Accessories → Terminal),依序執行:

sudo grub
find /boot/grub/stage1
root (hdX,Y)
setup (hdX)
quit

其中的 X、Y 由 find 指令的輸出決定。例如:

grub> find /boot/grub/menu.lst
   (hd0,7)

這裡的 grub> 是執行 sudo grub 後進入 GRUB 環境的提示符,不需輸入;由此得知 X 為 0、Y 為 7。

逐條指令解釋

sudo grub:以 root 身分啟動 GRUB,否則會出錯。
find /boot/grub/stage1:找出 /boot 所在分區,改用 find /boot/grub/menu.lst 效果相同。這一步其實可以跳過——執行 root (hdX, 時連按兩下 Tab 就會列出可用分區。
root (hdX,Y):設定硬碟 hdX 的根文件系統,也就是 /boot 所在分區。
setup (hdX):自動把 GRUB 安裝到硬碟 hdX。
quit:離開 GRUB。

提醒:root 與 setup 後面都要留一個空格,少了就會報錯。

不以 root 身分會發生什麼

grub> find /boot/grub/stage1
Error 15: File not found
grub> root(hd0,7)
Error 27: Unrecognized command
grub> root (hd0,7)
Error 21: Selected disk does not exist
grub> setup (hd0)
Error 12: Invalid device requested

可見光是少了空格或權限不足,就會得到一連串令人困惑的錯誤訊息。

成功案例

grub> find /boot/grub/menu.lst
   (hd0,7)
grub> root (hd0,7)
grub> setup (hd0)
 Checking if "/boot/grub/stage1" exists... yes
 Checking if "/boot/grub/stage2" exists... yes
 Checking if "/boot/grub/e2fs_stage1_5" exists... yes
 Running "embed /boot/grub/e2fs_stage1_5 (hd0)"... 15 sectors are embedded. succeeded
 Running "install /boot/grub/stage1 (hd0) (hd0)1+15 p (hd0,7)/boot/grub/stage2 /boot/grub/menu.lst"... succeeded
Done.
grub> quit

其他尋找 /boot 分區的方法

也可以用 sudo fdisk -l 找出啟動分區(這裡不涉及 /boot 獨立分區的情況):

Disk /dev/sda: 160.0 GB, 160041885696 bytes
Device Boot Start   End    Blocks   Id  System
/dev/sda1   *     1     2295   18434556   7  HPFS/NTFS
/dev/sda2         2296  19456  137845732+  f  W95 Ext'd (LBA)
/dev/sda8   *     16558 17361  6458098+   83  Linux
/dev/sda9         17362 19393  16322008+  83  Linux
/dev/sda10        19394 19456  506016     82  Linux swap / Solaris

由此可判斷啟動分區是 /dev/sda8。

有人會疑惑:Linux 下寫作 sda8,GRUB 中卻是 (hd0,7),這是為什麼?因為新版 Ubuntu 把序列埠硬碟與並列埠硬碟都辨識為 sd,a 代表第一顆硬碟,8 代表第 4 個邏輯分區(1 到 4 保留給主分區,擴充分區中的邏輯分區從 5 開始算);而 GRUB 一律用 hd 代表硬碟,硬碟號與分區號都從 0 起算。

補充:連 /boot 都毀掉的情況

以上做法的前提是 /boot 下的檔案還完好。若情況更糟,可以先重建 mtab 並把 GRUB 裝進 MBR:

sudo grep -v rootfs /proc/mounts | sudo tee /etc/mtab
grub-install --no-floppy /dev/hda

之後再自行補上 menu.lst 的內容即可。

Copyright © Linux教程網 All Rights Reserved