Chinaunix首页 | 论坛 | 博客
  • 博客访问: 289825
  • 博文数量: 111
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 672
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-05 23:41
文章分类

全部博文(111)

文章存档

2017年(111)

我的朋友

分类: LINUX

2017-06-23 19:17:23

这一篇在前两篇讲解的基础上来跟踪一下代码

linux-2.6.30

kernel_init
    do_basic_setup();  // 批注1
    
    if (!ramdisk_execute_command) // 批注2
        ramdisk_execute_command = "/init";
    
    if (sys_access((const char __user *) ramdisk_execute_command, 0) != 0) {
        ramdisk_execute_command = NULL;
        prepare_namespace();  // 批注3
    }
    
    init_post();

批注1:所有直接编译在kernel中的模块都是由它启动的。这里有一个地方涉及到根文件系统的挂载
当配置了CONFIG_BLK_DEV_INITRD,在这里会调用函数populate_rootfs;
如果没有配置populate_rootfs,则会调用函数default_rootfs

批注2:ramdisk_execute_command值通过“rdinit=”指定,如果未指定,则采用默认的值/init。

批注3:检查根文件系统中是否存在文件ramdisk_execute_command,如果存在的话则执行init_post(),
否则执行prepare_namespace()挂载根文件系统。
 
先来说说上面批注1可能的两个函数 populate_rootfs和default_rootfs


点击(此处)折叠或打开

  1. static int __init populate_rootfs(void)
  2. {
  3.     char *err = unpack_to_rootfs(__initramfs_start, // 批注1
  4.              __initramfs_end - __initramfs_start);
  5.     if (err)
  6.         panic(err);    /* Failed to decompress INTERNAL initramfs */
  7.     if (initrd_start) { // 批注2
  8. #ifdef CONFIG_BLK_DEV_RAM
  9.         int fd;
  10.         printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n");
  11.         err = unpack_to_rootfs((char *)initrd_start, // 批注3
  12.             initrd_end - initrd_start);
  13.         if (!err) {
  14.             free_initrd();
  15.             return 0;
  16.         } else {
  17.             clean_rootfs();
  18.             unpack_to_rootfs(__initramfs_start,
  19.                  __initramfs_end - __initramfs_start);
  20.         }
  21.         printk(KERN_INFO "rootfs image is not initramfs (%s)"
  22.                 "; looks like an initrd\n", err);
  23.         fd = sys_open("/initrd.image", O_WRONLY|O_CREAT, 0700); // 批注4
  24.         if (fd >= 0) {
  25.             sys_write(fd, (char *)initrd_start, // 批注5
  26.                     initrd_end - initrd_start);
  27.             sys_close(fd);
  28.             free_initrd(); // 批注6
  29.         }
  30. #else
  31.         printk(KERN_INFO "Unpacking initramfs...\n");
  32.         err = unpack_to_rootfs((char *)initrd_start,
  33.             initrd_end - initrd_start);
  34.         if (err)
  35.             printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err);
  36.         free_initrd();
  37. #endif
  38.     }
  39.     return 0;
  40. }
批注1:unpack_to_rootfs顾名思义,就是解压包到rootfs,其具有两个功能,一个是检测
是否是属于cpio包,另外一个就是解压cpio包,通过最后一个参数进行控制。1:检测,0:解压。
其实,Initramfs也是压缩过后的CPIO文件。initramfs位于地址__initramfs_start处,
是内核在编译过程中生成的,initramfs的是作为内核的一部分而存在的,不是 boot loader加载的。
(编译的时候通过连接脚本arch/arm/kernel/vmlinux.lds将其编译到__initramfs_start~__initramfs_end,
执行完unpack_to_rootfs后将被拷贝到根目录)。

批注2:判断是否加载了initrd,无论哪种格式的initrd,都会被bootloader加载到地址initrd_start处。 
 
批注3:判断加载的是不是CPIO-Initrd。

批注4:如果不是CPIO-Initrd,则就是Image-Initrd,将其内容保存到文件/initrd.image中。在根文件系统中创建文件/initrd.image。 
 
批注5:将内容保存到文件/initrd.image中

批注6:释放Initrd所占用的内存空间。
 
default_rootfs()主要往rootfs中生成两个目录/dev和/root以及一个设备文件/dev/console。

下面来继续看下面的函数:prepare_namespace

