openwrt (三)啟動

1. 啟動1號進程

1號進程的創(chuàng)建

kernel的啟動開始于start_kernel()函數(shù),在這里完成各種系統(tǒng)初始化后最后進入rest_init函數(shù),這整個過程都可以稱之為0進程:

    asmlinkage void __init start_kernel(void)
    {
        ...
            boot_cpu_init();
            page_address_init();
            pr_notice("%s", linux_banner);
            setup_arch(&command_line);
            mm_init_owner(&init_mm, &init_task);
            mm_init_cpumask(&init_mm);
            setup_command_line(command_line);
            setup_nr_cpu_ids();
            setup_per_cpu_areas();
            smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */

            build_all_zonelists(NULL, NULL);
            page_alloc_init();

            pr_notice("Kernel command line: %s\n", boot_command_line);
            parse_early_param();
        ...
            vfs_caches_init_early();
            sort_main_extable();
            trap_init();
            mm_init();
        ...
        
            /* Do the rest non-__init'ed, we're now alive */
            rest_init();

    }

rest_init()創(chuàng)建了兩個內(nèi)核線程kernel_init和kthreadd:

    static noinline void __init_refok rest_init(void)
    {
            int pid;

            rcu_scheduler_starting();
            /*
             * We need to spawn init first so that it obtains pid 1, however
             * the init task will end up wanting to create kthreads, which, if
             * we schedule it before we create kthreadd, will OOPS.
             */
            kernel_thread(kernel_init, NULL, CLONE_FS | CLONE_SIGHAND);
            numa_default_policy();
            pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);
            rcu_read_lock();
            kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);
            rcu_read_unlock();
            complete(&kthreadd_done);

            /*
             * The boot idle thread must execute schedule()
             * at least once to get things moving:
             */
            init_idle_bootup_task(current);
            schedule_preempt_disabled();
            /* Call into cpu_idle with preempt disabled */
            cpu_startup_entry(CPUHP_ONLINE);
    }

top命令可以看到kthreadd的pid為2

top.png

kernel_init后面會嘗試裝載init進程,并完成內(nèi)核態(tài)到用戶態(tài)的轉(zhuǎn)換,形成用戶態(tài)的祖先1號進程。

    static int __ref kernel_init(void *unused)
    {
        ...
            if (execute_command) {
                    if (!run_init_process(execute_command))
                            return 0;
                    pr_err("Failed to execute %s.  Attempting defaults...\n",
                            execute_command);
            }
            if (!run_init_process("/etc/preinit") ||
                !run_init_process("/sbin/init") ||
                !run_init_process("/etc/init") ||
                !run_init_process("/bin/init") ||
                !run_init_process("/bin/sh"))
                    return 0;

            panic("No init found.  Try passing init= option to kernel. "
                  "See Linux Documentation/init.txt for guidance.");
    }

execute_command為cmdline傳遞進來的init進程,不存在或運行失敗后依次嘗試運行:

    "/etc/preinit",     "/sbin/init",       "/etc/init",        "/bin/init",        "/bin/sh"
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容