The iniramfs is like a small os which mount root file system and hand over control to systemd. So, how does initramfs work internally? Is there any scripts executed to mount the root in linux?
After that initrd/initramfs (only historical difference) is extracted and mounted, it is the script /init that is run. Mostly it only mounts the real root with help of the modules from the initramfs.
And then it has to pivot/switch root to that new partition, a thing that is only possible as pid 1.
Here is a minimal /init script:
mkdir /newroot mount /dev/sda2 /newroot mount --move /sys /newroot mount --move /dev /newroot mount --move /proc /newroot switch_root /newroot /bin/bash
(or /sbin/init for a normal continuation)
The thing is the kernel cannot mount /dev/sda2 without the necessary modules built in. Having the modules on sda2 does not help: egg-and-hen problem.
So the kernel gets a cpio archive with a root filesystem containing modules and scripts. The kernel simply hands over to /init and is done with it's own startup.
That gentoo link starts with:
initramfs is a root filesystem that is embedded into the kernel...
What should that mean?
This is a minimal kernel command line:
telling the boot loader to load kernel "vmlinuz" and telling the kernel to mount /dev/sdaX.
vmlinuz initrd=initramfs.img root=/dev/sdaX
makes the boot loader also load an initrd, which the kernel will find and mount as root, and tells the (default) /init script to pivot/switch to /dev/sdaX (and then run default /sbin/init).