如果我从源代码构建linux内核,默认情况下它内部是否包含initramfs?

Cha*_*Kim 0 build linux-kernel

我想在构建内核之前,我必须使用 busybox 来填充 init 文件系统,并使用 cpio 来制作 initramfs 映像并将其添加到 Linux 内核源代码树中(例如在 arch/arm64/boot 中?)。但是如果我跳过这个过程并只构建内核,它是否包含一种默认的 initramfs ?或者它有空的 initramfs 吗?(网上的很多例子都没有显示busybox程序..)

saw*_*ust 5

如果我从源代码构建linux内核,默认情况下它内部是否包含initramfs?

是的,但它将是空的(默认情况下)。如Documentation/filesystems/ramfs-rootfs-initramfs.txt
中所述:

The 2.6 kernel build process always creates a gzipped cpio format initramfs
archive and links it into the resulting kernel binary.  By default, this
archive is empty (consuming 134 bytes on x86).
Run Code Online (Sandbox Code Playgroud)

您可以使用内部或外部 cpio 存档填充 initramfs。
使用 CONFIG_INITRAMFS_SOURCE 指向 cpio 存档文件,该文件将嵌入到内核映像中。
否则,可以使用旧的 initrd 机制在引导期间指定(外部)cpio 存档文件。
如果 initrd/initramfs 在启动时传递到 arm64 内核,则它必须完全驻留在 1 GB 对齐的物理内存窗口中,大小最多为 32 GB,并且也完全覆盖内核映像。


cpio 存档需要包含一个init文件,以阻止挂载根文件系统的进一步处理。据记录:

After extracting, the kernel checks to see if rootfs contains a file "init",
and if so it executes it as PID 1.  
If found, this init process is responsible for bringing the system the
rest of the way up, including locating and mounting the real root device (if
any).  
If rootfs does not contain an init program after the embedded cpio
archive is extracted into it, the kernel will fall through to the older code
to locate and mount a root partition, then exec some variant of /sbin/init
out of that.
Run Code Online (Sandbox Code Playgroud)

可以构建 Busybox 来提供该初始化文件。


附录

我发现旧的 .config 文件(2013~2014 年)中的 CONFIG_INITRAMFS_SOURCE 值设置为空(设置为“”)。

单个空格的字符串没有意义。
您的意思是空字符串,其中""是默认值吗?默认值意味着不会使用内核映像构建 initramfs 的存档。

现在我刚刚在文件中也找到了 CONFIG_RD_GZIP=y 。

这只是支持加载gzip 编码的初始 ramdisk 或 cpio 缓冲区。它只是可配置的几种可用压缩方法之一。还可能支持 bzip2、LZMA、XZ、LZO 和 LZ4。

显着的配置符号是 CONFIG_BLK_DEV_RAM,它将启用 RAM 磁盘支持。

当时我们在执行 linux 'make sImage' 之前将 busybox120.gz 文件放在 arch/sparc/boot 下。所以我猜当 arch/sparc/boot 下有 .gz 文件时,内核构建系统会使用该 .gz 文件作为 initramfs。

不,简单地将文件存储在目录中不会导致包含在内核构建中。
由于未指定 initramfs 文件,因此该busybox120.gz文件最有可能用于 ramdisk。
存档通常以.cpio.tar文件扩展名命名。您的文件两者都没有,因此这可能意味着它不是存档。缺少存档扩展名可能意味着它是一个映像文件,它永远不会用于 ramfs,但可以用于 ramdisk(或 initrd)。
据我所知,initrd 的映像(或存档)是由引导程序加载的(参考:使用初始 RAM 磁盘 (initrd)),而不是链接进来。

检查与该内核映像一起使用的内核命令行。
是否有指定 ramdisk 的参数,例如initrd=...和/或root=/dev/ram0

确保不要将 initramfs 和 initrd 混为一谈。请参阅Documentation/filesystems/ramfs-rootfs-initramfs.txtinitrd 与 initramfs 之间的区别?