Inside init/main.c:kernel_init():

854
855 static int __init kernel_init(void * unused)
856 {
857         lock_kernel();
858
859         /*
860          * init can allocate pages on any node
861          */
862         set_mems_allowed(node_possible_map);
863         /*
864          * init can run on any cpu.
865          */
866         set_cpus_allowed_ptr(current, cpu_all_mask);
867         /*
868          * Tell the world that we're going to be the grim
869          * reaper of innocent orphaned children.
870          *
871          * We don't want people to have to make incorrect
872          * assumptions about where in the task array this
873          * can be found.
874          */
875         init_pid_ns.child_reaper = current;
876
877         cad_pid = task_pid(current);
878
879         smp_prepare_cpus(setup_max_cpus);
880
881         do_pre_smp_initcalls();
882         start_boot_trace();
883
884         smp_init();
885         sched_init_smp();
886
887         do_basic_setup();
888

901
902         /*
903          * Ok, we have completed the initial bootup, and
904          * we're essentially up and running. Get rid of the
905          * initmem segments and start the user-mode stuff..
906          */
907
908         init_post();
909         return 0;
910 }


here do_basic_setup() (above) is executed:


775 /*
776  * Ok, the machine is now initialized. None of the devices
777  * have been touched yet, but the CPU subsystem is up and
778  * running, and memory and process management works.
779  *
780  * Now we can finally start doing some real work..
781  */
782 static void __init do_basic_setup(void)
783 {
784         rcu_init_sched(); /* needed by module_init stage. */
785         init_workqueues();
786         cpuset_init_smp();
787         usermodehelper_init();
788         driver_init();
789         init_irq_proc();
790         do_ctors();
791         do_initcalls();
792 }
793

>From above, we can see do_initcalls() is executed:

764 static void __init do_initcalls(void)
765 {
766         initcall_t *call;
767
768         for (call = __early_initcall_end; call < __initcall_end; call++)
769                 do_one_initcall(*call);
770
771         /* Make sure there is no pending stuff from the initcall sequence */
772         flush_scheduled_work();
773 }

and among the initcalls to be made is populate_rootfs():

567
568 static int __init populate_rootfs(void)
569 {
570         char *err = unpack_to_rootfs(__initramfs_start,
571                          __initramfs_end - __initramfs_start);
572         if (err)
573                 panic(err);     /* Failed to decompress INTERNAL initramfs */
574         if (initrd_start) {
575 #ifdef CONFIG_BLK_DEV_RAM
576                 int fd;
577                 printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n");
578                 err = unpack_to_rootfs((char *)initrd_start,
579                         initrd_end - initrd_start);
580                 if (!err) {
581                         free_initrd();
582                         return 0;
583                 } else {
584                         clean_rootfs();
585                         unpack_to_rootfs(__initramfs_start,
586                                  __initramfs_end - __initramfs_start);
587                 }
588                 printk(KERN_INFO "rootfs image is not initramfs (%s)"
589                                 "; looks like an initrd\n", err);
590                 fd = sys_open("/initrd.image", O_WRONLY|O_CREAT, 0700);
591                 if (fd >= 0) {
592                         sys_write(fd, (char *)initrd_start,
593                                         initrd_end - initrd_start);
594                         sys_close(fd);
595                         free_initrd();
596                 }
597 #else
598                 printk(KERN_INFO "Unpacking initramfs...\n");
599                 err = unpack_to_rootfs((char *)initrd_start,
600                         initrd_end - initrd_start);
601                 if (err)
602                         printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err);
603                 free_initrd();
604 #endif
605         }
606         return 0;
607 }
608 rootfs_initcall(populate_rootfs);

Here the rootfs is setup, and torn down later (except retain_initrd being specified).

And note that after all the do_basic_setup() is the init_post():

901
902         /*
903          * Ok, we have completed the initial bootup, and
904          * we're essentially up and running. Get rid of the
905          * initmem segments and start the user-mode stuff..
906          */
907
908         init_post();
909         return 0;
910 }

where all the userspace scripts are executed:

836         /*
837          * We try each of these until one succeeds.
838          *
839          * The Bourne shell can be used instead of init if we are
840          * trying to recover a really broken machine.
841          */
842         if (execute_command) {
843                 run_init_process(execute_command);
844                 printk(KERN_WARNING "Failed to execute %s.  Attempting "
845                                         "defaults...\n", execute_command);
846         }
847         run_init_process("/sbin/init");
848         run_init_process("/etc/init");
849         run_init_process("/bin/init");
850         run_init_process("/bin/sh");
851
852         panic("No init found.  Try passing init= option to kernel.");
853 }


Who call kernel_init()?


Arch-specific start_kernel()=====>rest_init()====>kernel_init().

667 #endif
668         thread_info_cache_init();
669         cred_init();
670         fork_init(num_physpages);
671         proc_caches_init();
672         buffer_init();
673         key_init();
674         security_init();
675         vfs_caches_init(num_physpages);
676         radix_tree_init();
677         signals_init();
678         /* rootfs populating might need page-writeback */
679         page_writeback_init();
680 #ifdef CONFIG_PROC_FS
681         proc_root_init();
682 #endif
683         cgroup_init();
684         cpuset_init();
685         taskstats_init_early();
686         delayacct_init();
687
688         check_bugs();
689
690         acpi_early_init(); /* before LAPIC and SMP init */
691
692         ftrace_init();
693
694         /* Do the rest non-__init'ed, we're now alive */
695         rest_init();
696 }
697

and kernel thread start kernel_init():

425 static noinline void __init_refok rest_init(void)
426         __releases(kernel_lock)
427 {
428         int pid;
429
430         rcu_scheduler_starting();
431         kernel_thread(kernel_init, NULL, CLONE_FS | CLONE_SIGHAND);
432         numa_default_policy();
433         pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);
434         kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);
435         unlock_kernel();
436


Reply via email to