Kernal
在Linux内核获得控制权后,它就会准备其内存结构和驱动程序。然后它将控制权移交给一个应用程序(通常是init),该应用程序的任务是进一步准备系统并确保在引导过程结束时所有必要的服务都在运行并且用户能够登录。
对于所有必需的文件和工具都驻留在同一文件系统上的系统,init应用程序可以完美地控制进一步的引导过程。但是当定义了多个文件系统(或者完成了更多奇特的安装)时,这可能会变得有点棘手:
- 当/usr分区位于单独的文件系统上时,除非/usr可用,否则无法使用将文件存储在/usr 中的工具和驱动程序。如果需要这些工具来使/usr可用,那么我们将无法启动系统。
- 如果根文件系统被加密,那么 Linux 内核将无法找到init应用程序,从而导致系统无法启动。
长期以来,这个问题的解决方案是使用initrd
Vmlinuz
压缩的内核,启动的一些东西,
Vmlinuz is a compressed Linux kernel.
Vmlinuz is built from vmlinux, by “make bzImage”.
initrd
initrd是包含安装所需的文件系统必需的工具和脚本一个内存磁盘结构(虚拟盘)之前控制移交给initrd系统上的应用程序。Linux 内核触发此根磁盘上的安装脚本(通常称为linuxrc但该名称不是必需的),它准备系统,切换到真正的根文件系统,然后调用init。
存在的问题:
It is a full-fledged block device, requiring the overhead of an entire file system; it has a fixed size. Choosing an initrd that is too small and all needed scripts cannot fit. Make it too big and memory will be wasted.Because it is a real, static device it consumes cache memory in the Linux kernel and is prone to the memory and file management methods in use (such as paging), this makes initrd greater in memory consumption.
它被当成块设备,本身就在内存里,但是还需要cache进行加载。浪费内存,并且大小固定
解决方式initramfs
initramfs
An initramfs is an initial ram file system based on tmpfs (a size-flexible, in-memory lightweight file system)
Just like the initrd, it contains the tools and scripts needed to mount the file systems before the init binary on the real root file system is called. These tools can be decryption abstraction layers (for encrypted file systems), logical volume managers, software raid, bluetooth driver based file system loaders, etc.
The content of the initramfs is made by creating a cpio archive. cpio is an old (but proven) file archiver solution (and its resulting archive files are called cpio files). cpio is definitely comparable to the tar archiver. The choice of cpio here was because it was easier to implement (code-wise) and supported device files which tar did not support at the time.
All files, tools, libraries, configuration settings (if applicable), etc. are put into the cpio archive. This archive is then compressed using the gzip utility and stored alongside the Linux kernel. The boot loader will then offer it to the Linux kernel at boot time so the kernel knows an initramfs is needed.
Once detected, the Linux kernel will create a tmpfs file system, extract the contents of the archive on it, and then launch the init script located in the root of the tmpfs file system. This script then mounts the real root file system (after making sure it can mount it, for instance by loading additional modules, preparing an encryption abstraction layer, etc.) as well as vital other file systems (such as /usr and /var ).
Once the root file system and the other vital file systems are mounted, the init script from the initramfs will switch the root towards the real root file system and finally call the /sbin/init binary on that system to continue the boot process.
可以用 dracut 生成。vmlinuxz 和 initramfs 版本必须一直。 initramfs 可以自己build ,可以自定义。
initramfs 其实是在 grub 里面进行加载的,打开grub.cfg可以看到被加载的initramfs
基于tmpfs
开机日志中也可以找到initramfs的身影
[ 1.273611] Trying to unpack rootfs image as initramfs…
[ 1.400517] Freeing initrd memory: 8452K (ffff880036f6e000 - ffff8800377af000)
Initrd for old kernel.
Initramfs for new kernel.
Initramfs is generated by “dracut”.
Initramfs contains earlier root filesystem
genkernel
genkernel —install —no-ramdisk-modules initramfs
dracut
emerge —ask sys-kernel/dracut dracut dracut —host-only —no-kernel
解压命令
Extract vmlinuz/initramfs:
# /usr/src/kernels/$(uname –r)/scripts/extract-vmlinux /boot/vmlinuz-$(uname –r)
# zcat initramfs-$(uname –r).img |cpio –divm
# /usr/lib/dracut/skipcpio initramfs-$(uname –r).img | gunzip –c |cpio –divm
# dracut ./initramfs —no-compress
参考链接
https://wiki.gentoo.org/wiki/Initramfs
https://wiki.gentoo.org/wiki/Initramfs/Guide
https://www.kernel.org/doc/Documentation/filesystems/ramfs-rootfs-initramfs.txt
https://developer.ibm.com/articles/l-initrd/
https://wiki.gentoo.org/wiki/Custom_Initramfs
https://wiki.gentoo.org/wiki/Dracut
Gentoo emergo的一些操作
https://edoceo.com/sys/emerge