在 Linux 中查看/操作挂载命名空间

hoo*_*enz 8 linux mount namespace

有没有办法查看或操作任意进程的挂载命名空间?

例如,正在运行的 docker 容器在本地挂载到 NFS 服务器。从容器内部可以看到,但在外部,主机不知道。使用网络命名空间,这是可行的。例如管道

但是,对于挂载命名空间,我对此一无所知。是否有公开的 API 或 sysfs 层来查看这些挂载并操作或创建新的挂载?

mik*_*erv 6

是的。你可以看看它/proc/$PID/mountinfo,否则你可以使用findmnt -N开关 - 关于它findmnt --help说:

  • -N, --task <tid>
    • 使用替代命名空间(/proc/<tid>/mountinfo文件)

findmnt还跟踪PROPAGATION标志,这是一个mountinfo准确报告此信息的字段 - 哪些进程共享哪些安装。

此外,您始终可以使用nsenter您喜欢的任何类型的命名空间 - 当然,前提是您拥有正确的权限。

 nsenter --help
Usage:
 nsenter [options] <program> [args...]

Options:
 -t, --target <pid>     target process to get namespaces from
 -m, --mount [=<file>]  enter mount namespace
 -u, --uts   [=<file>]  enter UTS namespace (hostname etc)
 -i, --ipc   [=<file>]  enter System V IPC namespace
 -n, --net   [=<file>]  enter network namespace
 -p, --pid   [=<file>]  enter pid namespace
 -U, --user  [=<file>]  enter user namespace
 -S, --setuid <uid>     set uid in user namespace
 -G, --setgid <gid>     set gid in user namespace
 -r, --root  [=<dir>]   set the root directory
 -w, --wd    [=<dir>]   set the working directory
 -F, --no-fork          do not fork before exec'ing <program>

 -h, --help     display this help and exit
 -V, --version  output version information and exit

For more details see nsenter(1).
Run Code Online (Sandbox Code Playgroud)