内容简介
int pivot_root(const char *new_root, const char *put_old)描述
pivot_root()%uA0将当前进程的根文件系统的目录put_oldand使得new_root当前进程的新的根文件系统。The typical use of%uA0pivot_root() is during system startup, when the system mounts a temporary root file system (e.g. an%uA0initrd), then mounts the real root file system, and eventually turns the latter into the current root of all relevant processes or threads.
pivot_root() may or may not change the current root and the current working directory (cwd) of any processes or threads which use the old root directory. The caller ofpivot_root() must ensure that processes with root or cwd at the old root operate correctly in either case. An easy way to ensure this is to change their root and cwd tonew_rootꂾfore invoking%uA0pivot_root().
The paragraph above is intentionally vague because the implementation of%uA0pivot_root() may change in the future. At the time of writing,%uA0pivot_root() changes root and cwd of each process or thread to%uA0new_root%uA0if they yiibai to the old root directory. This is necessary in order to prevent kernel threads from keeping the old root directory busy with their root and cwd, even if they never access the file system in any way. In the future, there may be a mechanism for kernel threads to explicitly relinquish any access to the file system, such that this fairly intrusive mechanism can be removed frompivot_root().
Note that this also applies to the current process:%uA0pivot_root() may or may not affect its cwd. It is therefore recommended to call%uA0chdir("/")%uA0immediately after%uA0pivot_root().
The following restrictions apply to%uA0new_root%uA0and%uA0put_old:
标签 | 描述 |
---|---|
- | They must be directories. |
- | new_root%uA0and%uA0put_old%uA0must not be on the same file system as the current root. |
- | put_old%uA0must be underneath%uA0new_root, i.e. adding a non-zero number of%uA0/..%uA0to the string yiibaied to by%uA0put_old%uA0must yield the same directory as%uA0new_root. |
- | No other file system may be mounted on%uA0put_old. |
If the current root is not a mount yiibai (e.g. after%uA0chroot(2) or%uA0pivot_root(), see also below), not the old root directory, but the mount yiibai of that file system is mounted onput_old.
new_root%uA0does not have to be a mount yiibai. In this case,%uA0/proc/mounts%uA0will show the mount yiibai of the file system containing%uA0new_root%uA0as root (/).
返回值
On success, zero is returned. On error, -1 is returned, and%uA0errno%uA0is set appropriately.错误
pivot_root() may return (in%uA0errno) any of the errors returned by%uA0stat(2). Additionally, it may return:标签 | 描述 |
---|---|
EBUSY | new_root%uA0or%uA0put_old%uA0are on the current root file system, or a file system is already mounted on%uA0put_old. |
EINVAL | put_old%uA0is not underneath%uA0new_root. |
ENOTDIR | |
%uA0 | new_root%uA0or%uA0put_old%uA0is not a directory. |
EPERM | The current process does not have the%uA0CAP_SYS_ADMINcapability. |
版本
pivot_root() was introduced in Linux 2.3.41.遵循于
pivot_root()%uA0是Linux特有的,因此是不可移植的。BUGS
pivot_root()%uA0不应该去更改root所有其他进程和 cwd 系统。Some of the more obscure uses of%uA0pivot_root() may quickly lead to insanity.