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

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s