linux - 被mount系统调用中的source和target关键字搞糊涂了

标签 linux mount linux-namespaces

我正在实现一个容器,该容器克隆了一个新的命名空间,包括挂载、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/

相关文章:

php - 不允许 chmod() 操作 - FatFree 框架

virtual-machine - 修改虚拟 secret 码

c - 如何从单个流程实例创建多个网络 namespace

linux - 'none' 中的 "mount -t cgroup -o cpuset none/cpuset"是什么意思

c - umount 不适用于 C 中的设备(但可以在终端中使用)

linux - 分离父进程的终端

linux - 如何在 Linux 中列出所有命名空间中的所有网络链接

linux - 使 bash printf 将数组视为一个参数

c - 为什么这段代码可以获取到环境变量地址呢?

php - 如何从 CentOS 7 上安装的 PHP 7、Plesk 12.5 连接 MSSQL