Linux启动过程概述
这里先简单列一下 Linux 操作系统启动的全过程:
- 按下电脑的电源键后,电脑通电,BIOS启动;
- BIOS读取硬盘的MBR,运行启动扇区中的代码,旧系统往往需要自己写启动扇区,而新系统基本上由专用的启动软件接管了,在 Linux 世界中,目前都是用的 Grub2。由于启动扇区空间太小,放不下太复杂的代码逻辑,所以 Grub2 也使用了多阶段启动的策略;
- Grub2 负责将操作系统内核加载到内存,如果有必要,也会把 initramfs 文件加载到内存,然后将控制权交给内核;
- 内核进行初始化,内核的初始化过程结束后,就会把控制权交给
/init
程序,从此进入用户空间; - 因为内核先是将 initramfs 文件挂在为根文件系统,所以刚开始运行的
/init
程序其实是 initramfs 文件中的,所以该文件需要的重要的初始化脚本、内核模块、配置文件等,都位于 initramfs 文件中,这也是为什么很多时候我们修改了某些配置文件后,需要先更新 initramfs 文件再重启操作系统才会生效; - initramfs 文件中的
/init
程序负责挂载硬盘上的文件系统,然后再把根文件系统切换到硬盘上的根分区,再运行/sbin/init
程序,这时所有程序、配置文件、脚本都是使用的硬盘上的了,当然,网络文件系统也是同理。可以看出 init 程序的运行也是一个分阶段的过程; /sbin/init
程序负责系统的初始化、各种服务的运行、用户的登陆等等;- 如果需要运行图形界面,则
/sbin/init
程序会运行 Display Manager,在 Fedora 中是 gdm,在 Ubuntu 中是 lightdm。然后 Display Manager 负责启动整个图形界面。
initramfs概述
Initramfs/Initrd 用于创建一个系统启动初期的小型用户空间,解决复杂启动流程问题,作为真实rootfs挂载前的过渡。
创建一个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的启动参数实现
Whеn sоme one searches for his essential thing, thus he/she wishes to be
available that in detail, thus thаt thіng is maintaіned over here.
赞赞
hope it can help you
赞赞
Have you ever wanted to work from home or just top up your earnings ? This tried and tested system teaches you how you can make a small fortune from the comfort of your own home. http://bit.ly/-Work-from-Home
赞赞