bind mount是一项非常有用的技术,它可以实现跨文件系统共享数据,这是容器实现自身文件系统的基础,来看个示例:
# mount -o bind /root/tmp /tmpmnt
# cat /proc/mounts
/dev/mapper/vg_yy1-lv_root / ext4 rw,relatime,barrier=1,data=ordered 0 0
/dev/mapper/vg_yy1-lv_root /tmpmnt ext4 rw,relatime,barrier=1,data=ordered 0 0
对于bind mount,将创建一个新的vfsmount,并将其device name为source的device name:
///入参为source对应的vfsmount、dentry,返回新的vfsmount
static struct vfsmount *
clone_mnt(struct vfsmount *old, struct dentry *root)
{
struct super_block *sb = old->mnt_sb;
struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname); ///source的device name
if (mnt) {
mnt->mnt_flags = old->mnt_flags;
atomic_inc(&sb->s_active);
mnt->mnt_sb = sb;
mnt->mnt_root = dget(root); ///本文件系统的根目录
mnt->mnt_mountpoint = mnt->mnt_root; ///挂载点暂时指向本文件系统的根目录,后面再修改指向target的挂载点目录
mnt->mnt_parent = mnt; ///暂时指向自己
mnt->mnt_namespace = old->mnt_namespace;
}
return mnt;
}