代码改变世界

setns 切换命名空间,/proc 目录与 Namespace

2019-10-08 21:11  Loull  阅读(3598)  评论(0编辑  收藏  举报

http://man7.org/linux/man-pages/man2/setns.2.html

int setns(int fd, int nstype);

Given a file descriptor referring to a namespace, reassociate the calling thread with that namespace.

当前线程切到fd对应的namespace,并且这个线程上下文中创建的新线程也在这个namespace

  • fd:要加入的 namespace 的文件描述符,一般为 /proc/[pid]/ns 下某个对应类型 namespace 的软链接;

  • nstype:调用进程想要加入的 namesapce 的类型,其类型对应上文的表格中的 7 种 Namespace 类型:

    • 0:允许加入任何类型的 namespace;
    • CLONE_NEWCGROUPfd 必须指向一个 cgroup 的 namespace;
    • CLONE_NEWIPCfd 必须指向一个 IPC 的 namespace;
    • CLONE_NEWNETfd 必须指向一个 network 的 namespace;
    • CLONE_NEWNSfd 必须指向一个 mount 的 namespace;
    • CLONE_NEWPIDfd 必须指向一个 pid 的 namespace;
    • CLONE_NEWUSERfd 必须指向一个 user 的 namespace;
    • CLONE_NEWUTSfd 必须指向一个 UTS 的 namespace;

 

/proc 目录与 Namespace

除了 3 个 API 之外,还需要关注 /proc 目录。

自 Linux 3.8 开始,用户就可以在 /proc/[pid]/ns 目录下看到指向不同 namespace 号的文件,如:

1
2
3
4
5
6
7
8
9
10
# ls -l /proc/$$/ns
total 0
lrwxrwxrwx 1 root root 0 Apr 14 13:26 cgroup -> cgroup:[4026531835]
lrwxrwxrwx 1 root root 0 Apr 14 13:26 ipc -> ipc:[4026531839]
lrwxrwxrwx 1 root root 0 Apr 14 13:26 mnt -> mnt:[4026531840]
lrwxrwxrwx 1 root root 0 Apr 14 13:26 net -> net:[4026531993]
lrwxrwxrwx 1 root root 0 Apr 14 13:26 pid -> pid:[4026531836]
lrwxrwxrwx 1 root root 0 Apr 14 13:26 pid_for_children -> pid:[4026531836]
lrwxrwxrwx 1 root root 0 Apr 14 13:26 user -> user:[4026531837]
lrwxrwxrwx 1 root root 0 Apr 14 13:26 uts -> uts:[4026531838]

每一个进程在其对应的 /proc/[pid]/ns 下都有其 namespace 信息,该目录下每一个文件都是一个软链接,setns() 可以通过打开对应进程下的 ns 目录下的软链接文件来加入到对应的 namespace 中。

该目录下的每个软链接的内容其实是一串字符,由 namespace 类型和 inode number 组成:

1
2
# readlink /proc/$$/ns/uts
uts:[4026531838]

且满足以下几个条件:

  • 若几个进程中对应 namespace 软链接内容一致,则这几个进程同属于同一个 namespace;

  • 即使 namespace 中的进程全部终结了,只要其软链接文件一直处于 open 状态,则 namespace 将一直存在;

 

参考:https://zhengyinyong.com/introduction-to-linux-namespace.html