I'm trying to understand how the linux kernel knows where the desired rootfs is on boot.
I read this document:
One part of interest says:
All 2.6 Linux kernels contain a gzipped "cpio" format archive, which is extracted into rootfs when the kernel boots up ... 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
Our kernel is 4.X, but I'm guessing this still applies? This sounds like all kernels have an embedded "cpio" rootfs.
And indeed as we read on it says:
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 ... The config option CONFIG_INITRAMFS_SOURCE ... can be used to specify a source for the initramfs archive
This raises a few more questions:
- So if I want my rootfs to be in RAM, I need to set
CONFIG_INITRAMFS_SOURCEto point to my rootfs (in cpio format presumably).
But won't that mean my kernel and rootfs are now inseparable? What if I want to make a small tweak to the RootFS without rebuilding? What if I want my rootfs stored separate from the kernel? How do I tell the kernel the location of my rootfs?
- Furthermore, what if I want my rootfs to be on physical storage (like eMMC, flash drive, etc.) and not in RAM?
It said earlier that:
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
But... how? How does it know where to locate the rootfs? If it's on eMMC I need to tell the kernel that somehow, right?
The bootloader I am using is U-boot. I checked U-boot environment variables to see if it was somehow passing the rootfs location to the kernel as a boot arg, but it doesn't seem to be the case...
As pointed out in the comments, the location of the rootfs is passed to the kernel via boot arg. In my case, the u-boot is passing
root=/dev/mmcblk0p4 rw as a boot arg to the kernel. So that answers one of my questions - you can pass the location to any decompressed rootfs as a boot arg.
I'm still not clear how, given some
rootfs.tar.gz that is seperate from the kernel how to tell the kernel to untar that into RAM and use it as the rootfs. Maybe that's not possible and I just need to use
CONFIG_INITRAMFS_SOURCE? At any rate, I'll read up on the 4.X docs.