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??
1 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.
-
3Is there anything that allows calling
setns
from bash, and moving a particular process by their ID to a namespace? Commented Aug 25, 2015 at 12:08 -
setns
seems to be completely useless: "A multithreaded process may not change user namespace with setns()", "A process may not be reassociated with a new mount namespace if it is multithreaded"– NavinCommented Mar 13, 2018 at 7:04