]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/pivot_root.2
pivot_root.2: pivot_root() affects only other processes in the same mount namespace
[thirdparty/man-pages.git] / man2 / pivot_root.2
1 .\" Copyright (C) 2000 by Werner Almesberger
2 .\"
3 .\" %%%LICENSE_START(GPL_NOVERSION_ONELINE)
4 .\" May be distributed under GPL
5 .\" %%%LICENSE_END
6 .\"
7 .\" Written 2000-02-23 by Werner Almesberger
8 .\" Modified 2004-06-17 Michael Kerrisk <mtk.manpages@gmail.com>
9 .\"
10 .TH PIVOT_ROOT 2 2019-08-02 "Linux" "Linux Programmer's Manual"
11 .SH NAME
12 pivot_root \- change the root filesystem
13 .SH SYNOPSIS
14 .BI "int pivot_root(const char *" new_root ", const char *" put_old );
15 .PP
16 .IR Note :
17 There is no glibc wrapper for this system call; see NOTES.
18 .SH DESCRIPTION
19 .BR pivot_root ()
20 changes the root filesystem in the mount namespace of the calling process.
21 More precisely, it moves the root filesystem to the
22 directory \fIput_old\fP and makes \fInew_root\fP the new root filesystem.
23 The calling process must have the
24 .B CAP_SYS_ADMIN
25 capability in the user namespace that owns the caller's mount namespace.
26 .PP
27 The typical use of
28 .BR pivot_root ()
29 is during system startup, when the
30 system mounts a temporary root filesystem (e.g., an \fBinitrd\fP), then
31 mounts the real root filesystem, and eventually turns the latter into
32 the current root of all relevant processes or threads.
33 .PP
34 .BR pivot_root ()
35 may or may not change the current root and the current
36 working directory of any processes or threads that
37 use the old root directory and which are in
38 the same mount namespace as the caller of
39 .BR pivot_root ().
40 The caller of
41 .BR pivot_root ()
42 must ensure that processes with root or current working directory
43 at the old root operate correctly in either case.
44 An easy way to ensure this is to change their
45 root and current working directory to \fInew_root\fP before invoking
46 .BR pivot_root ().
47 .PP
48 The paragraph above is intentionally vague because the implementation of
49 .BR pivot_root ()
50 may change in the future.
51 Currently,
52 .BR pivot_root ()
53 changes the root directory and the current working directory
54 of each process or thread in the same mount namespace to
55 .I new_root
56 if they point to the old root directory.
57 This is necessary in order to prevent kernel threads from keeping the old
58 root directory busy with their root and current working directory,
59 even if they never access
60 the filesystem in any way.
61 In the future, there may be a mechanism for
62 kernel threads to explicitly relinquish any access to the filesystem,
63 such that this fairly intrusive mechanism can be removed from
64 .BR pivot_root ().
65 .PP
66 Note that this also applies to the calling process:
67 .BR pivot_root ()
68 may or may not affect its current working directory.
69 It is therefore recommended to call
70 \fBchdir("/")\fP immediately after
71 .BR pivot_root ().
72 .PP
73 The following restrictions apply to \fInew_root\fP and \fIput_old\fP:
74 .IP \- 3
75 They must be directories.
76 .IP \- 3
77 \fInew_root\fP and \fIput_old\fP must not be on the same filesystem as
78 the current root.
79 .IP \- 3
80 \fIput_old\fP must be underneath \fInew_root\fP, that is, adding a nonzero
81 number of \fI/..\fP to the string pointed to by \fIput_old\fP must yield
82 the same directory as \fInew_root\fP.
83 .IP \- 3
84 .I new_root
85 must be a mount point.
86 (If it is not otherwise a mount point, it suffices to bind mount
87 .I new_root
88 on top of itself.)
89 .IP \- 3
90 The propagation type of
91 .I new_root
92 and its parent mount must not be
93 .BR MS_SHARED ;
94 similarly, if
95 .I put_old
96 is an existing mount point, its propagation type must not be
97 .BR MS_SHARED .
98 .IP \- 3
99 No other filesystem may be mounted on \fIput_old\fP.
100 .PP
101 See also
102 .BR pivot_root (8)
103 for additional usage examples.
104 .PP
105 If the current root is not a mount point (e.g., after an earlier
106 .BR chroot (2)
107 or
108 .BR pivot_root ()),
109 then the mount point of the filesystem containing the current root directory
110 (i.e., not the directory itself) is mounted on \fIput_old\fP.
111 .SH RETURN VALUE
112 On success, zero is returned.
113 On error, \-1 is returned, and
114 \fIerrno\fP is set appropriately.
115 .SH ERRORS
116 .BR pivot_root ()
117 may fail with any of the same errors as
118 .BR stat (2).
119 Additionally, it may fail with the following errors:
120 .TP
121 .B EBUSY
122 \fInew_root\fP or \fIput_old\fP are on the current root filesystem,
123 or a filesystem is already mounted on \fIput_old\fP.
124 .TP
125 .B EINVAL
126 .I new_root
127 is not a mount point.
128 .TP
129 .B EINVAL
130 \fIput_old\fP is not underneath \fInew_root\fP.
131 .TP
132 .B EINVAL
133 The current root is on the rootfs (initial ramfs) filesystem.
134 .TP
135 .B EINVAL
136 Either the mount point at
137 .IR new_root ,
138 or the parent mount of that mount point,
139 has propagation type
140 .BR MS_SHARED .
141 .TP
142 .B EINVAL
143 .I put_old
144 is a mount point and has the propagation type
145 .BR MS_SHARED .
146 .TP
147 .B ENOTDIR
148 \fInew_root\fP or \fIput_old\fP is not a directory.
149 .TP
150 .B EPERM
151 The calling process does not have the
152 .B CAP_SYS_ADMIN
153 capability.
154 .SH VERSIONS
155 .BR pivot_root ()
156 was introduced in Linux 2.3.41.
157 .SH CONFORMING TO
158 .BR pivot_root ()
159 is Linux-specific and hence is not portable.
160 .SH NOTES
161 Glibc does not provide a wrapper for this system call; call it using
162 .BR syscall (2).
163 .PP
164 The rootfs (initial ramfs) cannot be
165 .BR pivot_root ()ed.
166 The recommended method of changing the root filesystem in this case is
167 to delete everything in rootfs, overmount rootfs with the new root, attach
168 .IR stdin / stdout / stderr
169 to the new
170 .IR /dev/console ,
171 and exec the new
172 .BR init (1).
173 Helper programs for this process exist; see
174 .BR switch_root (8).
175 .SH BUGS
176 .BR pivot_root ()
177 should not have to change root and current working directory of other
178 processes in the system.
179 .PP
180 Some of the more obscure uses of
181 .BR pivot_root ()
182 may quickly lead to
183 insanity.
184 .SH SEE ALSO
185 .BR chdir (2),
186 .BR chroot (2),
187 .BR mount (2),
188 .BR stat (2),
189 .BR initrd (4),
190 .BR mount_namespaces (7),
191 .BR pivot_root (8),
192 .BR switch_root (8)