解析linux根文件系统的挂载过程_第1页
解析linux根文件系统的挂载过程_第2页
解析linux根文件系统的挂载过程_第3页
解析linux根文件系统的挂载过程_第4页
解析linux根文件系统的挂载过程_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、一:前言 前段时间在编译 kernel的时候发现rootfs挂载不上。相同的root选项设置旧版的image却可 以。为了彻底解决这个问题。研究了一下 rootfs的挂载过程。特总结如下,希望能给这部份 知识点比较迷茫的朋友一点帮助。二:roo一:前言 前段时间在编译 kernel的时候发现rootfs挂载不上。相同的root选项设置旧版的image却可 以。为了彻底解决这个问题。研究了一下 rootfs的挂载过程。特总结如下,希望能给这部份 知识点比较迷茫的朋友一点帮助。二:rootfs的种类总的来说,rootfs分为两种:虚拟 rootfs和真实rootfs.现在kernel的发展趋势是将

2、更多的功 能放到用户空间完成。以保持内核的精简。虚拟 rootfs也是各linux发行厂商普遍采用的一 种方式。可以将一部份的初始化工作放在虚拟的rootfs里完成。然后切换到真实的文件系统.在虚拟rootfs的发展过程中。又有以下几个版本:initramfs:Initramfs是在kernel 2.5中引入的技术,实际上它的含义就是:在内核镜像中附加一个cpio包,这个cpio包中包含了一个小型的文件系统,当内核启动时,内核将这个 cpio包解开, 并且将其中包含的文件系统释放到rootfs中,内核中的一部分初始化代码会放到这个文件系统中,作为用户层进程来执行。这样带来的明显的好处是精简了内

3、核的初始化代码,而且使得内核的初始化过程更容易定制。这种这种方式的rootfs是包含在kernel image之中的.cpio-initrd: cpio 格式的 rootfsimage-initrd:传统格式的 rootfs关于这两种虚拟文件系统的制作请自行参阅其它资料三:rootfs文件系统的挂载过程 这里说的rootfs不同于上面分析的rootfs。这里指的是系统初始化时的根结点。即/结点。它是其于内存的rootfs文件系统。这部份之前在 << linux启动过程分析 >>和文件系统中已经分 析过。为了知识的连贯性这里再重复一次。Start_kernel() mnt

4、_init(): void _init mnt_init(void)(init_rootfs();init_mount_tree();Init_rootfs的代码如下: int _init init_rootfs(void)int err;err = bdi_init(&ramfs_backing_dev_info);if (err)return err;err = register_filesystem(&rootfs_fs_type);if (err)bdi_destroy(&ramfs_backing_dev_info);return err;这个函数很简单。就是

5、注册了rootfs的文件系统.init_mount_tree()代码如下:static void _init init_mount_tree(void)(struct vfsmount *mnt;struct mnt_namespace *ns;struct path root;mnt = do_kern_mount("rootfs”, 0, "rootfs", NULL);if (IS_ERR(mnt)panic("Can't create rootfs");ns = kmalloc(sizeof(*ns), GFP_KERNEL);

6、if (!ns)panic("Can't allocate initial namespace");atomic_set(&ns->count, 1);INIT_LIST_HEAD(&ns->list);init_waitqueue_head(&ns->poll);ns->event = 0;list_add(&mnt->mnt_list, &ns->list);ns->root = mnt;mnt->mnt_ns = ns;init_task.nsproxy->mnt_n

7、s = ns;get_mnt_ns(ns);root.mnt = ns->root;root.dentry = ns->root->mnt_root;set_fs_pwd(current->fs, &root); set_fs_root(current->fs, &root);在这里,将rootfs文件系统挂载。它的挂载点默认为”/”.最后切换进程的根目录和当前目录为” /”.这也就是根目录的由来。不过这里只是初始化。等挂载完具体的文件系统之后,一般都会将根目录切换到具体的文件系统。所以在系统启动之后,用mount命令是看不到rootfs的挂载信息

8、的.四:虚拟文件系统的挂载根目录已经挂上去了,可以挂载具体的文件系统了在 start_kernel()(Test_init() ckernel_init():static int _init kernel_init(void * unused)do_basic_setup();if (!ramdisk_execute_command)ramdisk_execute_command = "/init"if (sys_access(const char _user *) ramdisk_execute_command, 0) != 0) ramdisk_execute_comm

9、and = NULL;prepare_namespace();/* Ok, we have completed the initial bootup, and* we're essentially up and running. Get rid of the* initmem segments and start the user-mode stuff.*/init_post();return 0;do_basic_setup()是一个很关键的函数,所有直接编译在 kernel中的模块都是由它启动的。代码片段如下:static void _init do_basic_setup(voi

10、d)/* drivers will send hotplug events */ init_workqueues();usermodehelper_init();driver_init();init_irq_proc();do_initcalls();Do_initcalls()用来启动所有在 _initcall_start和_initcall_end段的函数,而静态编译进内核的 modules也会将其入口放置在这段区间里。跟根文件系统相关的初始化函数都会由rootfs_initcall ()所引用。注意到有以下初始化函数:rootfs_initcall(populate_rootfs);也就

11、是说会在系统初始化的时候会调用populate_rootfs进行初始化。代码如下:static int _init populate_rootfs(void)char *err = unpack_to_rootfs(_initramfs_start,_initramfs_end - _initramfs_start, 0);if (err)panic(err);if (initrd_start) #ifdef CONFIG_BLK_DEV_RAMint fd;printk(KERN_INFO "checking if image is initramfs.");err =

12、unpack_to_rootfs(char *)initrd_start,initrd_end - initrd_start, 1);if (!err) printk(" it isn");unpack_to_rootfs(char *)initrd_start,initrd_end - initrd_start, 0); free_initrd();return 0;printk("it isn't (%s); looks like an initrdn", err);fd = sys_open("/initrd.image”, O_

13、WRONL Y|O_CREA T, 0700);if (fd >= 0) sys_write(fd, (char *)initrd_start,initrd_end - initrd_start); sys_close(fd);free_initrd();#elseprintk(KERN_INFO "Unp acking initramfs.");err = unpack_to_rootfs(char *)initrd_start,initrd_end - initrd_start, 0);if (err)panic(err);printk(" donen&

14、quot;);free_initrd();#endifreturn 0;unpack_to_rootfs :顾名思义就是解压包,并将其释放至rootfs。它实际上有两个功能,一个是释放包,一个是查看包,看其是否属于cpio结构的包。功能选择是根据最后的一个参数来区分的.在这个函数里,对应我们之前分析的三种虚拟根文件系统的情况。一种是跟kernel融为一体的initramfs.在编译 kernel的 时候,通过 链接脚 本将其 存放在_initramfs_start 至 _initramfs_end的区域。这种情况下,直接调用unpack_to_rootfs将其释放到根目录.如果不是属于这种形式

