按下電源鍵到登入畫面,Linux 經歷了什麼?本文將啟動流程拆解為五大階段,並對應現代 systemd 架構說明,幫助你從核心視角理解系統初始化。
.efi 檔案;Legacy BIOS 讀取 MBR 前 512 Bytes。grubx64.efi 或 shimx64.efi (Secure Boot)。core.img 載入後讀取 /boot/grub2/grub.cfg (或 /boot/efi/EFI/.../grub.cfg)。chainloader 呼叫 Windows Boot Manager 實現多重開機。vmlinuz) 至記憶體。devtmpfs、sysfs、procfs。root= 參數或 initrd/initramfs 中的腳本掛載 / (read-only)。init (PID 1):傳統 SysV init 或現代 systemd。包含核心模組、udev、LVM/RAID/加密工具、掛載根裝置腳本。若根裝置在 LVM/RAID/加密/網路儲存上,沒有 initramfs 就無法掛載根檔案系統。
/etc/rc.d/rc.sysinit → 設定環境、網路、swap、檢查/掛載檔案系統
/etc/rc.d/rc.d/ → 依運行等級啟動/停止服務腳本 (Sxx/Kxx)
.service, .socket, .target, .mount…default.target (通常連結 graphical.target 或 multi-user.target) 為啟動終點。systemctl, journalctl, systemd-analyze。類似 Windows 虛擬記憶體。當實體記憶體不足,核心將較少使用的記憶體頁面換出至 Swap 分區/檔案。建議:
• 記憶體 ≤ 8GB:Swap = 1~2× RAM
• 記憶體 > 8GB:Swap = 8~16 GB
• 需休眠功能:Swap ≥ RAM
BIOS/UEFI POST
↓
MBR / ESP (Bootloader)
↓
GRUB2 (選單、參數)
↓
Linux Kernel + initramfs
↓
掛載根檔案系統 (ro)
↓
systemd (PID 1) → 並行啟動服務 → 登入畫面
