如何在/proc/1/ns/{ns}中检查设备的设备号?
我已经阅读了Go库的代码(请参阅下面的代码),其中指出,可以确定容器是否位于主机名称空间中:未命名空间的/proc/1/ns/{ns}的设备编号为4,任何其他的都更高。
现在,在没有用户名称空间或cgroup的新Debian容器中,我运行以下命令:
root@54d74f795843:/# ls -la /proc/1/ns
total 0
dr-x--x--x 2 root root 0 Feb 29 17:18 .
dr-xr-xr-x 9 root root 0 Feb 29 17:18 ..
lrwxrwxrwx 1 root root 0 Feb 29 17:18 cgroup -> 'cgroup:[4026531835]'
lrwxrwxrwx 1 root root 0 Feb 29 17:18 ipc -> 'ipc:[4026532290]'
lrwxrwxrwx 1 root root 0 Feb 29 17:18 mnt -> 'mnt:[4026532288]'
lrwxrwxrwx 1 root root 0 Feb 29 17:18 net -> 'net:[4026532293]'
lrwxrwxrwx 1 root root 0 Feb 29 17:18 pid -> 'pid:[4026532291]'
lrwxrwxrwx 1 root root 0 Feb 29 17:18 user -> 'user:[4026531837]'
lrwxrwxrwx 1 root root 0 Feb 29 17:18 uts -> 'uts:[4026532289]''user:[4026531837]'中的4026531837在这里意味着什么?我不能成为设备号,因为容器使用与主机相同的用户名称空间(我已经验证了这一点)。
/proc/1/ns/{ns}文件的设备编号?ls -la命令显示这些文件是符号链接,那么它们如何才能有设备号呢?amicontained/vendor/github.com/jessfraz/bpfd/proc/proc.go/
// HasNamespace determines if a container is using a particular namespace or the
// host namespace.
// The device number of an unnamespaced /proc/1/ns/{ns} is 4 and anything else is
// higher.
// Only works from inside a container.
func HasNamespace(ns string) (bool, error) {
file := fmt.Sprintf("/proc/1/ns/%s", ns)
// Use Lstat to not follow the symlink.
var info syscall.Stat_t
if err := syscall.Lstat(file, &info); err != nil {
return false, &os.PathError{Op: "lstat", Path: file, Err: err}
}
// Get the device number. If it is higher than 4 it is in a namespace.
if info.Dev > 4 {
return true, nil
}
return false, nil
}发布于 2020-02-29 17:58:38
在这里,'user:4026531837‘中的4026531837是什么意思?
这些编号是由nsfs文件系统实现的inode文件号,可以打开该文件系统并与setns(2)一起使用它将进程与命名空间相关联。
如何列出文件/proc/1/ns/{ns}的设备编号?
根据系统问题讨论(4.14+通过/proc/1/sched进行虚拟化检测不再适用于Linux):
在我可以测试的系统上(内核为Arch,内核为4.15.1,Debian,内核为4.9.65,Ubuntu,内核为4.13.0),未命名空间的
/proc/1/ns/pid的设备编号似乎总是为4,而在PID命名空间中,这是一个不同的、更高的数字,显然与PID的数量有关。名称空间。您可以使用以下命令进行尝试: stat --格式化%d /proc/1/ns/pid 4 sudo unshare --pid -叉-挂载-proc stat --格式%d /proc/1/ns/pid 36
https://unix.stackexchange.com/questions/570418
复制相似问题