Initramfs
An initramfs (initial ram file system) is used by Linux systems to prepare the system during boot before the operating systems'init process starts.
The initramfs usually takes care of mounting important file systems (by loading the proper kernel modules and drivers) such as/usr or /var, preparing the /dev file structure, etc. Users who use an encrypted file system will also have the initramfs ask them for the passphrase before it can mount the file systems.
When the file systems are mounted, control is passed on to init which then takes care of further starting all necessary services and booting up the remainder of the system.
『參考資料』https://www.kernel.org/pub/linux/utils/boot/dracut/dracut.html#dracut.kernel
Dracut
Dracut is an initramfs infrastructure and aims to have as little as possible hard-coded into the initramfs.
Dracut can generate a customized initrams image which contains only whatever is necessary to boot some particular computer, such as ATA, SCSI and filesystem kernel modules (host-only mode).
Dracut can also generate a more generic initramfs image (default mode).
一、概述
Initramfs/Initrd 用於創建一個系統啟動初期的小型用戶空間,解決復雜啟動流程問題,作為真實rootfs掛載前的過渡。Dracut完整的命令行參數,請查看dracut.cmdline(7)。
二、使用方法簡介
創建一個initramfs
# dracut [--force] [/PATH/TO/new_image_name] [kernel version]
這將創建一個通用的initramfs image,包含盡可能多的當前active kernel的驅動模塊和系統工具,image默認位置:/boot/initramfs-<kernel version>.img;--force選項用於強行覆蓋已經存在的同名initramfs;可以在cmdline指名新image的路徑和名稱及為哪一個版本的kernel制作image;如果想制作一個更輕、更小、啟動速度更快的initramfs,可以添加--hostonly選項,通常需要制作一個全功能通用型image以備不時之需。
-
查看initramfs包含的文件列表及某個文件的內容
# lsinitrd | less
# lsinitrd -f /etc/ld.so.conf
include ld.so.conf.d/*.conf 注:ld.so.conf文件的全部內容
- 添加模塊
一些dracut內置模塊默認不會添加至initramfs中,可以在/etc/dracut.conf或/etc/dracut.conf.d/xxx.xonf中添加,也可以使用--add選項
# dracut --add bootchart initramfs-bootchart.img
查看所有可用的dracut模塊
# dracut --list-modules
- 忽略(不添加)模塊
用於精簡initramfs,同樣可以在配置文件(man 5 dracut.conf)或命令行操作,如下示例:去除lvm邏輯卷支持
# dracut --omit "multipath lvm" no-multipath-lvm.img
- 手動添加沒有被dracut自動識別的kernel模塊
同理,/etc/dracut.conf或/etc/dracut.conf.d/xxx.conf或cmdline
# dracut --add-drivers mymod initramfs-with-mymod.img
- kernel啟動參數(/boot/grub[2]/grub.cfg)
查看當前系統啟動參數
# dracut --print-cmdline root=UUID=8b8b6f91-95c7-4da2-831b-171e12179081 rootflags=rw,relatime,discard,data=ordered rootfstype=ext4
指定啟動設備
root=/dev/sdX
- OR -
root=UUID=19e9dda3-5a38-484d-a9b0-fa6b067d0331
- OR -
root=LABEL=XXX
查看磁盤id及label
blkid
- OR -
ls -l /dev/disk/by-uuid && ls -l /dev/disk/by-label
屏蔽kernel模塊(即:使其不能自動加載)
rd.driver.blacklist=mptsas rd.driver.blacklist=nouveau
在kernel command line上指明initramfs的啟動參數,可以加快啟動速度;如果不指明,dracut將在最大的范圍內查找
#This turns off every automatic assembly of LVM, MD raids, DM raids and crypto LUKS
rd.luks=0 rd.lvm=0 rd.md=0 rd.dm=0
綜上所述,加快initramfs啟動速的途徑有兩種,一是在生成initramfs image之前直接精簡其內容,二是通地操作grub.cfg的啟動參數實現
三、開機流程:Systemd & Dracut
systemd-journal.socket | v dracut-cmdline.service | v dracut-pre-udev.service | v systemd-udevd.service | v local-fs-pre.target dracut-pre-trigger.service | | v v (various mounts) (various swap systemd-udev-trigger.service | devices...) | (various low-level (various low-level | | | services: seed, API VFS mounts: v v v tmpfiles, random mqueue, configfs, local-fs.target swap.target dracut-initqueue.service sysctl, ...) debugfs, ...) | | | | | \_______________|____________________ | ___________________|____________________/ \|/ v sysinit.target | _________________/|\___________________ / | \ | | | v | v (various | rescue.service sockets...) | | | | v v | rescue.target sockets.target | | | \_________________ | emergency.service \| | v v basic.target emergency.target | ______________________/| / | | v | dracut-pre-mount.service | | | v | sysroot.mount | | | v | initrd-root-fs.target (custom initrd services) | | v | dracut-mount.service | | | v | initrd-parse-etc.service | | | v | (sysroot-usr.mount and | various mounts marked | with fstab option | x-initrd.mount) | | | v | initrd-fs.target \______________________ | \| v initrd.target | v dracut-pre-pivot.service | v initrd-cleanup.service isolates to initrd-switch-root.target | v ______________________/| / | | initrd-udevadm-cleanup-db.service | | (custom initrd services) | | | \______________________ | \| v initrd-switch-root.target | v initrd-switch-root.service | v switch-root
四、關機流程:Systemd & Dracut
On a systemd driven system, the dracut initramfs is also used for the shutdown procedure.The following steps are executed during a shutdown:
- systemd switches to the shutdown.target
- systemd starts /lib/systemd/system/shutdown.target.wants/dracut-shutdown.service
- dracut-shutdown.service executes /usr/lib/dracut/dracut-initramfs-restore which unpacks the initramfs to /run/initramfs
- systemd finishes shutdown.target
- systemd kills all processes
- systemd tries to unmount everything and mounts the remaining read-only
- systemd checks, if there is a /run/initramfs/shutdown executable
- if yes, it does a pivot_root to /run/initramfs and executes ./shutdown. The old root is then mounted on /oldroot. /usr/lib/dracut/modules.d/99shutdown/shutdown.sh is the shutdown executable.
- shutdown will try to umount every /oldroot mount and calls the various shutdown hooks from the dracut modules.This ensures, that all devices are disassembled and unmounted cleanly
