]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/pivot_root.2
e1a5b927f4f9ac0aceed82ed3ff8fb29f7a460c2
[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-03-06 "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 moves the root filesystem of the calling process to the
21 directory \fIput_old\fP and makes \fInew_root\fP the new root filesystem
22 of the calling process.
23 .\"
24 .\" The
25 .\" .B CAP_SYS_ADMIN
26 .\" capability is required.
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 which use the old
38 root directory.
39 The caller of
40 .BR pivot_root ()
41 must ensure that processes with root or current working directory
42 at the old root operate correctly in either case.
43 An easy way to ensure this is to change their
44 root and current working directory to \fInew_root\fP before invoking
45 .BR pivot_root ().
46 .PP
47 The paragraph above is intentionally vague because the implementation of
48 .BR pivot_root ()
49 may change in the future.
50 At the time of writing,
51 .BR pivot_root ()
52 changes root and current working directory of each process or
53 thread to \fInew_root\fP if they point to the old root directory.
54 This is necessary in order to prevent kernel threads from keeping the old
55 root directory busy with their root and current working directory,
56 even if they never access
57 the filesystem in any way.
58 In the future, there may be a mechanism for
59 kernel threads to explicitly relinquish any access to the filesystem,
60 such that this fairly intrusive mechanism can be removed from
61 .BR pivot_root ().
62 .PP
63 Note that this also applies to the calling process:
64 .BR pivot_root ()
65 may or may not affect its current working directory.
66 It is therefore recommended to call
67 \fBchdir("/")\fP immediately after
68 .BR pivot_root ().
69 .PP
70 The following restrictions apply to \fInew_root\fP and \fIput_old\fP:
71 .IP \- 3
72 They must be directories.
73 .IP \- 3
74 \fInew_root\fP and \fIput_old\fP must not be on the same filesystem as
75 the current root.
76 .IP \- 3
77 \fIput_old\fP must be underneath \fInew_root\fP, that is, adding a nonzero
78 number of \fI/..\fP to the string pointed to by \fIput_old\fP must yield
79 the same directory as \fInew_root\fP.
80 .IP \- 3
81 No other filesystem may be mounted on \fIput_old\fP.
82 .PP
83 See also
84 .BR pivot_root (8)
85 for additional usage examples.
86 .PP
87 If the current root is not a mount point (e.g., after
88 .BR chroot (2)
89 or
90 .BR pivot_root (),
91 see also below), not the old root directory, but the
92 mount point of that filesystem is mounted on \fIput_old\fP.
93 .PP
94 .I new_root
95 must be a mount point.
96 (If it is not otherwise a mount point, it suffices to bind mount
97 .I new_root
98 on top of itself.)
99 .PP
100 The propagation type of
101 .I new_root
102 and its parent mount must not be
103 .BR MS_SHARED ;
104 similarly, if
105 .I put_old
106 is an existing mount point, its propagation type must not be
107 .BR MS_SHARED .
108 .SH RETURN VALUE
109 On success, zero is returned.
110 On error, \-1 is returned, and
111 \fIerrno\fP is set appropriately.
112 .SH ERRORS
113 .BR pivot_root ()
114 may return (in \fIerrno\fP) any of the errors returned by
115 .BR stat (2).
116 Additionally, it may return:
117 .TP
118 .B EBUSY
119 \fInew_root\fP or \fIput_old\fP are on the current root filesystem,
120 or a filesystem is already mounted on \fIput_old\fP.
121 .TP
122 .B EINVAL
123 .I new_root
124 is not a mount point.
125 .TP
126 .B EINVAL
127 \fIput_old\fP is not underneath \fInew_root\fP.
128 .TP
129 .B EINVAL
130 The current root is on the rootfs (initial ramfs) filesystem.
131 .TP
132 .B EINVAL
133 Either the mount point at
134 .IR new_root ,
135 or the parent mount of that mount point,
136 has propagation type
137 .BR MS_SHARED .
138 .TP
139 .B EINVAL
140 .I put_old
141 is a mount point and has the propagation type
142 .BR MS_SHARED .
143 .TP
144 .B ENOTDIR
145 \fInew_root\fP or \fIput_old\fP is not a directory.
146 .TP
147 .B EPERM
148 The calling process does not have the
149 .B CAP_SYS_ADMIN
150 capability.
151 .SH VERSIONS
152 .BR pivot_root ()
153 was introduced in Linux 2.3.41.
154 .SH CONFORMING TO
155 .BR pivot_root ()
156 is Linux-specific and hence is not portable.
157 .SH NOTES
158 Glibc does not provide a wrapper for this system call; call it using
159 .BR syscall (2).
160 .PP
161 The rootfs (initial ramfs) cannot be
162 .BR pivot_root ()ed.
163 The recommended method of changing the root filesystem in this case is
164 to delete everything in rootfs, overmount rootfs with the new root, attach
165 .IR stdin / stdout / stderr
166 to the new
167 .IR /dev/console ,
168 and exec the new
169 .BR init (1).
170 Helper programs for this process exist; see
171 .BR switch_root (8).
172 .SH BUGS
173 .BR pivot_root ()
174 should not have to change root and current working directory of all other
175 processes in the system.
176 .PP
177 Some of the more obscure uses of
178 .BR pivot_root ()
179 may quickly lead to
180 insanity.
181 .SH SEE ALSO
182 .BR chdir (2),
183 .BR chroot (2),
184 .BR mount (2),
185 .BR stat (2),
186 .BR initrd (4),
187 .BR pivot_root (8),
188 .BR switch_root (8)