45 mount 文件系统

前言

在 linux 中常见的文件系统 有很多, 如下

基于磁盘的文件系统, ext2, ext3, ext4, xfs, btrfs, jfs, ntfs

内存文件系统, procfs, sysfs, tmpfs, squashfs, debugfs

闪存文件系统, ubifs, jffs2, yaffs

文件系统这一套体系在 linux 有一层 vfs 抽象, 用户程序不用关心 底层文件系统的具体实现, 用户只用操作 open/read/write/ioctl/close 的相关 系统调用, 这一层系统调用 会操作 vfs 来处理响应的业务

vfs 会有上面各种文件系统对应的 读写 相关服务, 进而 将操作下沉到 具体的文件系统

我们这里 着重来关注一下 文件系统的挂载

系统自身的文件系统挂载

在启动的过程中需要挂载如下文件系统

复制代码
mnt_init - sysfs_init
mnt_init - init_rootfs
mnt_init - init_ramfs
vfs_caches_init - bdev_cache_init
start_kernel - proc_root_init
start_kernel - cpuset_init
start_kernel - cgroup_init
start_kernel - cgroup_init - cgroup2
kernel_init - shmem_init
kernel_init - driver_init - devtmpfs_init
kernel_init - do_init_calls - debugfs_init
kernel_init - do_init_calls - tracefs_init
kernel_init - do_init_calls - securityfs_init
kernel_init - do_init_calls - sock_init
kernel_init - do_init_calls - bpf_init
kernel_init - do_init_calls - pipepfs_init
kernel_init - do_init_calls - init_hugetlbfs_fs
kernel_init - do_init_calls - init_devpts_fs
kernel_init - do_init_calls - ext4_init_fs - register_as_ext3
kernel_init - do_init_calls - ext4_init_fs - register_as_ext2
kernel_init - do_init_calls - ext4_init_fs
kernel_init - do_init_calls - init_squashfs_fs
kernel_init - do_init_calls - init_vfat_fs
kernel_init - do_init_calls - init_ecrypt_fs
kernel_init - do_init_calls - fuse_fs_init
kernel_init - do_init_calls - fuse_ctl_init
kernel_init - do_init_calls - init_pstore_fs
kernel_init - do_init_calls - init_mqueue_fs

基于 tmpfs 来调试

主要是调试 mount 这个系统调用, 我们使用比较简单一点的 tmpfs 来进行调试, 测试

主要是有几个流程, 创建super_block, 初始化super_block, 真实的挂载操作

复制代码
(initramfs) mkdir /memFs
(initramfs) mount -t tmpfs -osize=64m tmpfs /memFs

创建 super_block

为 super_block 分配空间, 初始化各个链表, 各种基础属性, 锁

注册 super_block 到 全局链表

初始化 super_block

主要是基于 各个 fsType 传入的 fill_super 来进行初始化 super_block

初始化 maxBytes, blockSize, blockSizeBits, magic, s_op[文件系统给的各类操作], root 节点的 inode, 创建 root 对应的 dentry, 并和 super_block 关联起来

挂载 mount节点 到 mount树

如下 这里还是在初始化 mount, vfsmount

记录 mount_root, mount_super_block, mount_point 等等, 返回给 上层的是 vfs_mount

将 mount 挂载到 mount 树, 挂载路径为 "/memFs"

这里就是在 namespace.mount_hashtable 中注册 rootfs 的 "/memFs" 挂载到了 "tmpfs" 的 "/" 上面, 访问 rootfs 的 "/memFs" 需要转换为 访问 tmpfs 的 "/"

除此之外 还有记录其他的元数据什么的

挂载信息的使用

在根据 path 进行 lookup 的时候, 会查询 mount_hashtable, 查看给定的 路径 是否挂载了其他文件系统, 如果有转换为 给定的文件系统的 "/"

如下地方就是在 切换 path

更详细可以参考 proc 文件系统 中 "根据 path 获取到 inode 的过程"

相关推荐
cui__OaO18 分钟前
Linux软件编程--线程
linux·开发语言·线程·互斥锁·死锁·信号量·嵌入式学习
小狗爱吃黄桃罐头20 分钟前
正点原子【第四期】Linux之驱动开发篇学习笔记-1.1 Linux驱动开发与裸机开发的区别
linux·驱动开发·学习
小晶晶京京1 小时前
day34-LNMP详解
linux·运维·服务器
画个太阳作晴天1 小时前
A12预装app
linux·服务器·前端
fengyehongWorld1 小时前
Linux crontab定时任务
linux·运维
shuangrenlong2 小时前
ubuntu更新chrome版本
linux·chrome·ubuntu
碎像2 小时前
Linux上配置环境变量
linux·运维·服务器
敲上瘾3 小时前
Linux系统cgroups资源精细化控制基础
linux·测试工具·docker·压力测试·cgroups
起个昵称吧3 小时前
线程相关编程、线程间通信、互斥锁
linux·算法
sunflower_w4 小时前
linux I2C核心、总线与设备驱动
linux·运维·服务器