15、的。 也就是_initramfs_start和_initramfs_end的值相等,长度为零。 不会做 任何处理。退出.对应后两种情况。 从代码中看至叭 必须要配制 CONFIG_BLK_DEV_RAM 才会支持 image-initrd。否则全当成 cpio-initrd的形式处理。对于是cpio-initrd的情况。直接将其释放到根目录。对于是 image-initrd的情况。将其释放 到/initrd.image.最后将initrd内存区域归入伙伴系统。这段内存就可以由操作系统来做其它 的用途了。接下来,内核对这几种情况又是怎么处理的呢?不要着急。往下看:回到kernel_init()这

16、个函数:static int _init kernel_init(void * unused)do_basic_setup();/* check if there is an early userspace init. If yes, let it do all* the work*/ramdisk_execute_command = "/init"if (sys_access(const char _user *) ramdisk_execute_command, 0) != 0) ramdisk_execute_command = NULL;prepare_namesp

17、ace();/* Ok, we have completed the initial bootup, and* we're essentially up and running. Get rid of the* initmem segments and start the user-mode stuff.*/init_post();return 0;ramdisk_execute_command:在kernel解析引导参数的时候使用。如果用户指定了 init文件路径,即使用了 init= ",就会将这个参数值存放到这里。如果没有指定init文件路径。默认为/init对应于前面

