]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/pivot_root.2
pivot_root.2: srcfix
[thirdparty/man-pages.git] / man2 / pivot_root.2
1 .\" Copyright (C) 2000 by Werner Almesberger
2 .\" and Copyright (C) 2019 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(GPL_NOVERSION_ONELINE)
5 .\" May be distributed under GPL
6 .\" %%%LICENSE_END
7 .\"
8 .\" Written 2000-02-23 by Werner Almesberger
9 .\" Modified 2004-06-17 Michael Kerrisk <mtk.manpages@gmail.com>
10 .\"
11 .TH PIVOT_ROOT 2 2019-08-02 "Linux" "Linux Programmer's Manual"
12 .SH NAME
13 pivot_root \- change the root filesystem
14 .SH SYNOPSIS
15 .BI "int pivot_root(const char *" new_root ", const char *" put_old );
16 .PP
17 .IR Note :
18 There is no glibc wrapper for this system call; see NOTES.
19 .SH DESCRIPTION
20 .BR pivot_root ()
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
25 .B CAP_SYS_ADMIN
26 capability in the user namespace that owns the caller's mount namespace.
27 .PP
28 The typical use of
29 .BR pivot_root ()
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.
34 .PP
35 .BR pivot_root ()
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
40 .BR pivot_root ().
41 The caller of
42 .BR pivot_root ()
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
47 .BR pivot_root ().
48 .PP
49 The paragraph above is intentionally vague because the implementation of
50 .BR pivot_root ()
51 may change in the future.
52 Currently,
53 .BR pivot_root ()
54 changes the root directory and the current working directory
55 of each process or thread in the same mount namespace to
56 .I new_root
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
65 .BR pivot_root ().
66 .PP
67 Note that this also applies to the calling process:
68 .BR pivot_root ()
69 may or may not affect its current working directory.
70 It is therefore recommended to call
71 \fBchdir("/")\fP immediately after
72 .BR pivot_root ().
73 .PP
74 The following restrictions apply to \fInew_root\fP and \fIput_old\fP:
75 .IP \- 3
76 They must be directories.
77 .IP \-
78 \fInew_root\fP and \fIput_old\fP must not be on the same filesystem as
79 the current root.
80 .IP \-
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.
84 .IP \-
85 .I new_root
86 must be a mount point.
87 (If it is not otherwise a mount point, it suffices to bind mount
88 .I new_root
89 on top of itself.)
90 .IP \-
91 The propagation type of
92 .I new_root
93 and its parent mount must not be
94 .BR MS_SHARED ;
95 similarly, if
96 .I put_old
97 is an existing mount point, its propagation type must not be
98 .BR MS_SHARED .
99 .PP
100 See also
101 .BR pivot_root (8)
102 for additional usage examples.
103 .PP
104 If the current root is not a mount point (e.g., after an earlier
105 .BR chroot (2)
106 or
107 .BR pivot_root ()),
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.
110 .SH RETURN VALUE
111 On success, zero is returned.
112 On error, \-1 is returned, and
113 \fIerrno\fP is set appropriately.
114 .SH ERRORS
115 .BR pivot_root ()
116 may fail with any of the same errors as
117 .BR stat (2).
118 Additionally, it may fail with the following errors:
119 .TP
120 .B EBUSY
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.
123 .TP
124 .B EINVAL
125 .I new_root
126 is not a mount point.
127 .TP
128 .B EINVAL
129 \fIput_old\fP is not underneath \fInew_root\fP.
130 .TP
131 .B EINVAL
132 The current root is on the rootfs (initial ramfs) filesystem.
133 .TP
134 .B EINVAL
135 Either the mount point at
136 .IR new_root ,
137 or the parent mount of that mount point,
138 has propagation type
139 .BR MS_SHARED .
140 .TP
141 .B EINVAL
142 .I put_old
143 is a mount point and has the propagation type
144 .BR MS_SHARED .
145 .TP
146 .B ENOTDIR
147 \fInew_root\fP or \fIput_old\fP is not a directory.
148 .TP
149 .B EPERM
150 The calling process does not have the
151 .B CAP_SYS_ADMIN
152 capability.
153 .SH VERSIONS
154 .BR pivot_root ()
155 was introduced in Linux 2.3.41.
156 .SH CONFORMING TO
157 .BR pivot_root ()
158 is Linux-specific and hence is not portable.
159 .SH NOTES
160 Glibc does not provide a wrapper for this system call; call it using
161 .BR syscall (2).
162 .PP
163 The rootfs (initial ramfs) cannot be
164 .BR pivot_root ()ed.
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
168 to the new
169 .IR /dev/console ,
170 and exec the new
171 .BR init (1).
172 Helper programs for this process exist; see
173 .BR switch_root (8).
174 .SH BUGS
175 .BR pivot_root ()
176 should not have to change root and current working directory of other
177 processes in the system.
178 .PP
179 Some of the more obscure uses of
180 .BR pivot_root ()
181 may quickly lead to
182 insanity.
183 .SH SEE ALSO
184 .BR chdir (2),
185 .BR chroot (2),
186 .BR mount (2),
187 .BR stat (2),
188 .BR initrd (4),
189 .BR mount_namespaces (7),
190 .BR pivot_root (8),
191 .BR switch_root (8)