一、概述

内核有3中挂在rootfs的方式:普通的ext/jiffs2/yaffs2, initrd(ramdisk)和initramfs。

Document/early-userspace/README

128 The kernel has currently 3 ways to mount the root filesystem:
129
130 a) all required device and filesystem drivers compiled into the kernel, no
131    initrd.  init/main.c:init() will call prepare_namespace() to mount the
132    final root filesystem, based on the root= option and optional init= to run
133    some other init binary than listed at the end of init/main.c:init().
134
135 b) some device and filesystem drivers built as modules and stored in an
136    initrd.  The initrd must contain a binary '/linuxrc' which is supposed to
137    load these driver modules.  It is also possible to mount the final root
138    filesystem via linuxrc and use the pivot_root syscall.  The initrd is
139    mounted and executed via prepare_namespace().
140
141 c) using initramfs.  The call to prepare_namespace() must be skipped.
142    This means that a binary must do all the work.  Said binary can be stored
143    into initramfs either via modifying usr/gen_init_cpio.c or via the new
144    initrd format, an cpio archive.  It must be called "/init".  This binary
145    is responsible to do all the things prepare_namespace() would do.
146
147    To maintain backwards compatibility, the /init binary will only run if it
148    comes via an initramfs cpio archive.  If this is not the case,
149    init/main.c:init() will run prepare_namespace() to mount the final root
150    and exec one of the predefined init binaries.

 

二、代码分析

linux-2.6.36内核

start       arch/arm/boot/compressed/head.S

              arch/arm/kernel/head.S

start_kernel()    init/main.c

linux通用内核启动代码在init/main.c中:

汇编执行完后跳转到start_kernel()开始C 执行。

...

setup_arch(&command_line);      //把.config中配置的CONFIG_CMDLINE赋予command_line输出

...

setup_command_line(command_line);     //boot_command_line =>save_command_line;

                                                                 //command_line =>static_command_line;

...

printk(KERN_NOTICE ''kernel command line:%s\n", boot_command_line);

...

console_init();

...

rest_init();    //创建两线程kernel_init /kthreadd

    |----------->kernel_thread(kernel_init, NULL, CLONE_FS | CLONE_SIGHAND);

    |----------->kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);            //kernel/kthread.c

                    pid_t kernel_thread(int (*fn)(void *), void *args, unsigned long  flags);  //arch/arm/kernel/process.c

----------------------------------------------------------------------------------------------------------------------------------------

kernel_init()                    //init/main.c

    |---------->do_basic_setup()   //解压了相关文件系统

                   sys_open("/dev/console", O_RDWR, 0) <0

                         printk(KERN_WARNING "Warning: unable to open an initial console.\n");

                   if(! ramdisk_execute_command)

                         ramdisk_execute_command = "/init";             //initramfs 默认启动进程衔接

                   if(sys_access(ramdisk_execute_command, 0) != 0){

                         ramdisk_execute_command = NULL;

                         prepare_namespace();  //处理除ramdisk和initramfs外的其他文件系统。

                    }

    |---------->init_post();

// ramdisk_execute_command <= "rdinit="

// execute_command <= "init="   //bootargs传递

-----------------------------------------------------------------------------------------------------------------

kernel_init()-->do_basic_setup()

mm/CPU已运行,没有设备被创建。

786 /*
787  * Ok, the machine is now initialized. None of the devices
788  * have been touched yet, but the CPU subsystem is up and
789  * running, and memory and process management works.
790  *
791  * Now we can finally start doing some real work..
792  */
793 static void __init do_basic_setup(void)
794 {
795     cpuset_init_smp();
796     usermodehelper_init();
797     init_tmpfs();
798     driver_init();
799     init_irq_proc();
800     do_ctors();
801     do_initcalls();
802 }

773 extern initcall_t __initcall_start[], __initcall_end[], __early_initcall_end    [];
774
775 static void __init do_initcalls(void)
776 {
777     initcall_t *fn;
778
779     for (fn = __early_initcall_end; fn < __initcall_end; fn++)
780         do_one_initcall(*fn);
781
782     /* Make sure there is no pending stuff from the initcall sequence */
783     flush_scheduled_work();
784 }
kernel_init()  -> do_basic_setup() -> do_initcalls() ->rootfs_initcall(populate_rootfs) -> populate_rootfs()

ramdisk或initramfs的处理都是通过populate_rootfs()实现,位于init/initramfs.c中。

-----------------------------------------------------------------------------------------------------------------

init_post()      // init/main.c

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

linux系统启动完成后,任务都交给init进程完成。
---------------------------------------------------------------------------------------------------------

ramdisk和initramfs都是通过populate_rootfs()实现;

jffs,yaffs等通过prepare_namespace()实现,在do_mounts.c中。