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