Linux虚拟文件系统(安装根文件系统)


安装根文件系统式系统初始化的关键部分。Linux内核允许根文件系统放在很多不同的地方,比如硬盘分区、软盘、通过NFS共享的远程文件系统以及保存在ramdisk中。内核要在变量ROOT_DEV中寻找包含根文件系统的磁盘主设备号。当编译内核时,或者像最初的启动装入程序传递一个合适的“root”选项时,根文件系统可以被指定为/dev目录下的一个设备文件。

相关阅读:

安装根文件系统分为两个阶段:

1,内核安装特殊rootfs文件系统,该文件系统仅提供一个作为初始安装点的空目录

start_kernel()->vfs_caches_init()->mnt_init()->init_rootfs()

[cpp]
  1. /*初始化根文件系统*/  
  2. int __init init_rootfs(void)  
  3. {  
  4.     int err;  
  5.     /*初始化ramfs_backing_dev_info*/  
  6.     err = bdi_init(&ramfs_backing_dev_info);  
  7.     if (err)  
  8.         return err;  
  9.     /*注册rootfs_fs_type文件类型*/  
  10.     err = register_filesystem(&rootfs_fs_type);  
  11.     if (err)/*如果出错,销毁上面初始化的*/  
  12.         bdi_destroy(&ramfs_backing_dev_info);  
  13.   
  14.     return err;  
  15. }  
[cpp]
  1. static struct backing_dev_info ramfs_backing_dev_info = {  
  2.     .name       = "ramfs",  
  3.     .ra_pages   = 0,    /* No readahead */  
  4.     .capabilities   = BDI_CAP_NO_ACCT_AND_WRITEBACK |  
  5.               BDI_CAP_MAP_DIRECT | BDI_CAP_MAP_COPY |  
  6.               BDI_CAP_READ_MAP | BDI_CAP_WRITE_MAP | BDI_CAP_EXEC_MAP,  
  7. };  
[cpp]
  1. /** 
  2.  *  register_filesystem - register a new filesystem 
  3.  *  @fs: the file system structure 
  4.  * 
  5.  *  Adds the file system passed to the list of file systems the kernel 
  6.  *  is aware of for mount and other syscalls. Returns 0 on success, 
  7.  *  or a negative errno code on an error. 
  8.  * 
  9.  *  The &struct file_system_type that is passed is linked into the kernel  
  10.  *  structures and must not be freed until the file system has been 
  11.  *  unregistered. 
  12.  */  
  13.  /*注册一个新的文件系统*/  
  14. int register_filesystem(struct file_system_type * fs)  
  15. {  
  16.     int res = 0;  
  17.     struct file_system_type ** p;  
  18.   
  19.     BUG_ON(strchr(fs->name, '.'));  
  20.     if (fs->next)  
  21.         return -EBUSY;  
  22.     INIT_LIST_HEAD(&fs->fs_supers);  
  23.     write_lock(&file_systems_lock);  
  24.     /*从system_type链表中查找指定名称的file_system_type*/  
  25.     p = find_filesystem(fs->name, strlen(fs->name));  
  26.     if (*p)  
  27.         res = -EBUSY;  
  28.     else  
  29.         *p = fs;  
  30.     write_unlock(&file_systems_lock);  
  31.     return res;  
  32. }  

根文件系统定义如下

[cpp]
  1. static struct file_system_type rootfs_fs_type = {  
  2.     .name       = "rootfs",  
  3.     .get_sb     = rootfs_get_sb,  
  4.     .kill_sb    = kill_litter_super,  
  5. };  

下面看看他的两个函数

[cpp]
  1. /*获得根目录的sb*/  
  2. static int rootfs_get_sb(struct file_system_type *fs_type,  
  3.     int flags, const char *dev_name, void *data, struct vfsmount *mnt)  
  4. {  
  5.     return get_sb_nodev(fs_type, flags|MS_NOUSER, data, ramfs_fill_super,  
  6.                 mnt);  
  7. }  
[cpp]
  1. int get_sb_nodev(struct file_system_type *fs_type,  
  2.     int flags, void *data,  
  3.     int (*fill_super)(struct super_block *, void *, int),  
  4.     struct vfsmount *mnt)  
  5. {  
  6.     int error;  
  7.     /*获得sb结构*/  
  8.     struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);  
  9.   
  10.     if (IS_ERR(s))  
  11.         return PTR_ERR(s);  
  12.   
  13.     s->s_flags = flags;  
  14.     /*这里实际调用ramfs_fill_super,对sb结构的属性进行设置*/  
  15.     error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);  
  16.     if (error) {  
  17.         deactivate_locked_super(s);  
  18.         return error;  
  19.     }  
  20.     s->s_flags |= MS_ACTIVE;  
  21.     simple_set_mnt(mnt, s);/*设置mnt和sb关联*/  
  22.     return 0;  
  23. }  
[cpp]
  1. /** 
  2.  *  sget    -   find or create a superblock 
  3.  *  @type:  filesystem type superblock should belong to 
  4.  *  @test:  comparison callback 
  5.  *  @set:   setup callback 
  6.  *  @data:  argument to each of them 
  7.  */  
  8.  /*查找或创建一个sb结构*/  
  9. struct super_block *sget(struct file_system_type *type,  
  10.             int (*test)(struct super_block *,void *),  
  11.             int (*set)(struct super_block *,void *),  
  12.             void *data)  
  13. {  
  14.     struct super_block *s = NULL;  
  15.     struct super_block *old;  
  16.     int err;  
  17.   
  18. retry:  
  19.     spin_lock(&sb_lock);  
  20.     if (test) {  
  21.         list_for_each_entry(old, &type->fs_supers, s_instances) {  
  22.             if (!test(old, data))  
  23.                 continue;  
  24.             if (!grab_super(old))  
  25.                 goto retry;  
  26.             if (s) {  
  27.                 up_write(&s->s_umount);  
  28.                 destroy_super(s);  
  29.             }  
  30.             return old;  
  31.         }  
  32.     }  
  33.     if (!s) {/*如果找不到sb,从内存中申请一个*/  
  34.         spin_unlock(&sb_lock);  
  35.         s = alloc_super(type);  
  36.         if (!s)  
  37.             return ERR_PTR(-ENOMEM);  
  38.         goto retry;  
  39.     }  
  40.           
  41.     err = set(s, data);  
  42.     if (err) {  
  43.         spin_unlock(&sb_lock);  
  44.         up_write(&s->s_umount);  
  45.         destroy_super(s);  
  46.         return ERR_PTR(err);  
  47.     }  
  48.     /*初始化得到的sb结构*/  
  49.     s->s_type = type;  
  50.     strlcpy(s->s_id, type->name, sizeof(s->s_id));  
  51.     /*加入链表尾*/  
  52.     list_add_tail(&s->s_list, &super_blocks);  
  53.     list_add(&s->s_instances, &type->fs_supers);  
  54.     spin_unlock(&sb_lock);  
  55.     get_filesystem(type);  
  56.     return s;  
  57. }  
  • 1
  • 2
  • 3
  • 4
  • 下一页

相关内容