18、一段的分析,我们知道,对于 initramdisk和cpio-initrd的情况,都会将虚拟根文 件系统释放到根目录。如果这些虚拟文件系统里有/init这个文件。就会转入到 init_post()。Init_post()代码如下:static int noinline init_post(void)free_initmem();unlock_kernel();mark_rodata_ro();system_state = SYSTEM_RUNNING;numa_default_policy();if (sys_open(const char _user *) "/dev/consol

19、e", O_RDWR, 0) < 0)printk(KERN_W ARNING "Warning: unable to open an initial console.n");(void) sys_dup(0);(void) sys_dup(0);if (ramdisk_execute_command) (run_init_process(ramdisk_execute_command);printk(KERN_W ARNING "Failed to execute %sn",ramdisk_execute_command);/* We

20、try each of these until one succeeds.* The Bourne shell can be used instead of init if we are* trying to recover a really broken machine.*/if (execute_command) (run_init_process(execute_command);printk(KERN_W ARNING "Failed to execute %s. Attempting ""defaults.n”, execute_command);run

21、_init_process("/sbin/init");run_init_process("/etc/init");run_init_process("/bin/init");run_init_process("/bin/sh");panic("No init found. Try passing init= option to kernel.");从代码中可以看中,会依次执行指定的init文件,如果失败,就会执行/sbin/init, /etc/init,/bin/init,/bin/sh注意

22、的是,run_init_process在调用相应程序运行的时候,用的是kernel_execve。也就是说调用进程会替换当前进程。 只要上述任意一个文件调用成功,就不会返回到这个函数。 如果上面几个文件都无法执行。打印出没有找到init文件的错误。对于image-hdr或者是虚拟文件系统中没有包含/init的情况,会由prepare_namespace(处理。代码如下:void _init prepare_namespace(void)(int is_floppy;if (root_delay) (printk(KERN_INFO "Waiting %dsec before moun

23、ting root device.n", root_delay);ssleep(root_delay);/* wait for the known devices to complete their probing */while (driver_probe_done() != 0) msleep(100);/mtd的处理md_run_setup();if (saved_root_name0) root_device_name = saved_root_name;if (!strncmp(root_device_name, "mtd", 3) mount_bloc

24、k_root(root_device_name, root_mountflags);goto out;ROOT_DEV = name_to_dev_t(root_device_name);if (strncmp(root_device_name, "/dev/", 5) = 0)root_device_name += 5;if (initrd_load() goto out;/* wait for any asynchronous scanning to complete */if (ROOT_DEV = 0) && root_wait) printk(KE

25、RN_INFO "Waiting for root device %s.n”,saved_root_name);while (driver_probe_done() != 0 |(ROOT_DEV = name_to_dev_t(saved_root_name) = 0) msleep(100);is_floppy = MAJOR(ROOT_DEV) = FLOPPY_MAJOR;if (is_floppy && rd_doload && rd_load_disk(0) ROOT_DEV = Root_RAM0;mount_root();out:sys

26、_mount(".”, ”/”, NULL, MS_MOVE, NULL);sys_chroot(".”);这里有几个比较有意思的处理,首先用户可以用root=来指定根文件系统。它的值保存在saved_root_name中。如果用户指定了以 mtd开始的字串做为它的根文件系统。就会直接去 挂载。这个文件是 mtdblock的设备文件。否则将设备结点文件转换为ROOT_DEV即设备节点号然后,转向initrd_load()执行initrd预处理后,再将具体的根文件系统挂载。注意到,在这个函数末尾。会调用sys_mount()来移动当前文件系统挂载点到”/”目录下。然后将根目录

27、切换到当前目录。这样,根文件系统的挂载点就成为了我们在用户空间所看到的” /” 了 .对于其它根文件系统的情况,会先经过initrd的处理。即int _init initrd_load(void)(if (mount_initrd) (create_dev("/dev/ram", Root_RAM0);/* Load the initrd data into /dev/ram0. Execute it as initrd* unless /dev/ram0 is supposed to be our actual root device,* in that case the

28、 ram disk is just set up here, and gets* mounted in the normal path.*/if (rd_load_image("/initrd.image") && ROOT_DEV != Root_RAM0) (sys_unlink("/initrd.image");handle_initrd();return 1;sys_unlink("/initrd.image");return 0;建立一个 ROOT_RAM)的设备节点,并将 /initrd/.image 释放

29、到这个节点中,/initrd.image 的 内容,就是我们之前分析的image-initrd。如果根文件设备号不是ROOT_RAM0(用户指定的根文件系统不是/dev/ram0就会转入到handle_initrd()如果当前根文件系统是/dev/ram0.将其直接挂载就好了。handle_initrd ()代码如下:static void _init handle_initrd(void)int error;int pid;real_root_dev = new_encode_dev(ROOT_DEV);create_dev("/dev/root.old”, Root_RAM0);

30、/* mount initrd on rootfs' /root */mount_block_root("/dev/root.old", root_mountflags & MS_RDONL Y);sys_mkdir("/old", 0700);root_fd = sys_open("/", 0, 0);old_fd = sys_open("/old", 0, 0);/* move initrd over / and chdir/chroot in initrd root */sys_chdir(&

31、quot;/root");sys_mount(".", "/", NULL, MS_MOVE, NULL);sys_chroot(".”);/* In case that a resume from disk is carried out by linuxrc or one of* its children, we need to tell the freezer not to wait for us.*/current->flags |= PF_FREEZER_SKIP;pid = kernel_thread(do_linux

32、rc, "/linuxrc", SIGCHLD);if (pid > 0)while (pid != sys_wait4(-1, NULL, 0, NULL)yield();current->flags &= PF_FREEZER_SKIP;/* move initrd to rootfs' /old */ sys_fchdir(old_fd);sys_mount("/", ”.”, NULL, MS_MOVE, NULL);/* switch root and cwd back to / of rootfs */sys_fchdir(root_fd);sys_chroot(".”);sy

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论