0

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.. )

Chan Kim
  • 5,177
  • 12
  • 57
  • 112
  • *"use cpio to make initramfs image"* -- It's not an *"image"* but an archive. *"...just build the kernel, does it contain a kind of default initramfs?"* -- The *"kind of default initramfs"* will be empty. See https://stackoverflow.com/questions/17885451/how-to-rebuild-rootfs-in-buildroot/17892471, especially the comments to my answer. – sawdust Jan 24 '21 at 08:56
  • I don't use buildroot but I guess my understanding is correct. longtime ago we used busybox to use the initramfs (with cpio). if I just use make defconfig, make for the linux kernel source, what does the file system contain? (I know when I use initramfs, the /bin directory is populated with many busybox commands and I can run /init script to start sh.) – Chan Kim Jan 24 '21 at 11:09

1 Answers1

4

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?

sawdust
  • 16,103
  • 3
  • 40
  • 50
  • Thank you for the write-up. Just one thing, I had found CONFIG_INITRAMFS_SOURCE value in my old .config file (year 2013~2014) was set to just empty (set to " "). Now I just now found CONFIG_RD_GZIP=y too in the file. 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. Can you confirm my guess? – Chan Kim Jan 25 '21 at 05:20
  • That is not plausible. See addendum to answer. – sawdust Jan 25 '21 at 23:38
  • Hi, again, I checked the old command to make busybox120.gz was "find ./ | cpio -H newc -o |gzip -c9 >../busybox120.gz" so it was an archive. and although it was comment out, under arch/sparc/boot directory, by the Makefile, the busybox120.gz is 'gzip -d' ed and undergoes the linker with linker script which places the result busybox120.o in a memory region with start and end marked as initrd_start and initrd_end. But those part was left comment out. Just for information. I could ask my senior who retired years ago :). – Chan Kim Feb 09 '21 at 11:33