Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

If I build linux kernel from source, does it contain initramfs inside by default?

I thought I have to use busybox to populate init filesystem and use cpio to make initramfs image and add it to the linux kernel source tree (like in arch/arm64/boot ?) before I build the kernel. But if I skip the process and just build the kernel, does it contain a kind of default initramfs? or does it have empty initramfs? (many examples on the web doesn't show busybox procedures.. )

question from:https://stackoverflow.com/questions/65868294/if-i-build-linux-kernel-from-source-does-it-contain-initramfs-inside-by-default

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If I build linux kernel from source, does it contain initramfs inside by default?

Yes, but it will be empty (by default).
As stated in 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).

You can populate the initramfs with either an internal or external cpio archive.
Use CONFIG_INITRAMFS_SOURCE to point to a cpio archive file, which will be embedded into the kernel image.
Otherwise an (external) cpio archive file can be specified during boot by using the old initrd mechanism.
If an initrd/initramfs is passed to the arm64 kernel at boot, it must reside entirely within a 1 GB aligned physical memory window of up to 32 GB in size that fully covers the kernel Image as well.


The cpio archive needs to contain an init file in order to inhibit the further processing for mounting a root filesystem. As documented:

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.

Busybox can be built to provide that init file.


ADDENDUM

I had found CONFIG_INITRAMFS_SOURCE value in my old .config file (year 2013~2014) was set to just empty (set to " ").

A string of a single space does not make sense.
Did you mean an empty string, as in "" which is the default value? The default value means that no archive for the initramfs is to be built with the kernel image.

Now I just now found CONFIG_RD_GZIP=y too in the file.

That simply enables support for loading of a gzip-encoded initial ramdisk or cpio buffer. It is just one of several compression methods available that can be configured. There could also be support for bzip2, LZMA, XZ, LZO, and LZ4.

The salient configuration symbol would be CONFIG_BLK_DEV_RAM, which would enable RAM disk support.

At that time we put busybox120.gz file under arch/sparc/boot before doing linux 'make sImage'. so I guess when there is a .gz file under arch/sparc/boot, the kernel build system uses that .gz file as the initramfs.

No, simply storing a file in a directory is not going to cause inclusion in the kernel build.
Since no initramfs file is specified, the likeliest use of that busybox120.gz file would be for a ramdisk.
Archives are typically named with .cpio or .tar filename extensions. Your file has neither, so that could mean that it is not an archive. The absence of an archive extension could mean that it is an image file, which would never be used for a ramfs but could be used for a ramdisk (or initrd).
AFAIK an image (or archive) for the initrd is loaded by the boot program (ref: Using the initial RAM disk (initrd)) rather than linked in.

Check the kernel command line used with that kernel image.
Are there parameters that specify a ramdisk, e.g. initrd=... and/or root=/dev/ram0?

Be sure that you do not conflate initramfs and initrd. See Documentation/filesystems/ramfs-rootfs-initramfs.txt and The difference between initrd and initramfs?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...