1 .\" Copyright (C) 2000 by Werner Almesberger
2 .\" and Copyright (C) 2019 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(GPL_NOVERSION_ONELINE)
5 .\" May be distributed under GPL
8 .\" Written 2000-02-23 by Werner Almesberger
9 .\" Modified 2004-06-17 Michael Kerrisk <mtk.manpages@gmail.com>
11 .TH PIVOT_ROOT 2 2019-08-02 "Linux" "Linux Programmer's Manual"
13 pivot_root \- change the root filesystem
15 .BI "int pivot_root(const char *" new_root ", const char *" put_old );
18 There is no glibc wrapper for this system call; see NOTES.
21 changes the root filesystem in the mount namespace of the calling process.
22 More precisely, it moves the root filesystem to the
23 directory \fIput_old\fP and makes \fInew_root\fP the new root filesystem.
24 The calling process must have the
26 capability in the user namespace that owns the caller's mount namespace.
30 is during system startup, when the
31 system mounts a temporary root filesystem (e.g., an \fBinitrd\fP), then
32 mounts the real root filesystem, and eventually turns the latter into
33 the current root of all relevant processes or threads.
36 may or may not change the current root and the current
37 working directory of any processes or threads that
38 use the old root directory and which are in
39 the same mount namespace as the caller of
43 must ensure that processes with root or current working directory
44 at the old root operate correctly in either case.
45 An easy way to ensure this is to change their
46 root and current working directory to \fInew_root\fP before invoking
49 The paragraph above is intentionally vague because the implementation of
51 may change in the future.
54 changes the root directory and the current working directory
55 of each process or thread in the same mount namespace to
57 if they point to the old root directory.
58 This is necessary in order to prevent kernel threads from keeping the old
59 root directory busy with their root and current working directory,
60 even if they never access
61 the filesystem in any way.
62 In the future, there may be a mechanism for
63 kernel threads to explicitly relinquish any access to the filesystem,
64 such that this fairly intrusive mechanism can be removed from
67 Note that this also applies to the calling process:
69 may or may not affect its current working directory.
70 It is therefore recommended to call
71 \fBchdir("/")\fP immediately after
74 The following restrictions apply to \fInew_root\fP and \fIput_old\fP:
76 They must be directories.
78 \fInew_root\fP and \fIput_old\fP must not be on the same filesystem as
81 \fIput_old\fP must be underneath \fInew_root\fP, that is, adding a nonzero
82 number of \fI/..\fP to the string pointed to by \fIput_old\fP must yield
83 the same directory as \fInew_root\fP.
86 must be a mount point.
87 (If it is not otherwise a mount point, it suffices to bind mount
91 The propagation type of
93 and its parent mount must not be
97 is an existing mount point, its propagation type must not be
102 for additional usage examples.
104 If the current root is not a mount point (e.g., after an earlier
108 then the mount point of the filesystem containing the current root directory
109 (i.e., not the directory itself) is mounted on \fIput_old\fP.
111 On success, zero is returned.
112 On error, \-1 is returned, and
113 \fIerrno\fP is set appropriately.
116 may fail with any of the same errors as
118 Additionally, it may fail with the following errors:
121 \fInew_root\fP or \fIput_old\fP are on the current root filesystem,
122 or a filesystem is already mounted on \fIput_old\fP.
126 is not a mount point.
129 \fIput_old\fP is not underneath \fInew_root\fP.
132 The current root is on the rootfs (initial ramfs) filesystem.
135 Either the mount point at
137 or the parent mount of that mount point,
143 is a mount point and has the propagation type
147 \fInew_root\fP or \fIput_old\fP is not a directory.
150 The calling process does not have the
155 was introduced in Linux 2.3.41.
158 is Linux-specific and hence is not portable.
160 Glibc does not provide a wrapper for this system call; call it using
163 The rootfs (initial ramfs) cannot be
165 The recommended method of changing the root filesystem in this case is
166 to delete everything in rootfs, overmount rootfs with the new root, attach
167 .IR stdin / stdout / stderr
172 Helper programs for this process exist; see
176 should not have to change root and current working directory of other
177 processes in the system.
179 Some of the more obscure uses of
189 .BR mount_namespaces (7),