点击(此处)折叠或打开

  1. void __init prepare_namespace(void)
  2. {
  3.     int is_floppy;

  4.     if (root_delay) { // 批注1
  5.         printk(KERN_INFO "Waiting %dsec before mounting root device...\n",
  6.          root_delay);
  7.         ssleep(root_delay);
  8.     }

  9.     /*
  10.      * wait for the known devices to complete their probing
  11.      *
  12.      * Note: this is a potential source of long boot delays.
  13.      * For example, it is not atypical to wait 5 seconds here
  14.      * for the touchpad of a laptop to initialize.
  15.      */
  16.     wait_for_device_probe(); // 批注2

  17.     md_run_setup();

  18.     if (saved_root_name[0]) { // 批注3
  19.         root_device_name = saved_root_name;
  20.         if (!strncmp(root_device_name, "mtd", 3) ||
  21.          !strncmp(root_device_name, "ubi", 3)) {
  22.             mount_block_root(root_device_name, root_mountflags); // 批注4
  23.             goto out;
  24.         }
  25.         ROOT_DEV = name_to_dev_t(root_device_name); // 批注5
  26.         if (strncmp(root_device_name, "/dev/", 5) == 0)
  27.             root_device_name += 5;
  28.     }

  29.     if (initrd_load()) // 批注6
  30.         goto out;

  31.     /* wait for any asynchronous scanning to complete */
  32.     if ((ROOT_DEV == 0) && root_wait) { // 批注7
  33.         printk(KERN_INFO "Waiting for root device %s...\n",
  34.             saved_root_name);
  35.         while (driver_probe_done() != 0 ||
  36.             (ROOT_DEV = name_to_dev_t(saved_root_name)) == 0)
  37.             msleep(100);
  38.         async_synchronize_full();
  39.     }

  40.     is_floppy = MAJOR(ROOT_DEV) == FLOPPY_MAJOR;

  41.     if (is_floppy && rd_doload && rd_load_disk(0))
  42.         ROOT_DEV = Root_RAM0;

  43.     mount_root();
  44. out:
  45.     sys_mount(".", "/", NULL, MS_MOVE, NULL); // 批注8
  46.     sys_chroot("."); // 批注9
  47. }
批注1:对于将根文件系统存放到USB或者SCSI设备上的情况,Kernel需要等待这些耗费时间比较久的设备
驱动加载完毕,所以这里存在一个Delay。

批注2:等待根文件系统所在的设备探测函数的完成。 
 
批注3:参数saved_root_name存放的是uboot参数root=所指定的设备文件。

批注4:将saved_root_nam指定的设备加载。 
 
批注5:参数ROOT_DEV存放设备节点号。

批注6:挂载initrd,见下面的详解。 
 
批注7:如果指定mount_initrd为true,即没有指定在函数initrd_load中mount的话,则在这里重新realfs的mount操作。 
 
批注8:将挂载点从当前目录(实际当前的目录在mount_root中或者在mount_block_root中指定)移到根目录。
即是如/dev/mtdblock2。 
 
批注9:将当前目录当作系统的根目录,至此虚拟系统根目录文件系统切换到了实际的根目录文件系统。


点击(此处)折叠或打开

  1. int __init initrd_load(void)
  2. {
  3.     if (mount_initrd) { // 批注1
  4.         create_dev("/dev/ram", Root_RAM0); // 批注2
  5.         /*
  6.          * Load the initrd data into /dev/ram0. Execute it as initrd
  7.          * unless /dev/ram0 is supposed to be our actual root device,
  8.          * in that case the ram disk is just set up here, and gets
  9.          * mounted in the normal path.
  10.          */
  11.         if (rd_load_image("/initrd.image") && ROOT_DEV != Root_RAM0) { // 批注3
  12.             sys_unlink("/initrd.image");
  13.             handle_initrd(); // 批注4
  14.             return 1;
  15.         }
  16.     }
  17.     sys_unlink("/initrd.image");
  18.     return 0;
  19. }
批注1:可以通过Kernel的参数“noinitrd“来配置mount_initrd的值,默认为1,很少看到有项目区配置该值,
所以一般情况下,mount_initrd的值应该为1。

批注2:创建一个Root_RAM0的设备节点/dev/ram。 
 
批注3:如果根文件设备号不是Root_RAM0则程序就会执行代码就进入执行,如指定的/dev/mtdblock4设备节点
肯定就不是Root_RAM0。另外还将文件initrd.image释放到节点/dev/ram0,也就是对应image-initrd的操作。

批注4:函数handle_initrd主要功能是执行Initrd中的linuxrc文件,并且将realfs的根目录设置为当前
目录。其实前面也已经提到了,这些操作只对image-cpio的情况下才会去执行。。


