我正在实现一个容器,该容器克隆了一个新的命名空间,包括挂载、pid、用户命名空间等。 child 做的第一步是挂载几个重要的点,例如 /proc
、/sys
和 /tmp
使用 mount
系统调用。
if(::mount("proc", "/proc", "proc", 0, NULL)==-1) {
printf("Failed on mount: %s\n", strerror(errno));
return -1;
}
if(::mount("sysfs", "/sys", "sysfs", 0, NULL)==-1) {
printf("Failed on mount: %s\n", strerror(errno));
return -1;
}
if(::mount("tmp", "/tmp", "tmpfs", 0, NULL)==-1) {
printf("Failed on mount: %s\n", strerror(errno));
return -1;
}
但是,我对传递给 mount
的参数列表中的 source
字段感到有点困惑。
int mount(const char *source, const char *target,
const char *filesystemtype, unsigned long mountflags,
const void *data);
来源到底是什么意思?例如,挂载/tmp
似乎与源字符字符串无关。即使使用 ::mount(nullptr, "/tmp", "tmpfs", 0, NULL)
,我仍然可以看到在新命名空间下创建了一个新的 /tmp
文件夹。我错过了什么吗?
最佳答案
它只是应该与您的 /etc/fstab
文件中提供的参数相匹配。例如,在我的 fstab 上我有:
# <file system> <mount point> <type> <options> <dump> <pass>
...
proc /proc proc defaults 0 0
sysfs /sys sysfs defaults 0 0
但是这些例子有点不同,因为它们的性质。实际上,proc 和 sysfs 都不是通用文件系统。因此,如果您安装了硬盘驱动器,则源代码会更直接,例如 /dev/sda1
。
并且因为您在命名空间之上实现隔离,所以请注意容器是否在 /proc
上调用 umount
。它可能会揭示宿主的进程,从而打破隔离。
关于linux - 被mount系统调用中的source和target关键字搞糊涂了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40900272/