我最近研究的原则涉及,
隔离是通过在派生新子进程时添加各种参数来实现的。
通过在/sys/fs//下各目录下创建目录,指定配额参数,并将pid放入tasks中即可生效。
但为了隔离,我只知道切换了进程的根文件系统目录,但一直没明白具体的实现原理。 我很好奇并且会研究它。
首先我们看一下man手册
DESCRIPTION
pivot_root() changes the root mount in the mount namespace of the
calling process. More precisely, it moves the root mount to the
directory put_old and makes new_root the new root mount. The calling
process must have the CAP_SYS_ADMIN capability in the user namespace
that owns the caller's mount namespace.
pivot_root() changes the root directory and the current working
directory of each process or thread in the same mount namespace to
new_root if they point to the old root directory. (See also NOTES.)
On the other hand, pivot_root() does not change the caller's current
working directory (unless it is on the old root directory), and thus
it should be followed by a chdir("/") call.
The following restrictions apply:
- new_root and put_old must be directories.
- new_root and put_old must not be on the same mount as the current
root.
- put_old must be at or underneath new_root; that is, adding some
nonnegative number of "/.." prefixes to the pathname pointed to by
put_old must yield the same directory as new_root.
- new_root must be a path to a mount point, but can't be "/". A
path that is not already a mount point can be converted into one
by bind mounting the path onto itself.
- The propagation type of the parent mount of new_root and the
parent mount of the current root directory must not be MS_SHARED;
similarly, if put_old is an existing mount point, its propagation
type must not be MS_SHARED. These restrictions ensure that
pivot_root() never propagates any changes to another mount
namespace.
- The current root directory must be a mount point.
更改当前进程所在挂载中所有进程的根挂载,然后将其作为新的根挂载; 当前调用进程的工作目录未修改。 通常需要使用 chdir("/") 切换到新的根挂载点。 根目录。
圆形挂载可以理解为,即“/”。 更改要注意的挂载的“/”。 当前调用进程的工作目录不会改变。 注意,调用前提需要在fork进程时明确指定mount参数。
主要限制:
和 必须是目录且不能在同一个挂载中,或者子目录必须是挂载点,并且不能是当前挂载的“/”。
注意(,),chdir("/")后面是"/"的子目录,可以是
开关和隔离是使用实现的
首先创建一个临时子目录作为临时子目录,然后调用chdir("/")和(".",".")实现切换。
在实际使用中,我发现了一种特殊情况,可以避免使用临时目录,即(".",".")
(".", ".") 使用当前的 "/" 作为 /proc/self/cwd 以避免创建子目录,但在这种情况下,您需要先 (".") 然后 chdir("/")
VS VS
只改变当前进程的“/”
更改当前挂载的“/”
与()类似,但专门用于初始化系统。 不仅会删除旧根下的所有内容并释放内存,而且只能被pid=1的进程使用,不能在其他地方使用。