Linux – How to Move Process Between Namespaces

linuxlxcnamespaceprocess

This question is similar to my question about how to list namespaces. So in addtition, I'd like to know some information about moving processes from one namespace to other? E.g. I have processes of current session in one namespace and some other processes of lxc container in different namespace, so I want to run (e.g. links) in cgroup of that container (it's easily do with cgexec) and then move it to container's namespace , because I have to run this process in container without executing it exact in it. Can it be done or it's impossible in Linux??

Best Answer

You don't need to run process in some control groups if you already in certain namespace, instead you have to manipulate with namespaces. All new process in new namespace will «inherit» all control groups related to that namespace.

Moving processes between different namespaces can be done with setns() function or you can use nsenter command from util-linux to enter new namespace and then run new tasks in it. All you need is to know PID of process, which already is new namespace, then you can use (in case you want to run links):

# nsenter --PID --target pid_in_ns_you_want_to_enter && links

It's some cheat, because you don't moving processes, you just entered in namespace and running new processes, but with this possibility you can enter in certain NS and then fork in it already running in other NS process.

Related Question