Boot Process

tags: Linux Kernel Module Linux Kernel
  1. BIOS
    執行 POST (Power of self test)
    再從 MBR 載入 bootloader,也會檢查後面兩個 byte 是否為 0x55AA,否則為無效的 MBR

  2. bootloader(MBR)
    接下來 bootloader 就要 load 作業系統拉~要取哪裡找作業系統的檔案呢?

    就在 /boot 目錄中,裡面有個壓縮檔是 vmlinuz (vmlinx是解壓縮過後),解壓縮過後就是 linux kernel,也有 initrd.img。並將它們放到記憶體。

    執行 linux kernel 後掛載 initrd.img ,才能讓 kernel 掛載所需要的驅動程式,之後才能夠掛載真正的 root filesystem。

    在 linux 2.6版後,不再使用 ram disk(效率不高,因為依然需要像硬碟一樣對待,讀寫還是要用到內核的緩存,記憶體有兩份一樣的數據),而是 tempfs (只使用內核的緩存) ,雖然檔名還是叫做 initrd.img,但實現方式是 initramfs 。

    kernel 掛載了臨時的檔案系統後,在initrd.img 中的 init process 接下來就會去掛載真正的檔案系統,然後再執行真正的檔案系統裡的 /sbin/init。

    可以用去 /boot 找到 initrd.img,使用以下命令解壓縮
    unmkinitramfs /boot/initrd.img-$(uname -r) initramfs/

    底下有三個資料夾。在 main 裡面就是小型的檔案系統。

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    此外initramfs并没有包含各种庞多的驱动文件,而是仅带入开机过程会用到的核心模块而已,例如SCSI、cirto、RAID等和磁盘相关性高的模块[1]

  3. 執行第一支程式 systemd
    執行/sbin/init,但他其實會 link 到 systemd

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    所有的動作都會透過 systemd 的預設啟動服務集合,即是 default.target 來規劃。(/usr/lib/systemd/system/default.target/usr/lib/systemd/user/default.target;前面是 Graphical Interface、後面是 Main User Target ) (鳥哥: /etc/systemd/system/default.target )

    default.target 主要項目有 multi-user.target 以及 graphical.target 。會去取得 /usr/lib/systemd/system/ 底下的上面兩個 target 設定檔。

    舉例: graphical,於是就有

    • /etc/systemd/system/graphical.target.wants/:使用者設定載入的 unit
    • /usr/lib/systemd/system/graphical.target.wants/:系統預設載入的 unit

    若要列出系統服務的啟動流程,以下指令:
    systemctl list-dependencies [file name].target

    總之,根據我的理解,當啟動 systemd ,就會根據 default.target 設定檔和 其他設定檔執行各種 service,當該啟動的啟動完後就可以提供給使用者服務拉!
    (鳥哥: systemd 最主要的功能就是準備軟體執行的環境,包括系統的主機名稱、網路設定、語系處理、檔案系統格式及其他服務的啟動等。 而所有的動作都會透過 systemd 的預設啟動服務集合,亦即是 /etc/systemd/system/default.target 來規劃)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

參考資料:
[1] Linux文件系统映像:Initranfs 和 Initrd
[2] 第十九章、開機流程、模組管理與 Loader