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

Linux 啟動流程與 GRUB 引導載入器深度解析

編輯:Linux 教學

從按下電源鍵到登入提示符,Linux 經歷固韌體、引導載入器、核心、init 系統四大階段。本文聚焦 MBR/GPT 分區結構、GRUB 兩階段引導流程,並提供 MBR 備份恢復實操指令。

硬碟分區與 MBR 結構

+---------------------+  ← 磁區 0 (LBA 0)
| MBR (512 bytes)     |
|  ├─ Boot Loader (444 bytes)  ← GRUB Stage 1
|  ├─ Partition Table (64 bytes)  ← 最多 4 個主分區
|  └─ Magic Number (2 bytes)  ← 0xAA55
+---------------------+
| Partition 1 (boot)  |
| Partition 2 (root)  |
| Partition 3 (swap)  |
| Partition 4 (home)  |
+---------------------+
  • MBR 損壞 → 系統無法啟動,定期備份至關重要
  • GPT 磁碟使用 Protective MBR + EFI System Partition (ESP)

MBR 備份與還原

# 備份(假設 /dev/sda 為啟動磁碟)
dd if=/dev/sda of=/root/mbr_backup.bin bs=512 count=1
# 或可讀格式
dd if=/dev/sda bs=512 count=1 | od -Ax -tx1z -v > /root/mbr_hex.txt

# 還原(極度謹慎,確認磁碟無誤)
dd if=/root/mbr_backup.bin of=/dev/sda bs=512 count=1

GRUB 2 多階段引導流程

  1. BIOS/UEFI 讀取 MBR/ESP,驗證 Magic Number
  2. Stage 1 (boot.img, 444 bytes):嵌入 MBR,唯一任務載入 Stage 1.5 / Stage 2
  3. Stage 1.5 (core.img):檔案系統驅動,讀取 /boot/grub 目錄
  4. Stage 2 (kernel + initrd):顯示選單、載入核心參數、啟動 initramfs
  5. 核心初始化:掛載根檔案系統、啟動 /sbin/init (systemd/OpenRC/SysVinit)

GRUB 關鍵檔案

檔案用途
/boot/grub/grub.cfg主配置(由 grub-mkconfig 自動生成,勿手改)
/etc/default/grub使用者設定(預設啟動項、超時、核心參數)
/etc/grub.d/腳本目錄(偵測 OS、主題、自訂選單)

常用管理操作

# 重新生成 grub.cfg
grub-mkconfig -o /boot/grub/grub.cfg
# 或
update-grub

# 安裝 GRUB 至磁碟
grub-install /dev/sda

# 修改預設啟動核心
grub-set-default "Advanced options for Ubuntu>Ubuntu, with Linux 6.5.0-18-generic"

# 臨時編輯啟動參數(啟動選單按 e)
# 永久修改:編輯 /etc/default/grub → GRUB_CMDLINE_LINUX_DEFAULT → update-grub

UEFI 系統差異

  • 無 MBR,使用 ESP (FAT32 分區) 儲存 EFI/BOOT/BOOTX64.EFI 或 EFI/ubuntu/grubx64.efi
  • efibootmgr 管理啟動項
  • Secure Boot 需簽名啟動載入器

救援模式實戰

# Live USB 進入後
mkdir /mnt/sys
mount /dev/sda2 /mnt/sys          # 根分區
mount /dev/sda1 /mnt/sys/boot     # /boot 分區(如有)
mount --bind /dev /mnt/sys/dev
mount --bind /proc /mnt/sys/proc
mount --bind /sys /mnt/sys/sys
chroot /mnt/sys

# 修復 GRUB
grub-install /dev/sda
update-grub
exit
reboot

理解啟動流程是排除「開機進不去」恐慌的解藥。MBR 備份、GRUB 重裝、chroot 救援,三板斧打通生死關。

Copyright © Windows教程網 All Rights Reserved