How does Linux kernel know to load init?

The kernel is hard coded to load init. From Kernel source init/main.c:

if (!try_to_run_init_process("/sbin/init") ||
        !try_to_run_init_process("/etc/init") ||
        !try_to_run_init_process("/bin/init") ||
        !try_to_run_init_process("/bin/sh"))
        return 0;

If you have a Kernel source locally:

root [ /sources/linux-4.7.2 ]# find ./ -name main.c | xargs -IA grep -H run_init_process A
./init/main.c:static int run_init_process(const char *init_filename)
./init/main.c:static int try_to_run_init_process(const char *init_filename)
./init/main.c:  ret = run_init_process(init_filename);
./init/main.c:      ret = run_init_process(ramdisk_execute_command);
./init/main.c:      ret = run_init_process(execute_command);
./init/main.c:  if (!try_to_run_init_process("/sbin/init") ||
./init/main.c:      !try_to_run_init_process("/etc/init") ||
./init/main.c:      !try_to_run_init_process("/bin/init") ||
./init/main.c:      !try_to_run_init_process("/bin/sh"))

If systemd is used, init is symlinked to systemd.

[root@localhost ~]# ll /sbin/init 
lrwxrwxrwx. 1 root root 22 Dec 21 03:54 /sbin/init -> ../lib/systemd/systemd
Advertisements

Notes from Linux Kernel Fundamentals course

I took the Linux Kernel fundamentals course on Linkedin Learning during the Week of Learning. I went through the first three lessons. The last two deals with compiling the kernel. I skipped them as I had done some kernel compiling years ago and I won’t be doing them anytime soon.

These are my notes from the lessons:

I have always thought Virtual Filesystems like /proc and /sys are stored in memory, they are not. They are generated when asked for it. Each file and directory has associated function in the kernel that generates the content when you ask.
Continue reading