pivot_root()函式 Unix/Linux


pivot_root - 更改root檔案系統

內容簡介

int pivot_root(const char *new_root, const char *put_old);

描述

pivot_root() 將當前進程的根檔案系統的目錄put_oldand使得new_root當前進程的新的根檔案系統。

The typical use of pivot_root() is during system startup, when the system mounts a temporary root file system (e.g. an initrd), 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 before invoking pivot_root().

The paragraph above is intentionally vague because the implementation of pivot_root() may change in the future. At the time of writing, pivot_root() changes root and cwd of each process or thread to new_root if 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: pivot_root() may or may not affect its cwd. It is therefore recommended to call chdir("/") immediately after pivot_root().

The following restrictions apply to new_root and put_old:

標籤 描述
- They must be directories.
- new_root and put_old must not be on the same file system as the current root.
- put_old must be underneath new_root, i.e. adding a non-zero number of /.. to the string yiibaied to by put_old must yield the same directory as new_root.
- No other file system may be mounted on put_old.
See also pivot_root(8) for additional usage examples.

If the current root is not a mount yiibai (e.g. after chroot(2) or pivot_root(), see also below), not the old root directory, but the mount yiibai of that file system is mounted onput_old.

new_root does not have to be a mount yiibai. In this case, /proc/mounts will show the mount yiibai of the file system containing new_root as root (/).

返回值

On success, zero is returned. On error, -1 is returned, and errno is set appropriately.

錯誤

pivot_root() may return (in errno) any of the errors returned by stat(2). Additionally, it may return:
標籤 描述
EBUSY new_root or put_old are on the current root file system, or a file system is already mounted on put_old.
EINVAL put_old is not underneath new_root.
ENOTDIR
  new_root or put_old is not a directory.
EPERM The current process does not have the CAP_SYS_ADMINcapability.

版本

pivot_root() was introduced in Linux 2.3.41.

遵循於

pivot_root() 是Linux特有的,因此是不可移植的。

BUGS

pivot_root() 不應該去更改root所有其他進程和 cwd 系統。

Some of the more obscure uses of pivot_root() may quickly lead to insanity.

注意

Glibc does not provide a wrapper for this system call; call it using syscall(2).

另請參閱