2016-10-05 89 views
2

)mount()我对这里发生的事情有点困惑。我正在关注一个guide,在设置了CLONE_NEWNS标志后调用克隆后添加了一个新的挂载点。挂载点应该只存在于子进程中。我试图改变新的文件系统命名空间,它似乎影响父亲。克隆()后CLONE_NEWNS设置效果父(

我的c程序很简单。主将调用克隆

pid_t pid = clone(child_exec,c_stack, SIGCHLD | CLONE_NEWNS | CLONE_NEWPID ,args); 

args是一个聊天数组,其中包含一个exec命令。

int child_exec(void *arg) 
{ 
     int err =0; 
     char **commands = (char **)arg; 
     mount("none", "/mytmp", "tmpfs", 0, "");  
     execvp(commands[0],commands); 
     return 0; 
} 

如果传递给execvp命令是mount我希望输出遏制/程序退出不看/ mytmp上来后mytmp挂载点并再次运行该命令mount。这没有发生。当我调用execvp时,以及在运行mount时,我会在外面看到它。

我试着用MS_PRIVATE标志安装和使用unshare(CLONE_FS);

我也有过类似的问题,我想从子进程卸载/ proc和一个获得资源是忙中出错。我认为这不应该发生在一个新的命名空间。

+0

没有在这个问题上没有任何问题。你只是描述发生的事情,而不是问任何事情。 – immibis

+0

不清楚吗?我试图操纵新的文件系统命名空间,而不影响父。 – Matthew

+0

你很清楚你在做什么。目前还不清楚你问的是什么问题,因为你没有提出问题。 – immibis

回答

1

这对我来说有两个问题。

首先看起来像我使用的Ubuntu(16.04.1 LTS)或util-linux软件包共享/ mount命名空间,CLONE_NEWNS传播该设置。我/ mount被分享。我在/ proc/self/mountinfo和/ proc/1/mountinfo中验证了这一点。我试过sudo mount --make-private -o remount /从这个答案和升级包提到。 https://unix.stackexchange.com/questions/246312/why-is-my-bind-mount-visible-outside-its-mount-namespace。这使我能够在父命名空间没有任何影响的情况下进行额外的挂载。

第二个问题是卸载/ proc。这不起作用,因为我的系统安装了两次,即/proc/sys/fs/binfmt_misc。这里的讨论激励我去检查。 http://linux-kernel.vger.kernel.narkive.com/aVUicig1/umount-proc-after-clone-newns-in-2-6-25

我最后child_exec代码最终被

int child_exec(void *arg) 
{ 
     int err =0; 
     char **commands = (char **)arg; 
     printf("child...%s\n",commands[0]); 
//  if(unshare(CLONE_NEWNS) <0) 
//    printf("unshare issue?\n"); 
     if (umount("/proc/sys/fs/binfmt_misc") <0) 
       printf("error unmount bin: %s\n",strerror(errno)); 
     if (umount("/proc/sys/fs/binfmt_misc") <0) 
       printf("error unmount bin: %s\n",strerror(errno)); 
     if (umount("/proc") <0) 
       printf("error unmount: %s\n",strerror(errno)); 
     if (mount("proc", "/proc", "proc",0, NULL) <0) 
       printf("error mount: %s\n",strerror(errno)); 
     execvp(commands[0],commands); 
     return 0; 
}