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:

  1. systemd switches to the shutdown.target
  2. systemd starts /lib/systemd/system/shutdown.target.wants/dracut-shutdown.service
  3. dracut-shutdown.service executes /usr/lib/dracut/dracut-initramfs-restore which unpacks the initramfs to /run/initramfs
  4. systemd finishes shutdown.target
  5. systemd kills all processes
  6. systemd tries to unmount everything and mounts the remaining read-only
  7. systemd checks, if there is a /run/initramfs/shutdown executable
  8. 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.
  9. 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

专题:initramfs & dracut的更多相关文章

  1. openstack、kvm CentOS升级内核

    openstack平台需要使用各种Linux发行版镜像,其制作方法主要有两种,要么是基于各大Linux发行版ISO光盘手动制作,要么是使用官方提供的制作好镜像进行修改 问题 FATAL: Module ...

  2. centos 安装 谷歌BBR

    使用root用户登录,运行以下命令: wget --no-check-certificate https://github.com/teddysun/across/raw/master/bbr.sh  ...

  3. Linux 一键安装最新内核并开启 BBR 脚本

    原文链接   https://teddysun.com/489.html 请到原文链接仔细阅读后操作.建议查看过脚本内容后操作,方便理解运行过程. 使用root用户登录,运行以下命令: wget -- ...

  4. Centos7从3.10升级内核到4.9后无法启动解决办法:mpt[23]sas驱动问题

    Centos7升级内核后无法启动解决办法:mpt[23]sas驱动问题 前言 这个问题存在有一段时间了,之前做的centos7的ISO,在进行内核的升级以后就存在这个问题: 系统盘在板载sata口上是 ...

  5. 编译制作Linux 3.18内核rpm包(升级centos6.x虚拟机内核)

    介绍 openstack平台需要使用各种Linux发行版模板镜像,其制作方法主要有两种,要么是基于各大Linux发行版ISO光盘手动制作,要么是使用官方提供的模板镜像再做修改 之前制作的opensta ...

  6. Linux(CentOS)下安装NVIDIA GPU驱动

    GCC 官网:http://gcc.gnu.org 1)检查 NVIDIA驱动需要GCC等C/C++开发环境,首先检测GCC是否已经安装 gcc –v# 如果系统显示没有找到GCC指令,或没有显示GC ...

  7. gnome/KDE安装,gnome出现问题,重新安装nvdia驱动, Linux(CentOS7) NVIDIA GeForece GTX 745 显卡驱动

    新安装显示gtx745驱动NVIDIA-Linux-x86_64-346.59.run, yum groupremove kde-desktop yum groupinstall "Desk ...

  8. 13.1 CentOS系统启动流程介绍

    CentOS系统启动流程介绍 本章我们将学习 Linux 启动流程和内核模块管理相关的内容.通过本章我们将学习如下内容: Linux 系统的组成及特点 CentOS 系统的启动流程 开机启动成 gru ...

  9. Centos7升级内核后无法启动解决办法

    前言 这个问题存在有一段时间了,之前做的centos7的ISO,在进行内核的升级以后就存在这个问题: 系统盘在板载sata口上是可以正常启动新内核并且能识别面板硬盘 系统盘插在面板口上新内核无法启动, ...

随机推荐

  1. WebService---Android中访问WebService接口的方法

     最近公司有个项目需要从Android平台访问WebService接口,实现向发布的函数传递对象.在网上找了一些资料,发现使用ksoap2可以调用WebService传递对象.   需要引入ksoap ...

  2. RelativeLayout_布局

    RelativeLayout布局 android:layout_marginTop="25dip" //顶部距离 android:gravity="left" ...

  3. SVN服务器详细权限控制

    版权声明:本文为博主原创文章,未经博主允许不得转载. 下面是我配置SVN服务器的过程,现在把我所配置的方法,记录下来,以供其他有需要的朋友参考,需要改进的地方,请指教! 一 环境 操作系统:windo ...

  4. [HTML] CSS3 边框

    CSS3 边框 用CSS3,你可以创建圆角边框,添加阴影框,并作为边界的形象而不使用设计程序,如Photoshop. 在本章中,您将了解以下的边框属性: border-radius box-shado ...

  5. intellJ实用技巧

    常用语句 在IntelJ中和Eclipse中稍有不同,在Eclipse中,输入main再按Alt+/即可自动补全main函数,但是在IntellJ中则是输入psvm. 在方法体内部有for循环,在In ...

  6. (转载)RESTORE DATABASE命令还原SQLServer 2005 数据库

    今天恢复一个SQLServer2008R2,发现问题,然后通过园友的文章解决了问题,特记录备用 原文地址:http://www.cnblogs.com/adandelion/archive/2006/ ...

  7. jmeter的逻辑控制器

    这篇是在网上找的,写的实在是比我写的具体得多,也没什么好补充的,拿来记录一下,方便以后查询,感激原作者!! JMeter中的Logic Controller分为两类:一类用来控制Test Plan执行 ...

  8. ORCLE数据库导出导入

    从一个用户导出用户所有结构数据 再导入另一个用户里面 ORACLE导出用户下的数据库  exp 命令 用户名/密码服务名 文件地址  owner=(用户名)exp COM_HIOSC_OLD/COM_ ...

  9. window.parent

    今天出现了一个严重的bug,页面在载入时居然导致了页面崩溃,找了近5个小时的代码,才找到了原因(擦,我只能说我水平有限,浪费了太长时间了~). 源代码: var curr = window; whil ...

  10. Hibernate day02笔记

      对象状态与一级缓存 状态介绍 hibernate 规定三种状态:瞬时态.持久态.脱管态 状态     瞬时态:transient,session没有缓存对象,数据库也没有对应记录.         ...