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 內核啟動時根文件系統掛載的多種方式詳解

編輯:Linux 教學

核心問題:雞生蛋、蛋生雞

內核啟動需要掛載根文件系統,但根文件系統驅動(如 SCSI、SATA、NVMe、文件系統驅動)又在根文件系統上。Linux 通過多種機制打破這個循環依賴。

四大掛載模式

模式一:直接掛載 (內建驅動)


# 內核命令行
root=/dev/sda2 rootfstype=ext4
  • 前提:塊設備驅動、文件系統驅動**編譯進內核** (=y),非模組 (=m)
  • 流程:內核解析 root= → 根據主/次設備號查找驅動 → 直接掛載
  • 設備名對應表:init/do_mounts.c 中 root_dev_names[] 定義 (hd, sd, vd, nvme, mmc, mtd, nfs, ram...)
  • 優點:啟動最快、無額外組件
  • 缺點:內核體積大、硬體變更需重編內核

模式二:RAMDISK / 軟驅啟動 (歷史遺留)


# 內核命令行
root=/dev/fd0  # 軟驅
root=/dev/ram0 # 內存盤
  • 機制:Bootloader 將根文件系統映像載入記憶體 → 內核識別為 /dev/ram0 → 掛載
  • RAMDISK 支援:CONFIG_BLK_DEV_RAM=y,預設 16 個 (/dev/ram0-15)
  • 現狀:現代系統極少使用,嵌入式無磁碟設備偶見

模式三:initrd (傳統初始 RAM 磁碟)


# Bootloader 傳遞參數
initrd=0x80000000,0x200000  # 起始地址、大小
# 或編譯進內核
CONFIG_BLK_DEV_INITRD=y
CONFIG_INITRAMFS_SOURCE=""
  • 本質:一個**壓縮的文件系統映像** (ext2/minix/gzip)
  • 流程:
    1. Bootloader 載入內核 + initrd 至記憶體
    2. 內核啟動,檢測 initrd → 分配 /dev/ram0
    3. 將 initrd 內容解壓至 /dev/ram0
    4. 掛載 /dev/ram0 為根文件系統
    5. 執行 /linuxrc 腳本:載入模組、掃描硬體、掛載真實根分區
    6. pivot_root 切換根文件系統
  • 關鍵點:需要 CONFIG_BLK_DEV_RAM + CONFIG_BLK_DEV_INITRD 同時啟用

模式四:initramfs (現代標準,內核 2.5+)

  • 本質:內嵌於內核映像的 **cpio 歸檔** (可選壓縮 gzip/xz/lz4/lzma)
  • 優勢:
    • 無固定大小限制,動態使用頁面緩存
    • 無需 RAM 磁碟驅動,直接掛載為 rootfs (tmpfs/ramfs)
    • 支援微型用戶空間 (busybox、systemd、kexec 工具)
    • 可通過 CONFIG_INITRAMFS_SOURCE 指定源目錄自動打包
  • 流程:
    1. populate_rootfs() 解壓 cpio 至 rootfs
    2. 若存在 /init → 執行之 (systemd/busybox init)
    3. 否則 → prepare_namespace() 掛載真實根分區

嵌入式系統典型實現

架構Bootloader內核參數根文件系統備註
Flash 直啟U-Bootroot=/dev/mtdblock2 rootfstype=jffs2JFFS2/UBIFS無 initramfs,驅動內建
SD/eMMC 啟動U-Boot/SPLroot=/dev/mmcblk0p2 rootwaitext4/f2fsrootwait 等待設備就緒
NFS 根文件系統U-Bootroot=/dev/nfs nfsroot=192.168.1.100:/nfsroot ip=dhcpNFSv3/v4無磁碟工作站、開發調試
initramfs + 切換U-Bootroot=/dev/mmcblk0p2initramfs → ext4模組化驅動、加密磁碟、RAID

關鍵啟動參數對照表


# 基本參數
root=              # 根設備 (設備節點、UUID、LABEL、PARTUUID)
rootfstype=        # 文件系統類型 (ext4, xfs, btrfs, jffs2, ubifs, nfs...)
rootflags=         # 掛載選項 (ro, rw, noatime, compress=zstd...)
rootdelay=         # 等待設備就緒秒數 (預設 0,嵌入式常設 5-10)
rootwait           # 無限等待 (塊設備熱插拔、MMC、USB 儲存)

# initramfs 相關
rd.init=           # 指定 init 程式路徑
rd.shell           # 失敗時進入 shell
rd.break=        # 在特定階段中斷 (cmdline, pre-udev, pre-trigger, initqueue, pre-mount, mount, pre-pivot, cleanup)
rd.debug           # 開啟調試輸出

# NFS 根文件系統
nfsroot=           # 格式: [server:]path[,options]
ip=                # 格式: client-ip:server-ip:gateway:netmask:hostname:device:autoconf

從 initrd 遷移到 initramfs 檢查清單

  • [ ] 內核啟用 CONFIG_BLK_DEV_INITRD=y、CONFIG_INITRAMFS_SOURCE=""
  • [ ] 移除 CONFIG_BLK_DEV_RAM 依賴 (initramfs 不需要)
  • [ ] 將 /linuxrc 遷移為 /init (systemd/busybox)
  • [ ] 使用 dracut/mkinitcpio/update-initramfs 生成 cpio 歸檔
  • [ ] 驗證 lsinitramfs /boot/initramfs-$(uname -r).img | head 內容正確
  • [ ] 測試啟動、模組載入、根分區掛載、切換流程
Copyright © Windows教程網 All Rights Reserved