Initramfs 原理和实践

Initramfs 原理和实践,第1张

Initramfs 原理和实践

Linux系统启动时使用initramfs (initram file system), initramfs可以在启动早期提供一个用户态环境,借助它可以完成一些内核在启动阶段不易完成的工作。


当然initramfs是可选的,Linux中的内核编译选项默认开启initrd。


在下面的示例情况中你可能要考虑用initramfs。


  • 加载模块,比如第三方driver
  • 定制化启动过程 (比如打印welcome message等)
  • 制作一个非常小的rescue shell
  • 任何kernel不能做的,但在用户态可以做的 (比如执行某些命令)

一个initramfs至少要包含一个文件,文件名为/init。


内核将这个文件执行起来的进程作为main init进程(pid 1)。


当内核挂载initramfs后,文件系统的根分区还没有被mount, 这意味着你不能访问文件系统中的任何文件。


如果你需要一个shell,必须把shell打包到initramfs中,如果你需要一个简单的工具,比如ls, 你也必须把它和它依赖的库或者模块打包到initramfs中。


总之,initramfas是一个完全独立运行的体系。


另外initramfs打包的时候,要求打包成压缩的cpio档案。


cpio档案可以嵌入到内核image中,也可以作为一个独立的文件在启动的过程中被GRUB load。


Linux的initramrd img

在/boot目录下的initrd.img-xxx (Ubuntu)或者initramfs-xxx.img (CentOS) 文件即为Linux用的initramfs文件。


我们可以将其解压出来看看其目录结构,如下:

# ls -l /boot/
total
-rw-r--r-- root root Jul abi-4.4.--generic
-rw-r--r-- root root Aug abi-4.4.--generic
-rw-r--r-- root root Jul config-4.4.--generic
-rw-r--r-- root root Aug config-4.4.--generic
drwxr-xr-x root root Jul : grub
-rw-r--r-- root root Aug initrd.img-4.4.--generic
-rw-r--r-- root root Aug initrd.img-4.4.--generic
-rw------- root root Jul System.map-4.4.--generic
-rw------- root root Aug System.map-4.4.--generic
-rw------- root root Jul vmlinuz-4.4.--generic
-rw------- root root Aug vmlinuz-4.4.--generic
# initrd的文件类型是gzip压缩文件
# file /boot/initrd.img-4.4.--generic
/boot/initrd.img-4.4.--generic: gzip compressed data, from Unix, last modified: Thu Aug :: 2017
# cp /boot/initrd.img-4.4.--generic .
# 文件大小为22M
# ls -lh initrd.img-4.4.--generic
-rw-r--r-- root root 22M Jul : initrd.img-4.4.--generic
# 修改文件的后缀名,否则gzip工具无法识别
# mv initrd.img-4.4.--generic initrd.img-4.4.--generic.gz
# 用gzip解压缩
# gzip -d initrd.img-4.4.--generic.gz
# 解压后的大小为57M
# ls -lh initrd.img-4.4.--generic
-rw-r--r-- root root 57M Jul : initrd.img-4.4.--generic # 解压后的文件类型为cpio档案
# file initrd.img-4.4.--generic
initrd.img-4.4.--generic: ASCII cpio archive (SVR4 with no CRC) # 将文件从cpio档案中copy出来
# cpio -idmv < initrd.img-4.4.--generic
.
lib64
lib64/ld-linux-x86-.so.
...
lib/systemd
lib/systemd/systemd-udevd
blocks # 最终可以看到如下文件和目录结构,就是initramrd的结构
# ls
bin conf etc init initrd.img-4.4.--generic lib lib64 run sbin scripts

可以看到initramfs和跟分区文件系统的雏形很像,只是它的大小不大,少了很多工具和库。


有些内核模块就在其中,比如:/lib/modules/4.4.0-93-generic/kernel/。


qemu中启动"Hello World" initramfs

前文“在qemu环境中用gdb调试Linux内核”中,已经准备了一个Linux启动环境,但是缺少initramfs。


我们可以做一个最简单的Hello World initramfs,来直观地理解initramfs。


Hello World的C程序如下,与普通的Hello World相比,加了一行while(1)。


#include <stdio.h>

void main()
{
printf("Hello World\n");
fflush(stdout);
/* 让程序打印完后继续维持在用户态 */
while(1);
}

编译helloworld.c程序

# gcc -static -o helloworld -m32 helloworld.c
  • -static: On systems that support dynamic linking, this prevents linking with the shared libraries. //不让gcc动态链接shared libraries
  • -m32: Generate code for a 32-bit or 64-bit environment //在前文“在qemu环境中用gdb调试Linux内核”中Linux内核被编译成了32位架构,所以这里在gcc的选项中也编译成32位可执行程序

在64位机器上编译成32位程序,可能会报错如下:

In file included from /usr/include/stdio.h:27:0,
from helloworld.c:2:
/usr/include/features.h:374:25: fatal error: sys/cdefs.h: No such file or directory
# include <sys/cdefs.h>
^
compilation terminated.

解决方案是安装libc6-dev-i386包。


# apt-get install libc6-dev-i386

打包initramfs文件

# echo helloworld | cpio -o --format=newc > hwinitramfs

在qemu中启动编译好的内核,把hwinitramfs指定为initrd,在-append参数中将init指定为helloworld。


# qemu -kernel linux-3.18./arch/x86/boot/bzImage -initrd hwinitramfs -append "console=ttyS0 rdinit=helloworld" -nographic

系统能成功启动到输出"Hello World",并且在用户态停住。


结合前文“在qemu环境中用gdb调试Linux内核”,可以看到qemu虚机中运行的Linux系统已经成功挂载了initramfs, 在console日志中也能看到“Unpacking initramfs...”。


参考

Custom Initramfs

GNU CPIO Manual

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/585817.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-12
下一篇 2022-04-12

发表评论

登录后才能评论

评论列表(0条)

保存