歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux虛擬文件系統(安裝根文件系統)

Linux虛擬文件系統(安裝根文件系統)

日期:2017/2/28 15:57:10   编辑:Linux教程
安裝根文件系統式系統初始化的關鍵部分。Linux內核允許根文件系統放在很多不同的地方,比如硬盤分區、軟盤、通過NFS共享的遠程文件系統以及保存在ramdisk中。內核要在變量ROOT_DEV中尋找包含根文件系統的磁盤主設備號。當編譯內核時,或者像最初的啟動裝入程序傳遞一個合適的“root”選項時,根文件系統可以被指定為/dev目錄下的一個設備文件。

相關閱讀:

http://www.linuxidc.com/Linux/2012-02/53771.htm

安裝根文件系統分為兩個階段:

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. return err;
  14. }
[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. BUG_ON(strchr(fs->name, '.'));
  19. if (fs->next)
  20. return -EBUSY;
  21. INIT_LIST_HEAD(&fs->fs_supers);
  22. write_lock(&file_systems_lock);
  23. /*從system_type鏈表中查找指定名稱的file_system_type*/
  24. p = find_filesystem(fs->name, strlen(fs->name));
  25. if (*p)
  26. res = -EBUSY;
  27. else
  28. *p = fs;
  29. write_unlock(&file_systems_lock);
  30. return res;
  31. }

根文件系統定義如下

[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. if (IS_ERR(s))
  10. return PTR_ERR(s);
  11. s->s_flags = flags;
  12. /*這裡實際調用ramfs_fill_super,對sb結構的屬性進行設置*/
  13. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  14. if (error) {
  15. deactivate_locked_super(s);
  16. return error;
  17. }
  18. s->s_flags |= MS_ACTIVE;
  19. simple_set_mnt(mnt, s);/*設置mnt和sb關聯*/
  20. return 0;
  21. }
[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. retry:
  18. spin_lock(&sb_lock);
  19. if (test) {
  20. list_for_each_entry(old, &type->fs_supers, s_instances) {
  21. if (!test(old, data))
  22. continue;
  23. if (!grab_super(old))
  24. goto retry;
  25. if (s) {
  26. up_write(&s->s_umount);
  27. destroy_super(s);
  28. }
  29. return old;
  30. }
  31. }
  32. if (!s) {/*如果找不到sb,從內存中申請一個*/
  33. spin_unlock(&sb_lock);
  34. s = alloc_super(type);
  35. if (!s)
  36. return ERR_PTR(-ENOMEM);
  37. goto retry;
  38. }
  39. err = set(s, data);
  40. if (err) {
  41. spin_unlock(&sb_lock);
  42. up_write(&s->s_umount);
  43. destroy_super(s);
  44. return ERR_PTR(err);
  45. }
  46. /*初始化得到的sb結構*/
  47. s->s_type = type;
  48. strlcpy(s->s_id, type->name, sizeof(s->s_id));
  49. /*加入鏈表尾*/
  50. list_add_tail(&s->s_list, &super_blocks);
  51. list_add(&s->s_instances, &type->fs_supers);
  52. spin_unlock(&sb_lock);
  53. get_filesystem(type);
  54. return s;
  55. }
Copyright © Linux教程網 All Rights Reserved