Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

GRUB 開機載入器手動安裝與分區指定:root/setup 指令實戰

編輯:Linux 教學

多重開機環境下,GRUB 安裝位置決定哪個分區的 menu.lst(GRUB Legacy)或 grub.cfg(GRUB 2)被讀取。本文示範 GRUB 命令列模式下 root 與 setup 指令操作,掌控開機選單所有權。

一、 進入 GRUB 命令列

開機選單按 c 進入命令列,提示符 grub>。

二、 核心指令語法

grub> root (hdX,Y)
grub> setup (hd0)
  • hdX:硬碟編號(從 0 起算)。hd0 = 第一顆硬碟,hd1 = 第二顆。
  • Y:分區編號(從 0 起算)。(hd0,0) = 第一顆硬碟第一分區。
  • root (hdX,Y):告訴 GRUB 核心映像與 menu.lst 所在分區。
  • setup (hd0):將 GRUB Stage1 寫入 hd0 的 MBR(主開機記錄)。

三、 實戰範例

場景:Linux 根分區在第二顆硬碟第一分區,欲將 GRUB 裝入第一顆硬碟 MBR

grub> root (hd1,0)
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)"... 16 sectors are embedded.
succeeded
Running "install /boot/grub/stage1 (hd0) (hd0)1+16 p (hd1,0)/boot/grub/stage2 /boot/grub/menu.lst"... succeeded
Done.

四、 GRUB 2 現代對應操作

GRUB 2(Ubuntu 9.10+/RHEL 7+)使用 grub-install 與 grub-mkconfig:

# 安裝至 MBR
grub-install /dev/sda
# 生成配置
grub-mkconfig -o /boot/grub/grub.cfg
# 或更新現有配置
update-grub

多重開機修復常用:
grub-install --boot-directory=/mnt/boot /dev/sda(從 LiveCD 掛載目標系統後執行)

五、 常見問題排查

現象原因解決
Error 15: File not foundroot 指定分區錯誤,找不到 stage2/menu.lst用 find /boot/grub/stage1 確認正確分區
開機進入 grub rescue>MBR 階段 1.5 找不到核心模組LiveCD 重裝 grub-install
Windows 更新後 GRUB 消失Windows 覆寫 MBRLiveCD 重裝 GRUB

六、 結語

root 指向配置,**setup 寫入 MBR**。GRUB Legacy 時代的手動操作讓我們理解開機載入器的兩階段架構:MBR (446 bytes) → Stage1.5 (檔案系統驅動) → Stage2 (完整功能) → menu.lst。現代 GRUB 2 雖自動化程度高,但理解底層原理仍是修復複雜多重開機故障的關鍵。

Copyright © Windows教程網 All Rights Reserved