1PIVOT_ROOT(2)              Linux Programmer's Manual             PIVOT_ROOT(2)
2
3
4

NAME

6       pivot_root - change the root filesystem
7

SYNOPSIS

9       int pivot_root(const char *new_root, const char *put_old);
10
11       Note: There is no glibc wrapper for this system call; see NOTES.
12

DESCRIPTION

14       pivot_root()  moves  the  root filesystem of the calling process to the
15       directory put_old and makes new_root the new  root  filesystem  of  the
16       calling process.
17
18       The typical use of pivot_root() is during system startup, when the sys‐
19       tem mounts a temporary root filesystem (e.g., an initrd),  then  mounts
20       the real root filesystem, and eventually turns the latter into the cur‐
21       rent root of all relevant processes or threads.
22
23       pivot_root() may or may not change the current  root  and  the  current
24       working  directory  of  any processes or threads which use the old root
25       directory.  The caller of pivot_root() must ensure that processes  with
26       root  or current working directory at the old root operate correctly in
27       either case.  An easy way to ensure this is to change  their  root  and
28       current working directory to new_root before invoking pivot_root().
29
30       The  paragraph  above is intentionally vague because the implementation
31       of pivot_root() may change in the future.   At  the  time  of  writing,
32       pivot_root() changes root and current working directory of each process
33       or thread to new_root if they point to the old root directory.  This is
34       necessary  in order to prevent kernel threads from keeping the old root
35       directory busy with their root and current working directory,  even  if
36       they  never access the filesystem in any way.  In the future, there may
37       be a mechanism for kernel threads to explicitly relinquish  any  access
38       to  the  filesystem,  such  that this fairly intrusive mechanism can be
39       removed from pivot_root().
40
41       Note that this also applies to the calling process: pivot_root() may or
42       may  not  affect its current working directory.  It is therefore recom‐
43       mended to call chdir("/") immediately after pivot_root().
44
45       The following restrictions apply to new_root and put_old:
46
47       -  They must be directories.
48
49       -  new_root and put_old must not be on the same filesystem as the  cur‐
50          rent root.
51
52       -  put_old  must be underneath new_root, that is, adding a nonzero num‐
53          ber of /.. to the string pointed to by put_old must yield  the  same
54          directory as new_root.
55
56       -  No other filesystem may be mounted on put_old.
57
58       See also pivot_root(8) for additional usage examples.
59
60       If  the  current  root  is  not a mount point (e.g., after chroot(2) or
61       pivot_root(), see also below), not the  old  root  directory,  but  the
62       mount point of that filesystem is mounted on put_old.
63
64       new_root must be a mount point.  (If it is not otherwise a mount point,
65       it suffices to bind mount new_root on top of itself.)
66
67       The propagation type of new_root and  its  parent  mount  must  not  be
68       MS_SHARED; similarly, if put_old is an existing mount point, its propa‐
69       gation type must not be MS_SHARED.
70

RETURN VALUE

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

ERRORS

76       pivot_root()  may  return  (in  errno)  any  of  the errors returned by
77       stat(2).  Additionally, it may return:
78
79       EBUSY  new_root or put_old are on the current  root  filesystem,  or  a
80              filesystem is already mounted on put_old.
81
82       EINVAL new_root is not a mount point.
83
84       EINVAL put_old is not underneath new_root.
85
86       EINVAL The current root is on the rootfs (initial ramfs) filesystem.
87
88       EINVAL Either  the mount point at new_root, or the parent mount of that
89              mount point, has propagation type MS_SHARED.
90
91       EINVAL put_old is a mount point and has the propagation type MS_SHARED.
92
93       ENOTDIR
94              new_root or put_old is not a directory.
95
96       EPERM  The calling process does not have the CAP_SYS_ADMIN capability.
97

VERSIONS

99       pivot_root() was introduced in Linux 2.3.41.
100

CONFORMING TO

102       pivot_root() is Linux-specific and hence is not portable.
103

NOTES

105       Glibc does not provide a wrapper for this system call;  call  it  using
106       syscall(2).
107
108       The  rootfs  (initial ramfs) cannot be pivot_root()ed.  The recommended
109       method of changing the root filesystem in this case is to delete every‐
110       thing  in rootfs, overmount rootfs with the new root, attach stdin/std‐
111       out/stderr to the new /dev/console, and exec the new  init(1).   Helper
112       programs for this process exist; see switch_root(8).
113

BUGS

115       pivot_root()  should not have to change root and current working direc‐
116       tory of all other processes in the system.
117
118       Some of the more obscure uses  of  pivot_root()  may  quickly  lead  to
119       insanity.
120

SEE ALSO

122       chdir(2),   chroot(2),  mount(2),  stat(2),  initrd(4),  pivot_root(8),
123       switch_root(8)
124

COLOPHON

126       This page is part of release 5.02 of the Linux  man-pages  project.   A
127       description  of  the project, information about reporting bugs, and the
128       latest    version    of    this    page,    can     be     found     at
129       https://www.kernel.org/doc/man-pages/.
130
131
132
133Linux                             2019-08-02                     PIVOT_ROOT(2)
Impressum