步骤:

    1. 先执行clone,中带有CLONE_NEWNS标志时,新的mnt ns在子进程中被创建,新的mnt ns是一份父mnt ns的拷贝
    2. 接着 bind mount
    3. 最后执行pivot_root,切换rootfs

    mount命令是从/etc/mtab中读取的信息,而不是直接从内核读取的。/proc/mounts显示的是内核的数据。实际上,/proc/mounts是/proc/self/mounts的符号链接。

    /proc/mounts显示当前mnt namespace的mount信息,参考内核函数fs/namespace.c/show_vfsmnt。另外,我们可以通过/proc/self/mountinfo得到更详细的信息:



    # cat /proc/self/mountinfo 21 1 253:0 / / rw,relatime - ext4 /dev/mapper/vg_yy1-lv_root rw,barrier=1,data=ordered 36 21 253:0 /root/temp /tmpmnt rw,relatime - ext4 /dev/mapper/vg_yy1-lv_root rw,barrier=1,data=ordered
    各个字段的含义:
    36 35 98:0 /mnt1 /mnt2 rw,noatime master:1 - ext3 /dev/root rw,errors=continue
    (1)(2)(3) (4) (5) (6) (7) (8) (9) (10) (11)

    • (1) mount ID: unique identifier of the mount (may be reused after umount)
    • (2) parent ID: ID of parent (or of self for the top of the mount tree)
    • (3) major:minor: value of st_dev for files on filesystem
    • (4) root: root of the mount within the filesystem 这个能看到容器目录对应的宿主机目录
    • (5) mount point: mount point relative to the process’s root
    • (6) mount options: per mount options
    • (7) optional fields: zero or more fields of the form “tag[:value]”
    • (8) separator: marks the end of the optional fields
    • (9) filesystem type: name of filesystem of the form “type[.subtype]”
    • (10) mount source: filesystem specific information or “none”
    • (11) super options: per super block options

    Parsers should ignore all unrecognised optional fields. Currently the possible optional fields are:
    shared:X mount is shared in peer group X master:X mount is slave to peer group X propagate_from:X mount is slave and receives propagation from peer group X () unbindable mount is unbindable
    (
    ) X is the closest dominant peer group under the process’s root. If X is the immediate master of the mount, or if there’s no dominant peer group under the same root, then only the “master:X” field is present and not the “propagate_from:X” field.
    For more information on mount propagation see:
    Documentation/filesystems/sharedsubtree.txt