美文网首页
openwrt (三)启动

openwrt (三)启动

作者: onelifeisall | 来源:发表于2018-05-25 18:56 被阅读7次

    1. 启动1号进程

    1号进程的创建

    kernel的启动开始于start_kernel()函数,在这里完成各种系统初始化后最后进入rest_init函数,这整个过程都可以称之为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()创建了两个内核线程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进程,并完成内核态到用户态的转换,形成用户态的祖先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"
    

    相关文章

      网友评论

          本文标题:openwrt (三)启动

          本文链接:https://www.haomeiwen.com/subject/dcvujftx.html