点击(此处)折叠或打开

  1. static void __init handle_initrd(void)
  2. {
  3.     int error;
  4.     int pid;

  5.     real_root_dev = new_encode_dev(ROOT_DEV); // 批注1
  6.     create_dev("/dev/root.old", Root_RAM0); // 批注2
  7.     /* mount initrd on rootfs' /root */
  8.     mount_block_root("/dev/root.old", root_mountflags & ~MS_RDONLY);
  9.     sys_mkdir("/old", 0700); // 批注3
  10.     root_fd = sys_open("/", 0, 0);
  11.     old_fd = sys_open("/old", 0, 0);
  12.     /* move initrd over / and chdir/chroot in initrd root */
  13.     sys_chdir("/root"); // 批注4
  14.     sys_mount(".", "/", NULL, MS_MOVE, NULL);
  15.     sys_chroot(".");

  16.     /*
  17.      * In case that a resume from disk is carried out by linuxrc or one of
  18.      * its children, we need to tell the freezer not to wait for us.
  19.      */
  20.     current->flags |= PF_FREEZER_SKIP;

  21.     pid = kernel_thread(do_linuxrc, "/linuxrc", SIGCHLD); // 批注5
  22.     if (pid > 0)
  23.         while (pid != sys_wait4(-1, NULL, 0, NULL))
  24.             yield();

  25.     current->flags &= ~PF_FREEZER_SKIP;

  26.     /* move initrd to rootfs' /old */
  27.     sys_fchdir(old_fd); // 批注6
  28.     sys_mount("/", ".", NULL, MS_MOVE, NULL);
  29.     /* switch root and cwd back to / of rootfs */
  30.     sys_fchdir(root_fd);
  31.     sys_chroot(".");
  32.     sys_close(old_fd);
  33.     sys_close(root_fd);

  34.     if (new_decode_dev(real_root_dev) == Root_RAM0) { // 批注7
  35.         sys_chdir("/old");
  36.         return;
  37.     }

  38.     ROOT_DEV = new_decode_dev(real_root_dev); // 批注8
  39.     mount_root();

  40.     printk(KERN_NOTICE "Trying to move old root to /initrd ... ");
  41.     error = sys_mount("/old", "/root/initrd", NULL, MS_MOVE, NULL);
  42.     if (!error)
  43.         printk("okay\n");
  44.     else {
  45.         int fd = sys_open("/dev/root.old", O_RDWR, 0);
  46.         if (error == -ENOENT)
  47.             printk("/initrd does not exist. Ignored.\n");
  48.         else
  49.             printk("failed\n");
  50.         printk(KERN_NOTICE "Unmounting old root\n");
  51.         sys_umount("/old", MNT_DETACH);
  52.         printk(KERN_NOTICE "Trying to free ramdisk memory ... ");
  53.         if (fd < 0) {
  54.             error = fd;
  55.         } else {
  56.             error = sys_ioctl(fd, BLKFLSBUF, 0);
  57.             sys_close(fd);
  58.         }
  59.         printk(!error ? "okay\n" : "failed\n");
  60.     }
  61. }
批注1:real_root_dev为一个全局变量,用来保存realfs的设备号。


批注2:调用mount_block_root将realfs加载到VFS的/root下。 
 
批注3:提取rootfs的根文件描述符并将其保存到root_fd,资料中提及其用处就是在后续调用
sys_chroot到initrd的文件系统后,处理完init请求后,还能够再次切回到rootfs。


批注4:sys_chroot到initrd文件系统,前面已经挂载initrd到VFS的root目录下。 
 
批注5:执行initrd中的linuxrc,并等待执行结束。


批注6:initrd执行结束后,切回到rootfs。 
 
批注7:如果real_root_dev直接配置为Root_RAM0,也即直接使用直接使用initrd作为realfs,
改变当前目录到initrd中,并直接返回。 
 
批注8:执行完Linuxrc后,realfs已经确定,则调用mount_root将realfs挂载到VFS的/root目录下,
并将当前的目录配置为VFS的/root。 


再回过头来再看上面提到的init_post,该函数实际上是在Kernel_init中最后执行的函数

static noinline int init_post(void)
__releases(kernel_lock)

    if (ramdisk_execute_command)
    
    if (execute_command)
    
    run_init_process("/sbin/init");
    run_init_process("/etc/init");
    run_init_process("/bin/init");
    run_init_process("/bin/sh");

可以看到,在该函数的最后,以此会去搜索文件并执行ramdisk_execute_command、execute_command、/sbin/init、
/etc/init、/bin/init和/bin/sh,如果发现这些文件均不存在的话,则通过panic输出错误命令,并将当前的系统Halt在那里


大概总结一下流程:
内核会先创建一个基于内存的虚拟文件系统rootfs(和用什么机制无关);
根据采用的机制来挂载虚拟
文件系统,完成挂载真正文件系统前的工作;
挂载真正的文件系统;
执行最初的应用程序。

阅读(1511) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~