2 * switchroot.c - switch to new root directory and start init.
4 * Copyright 2002-2009 Red Hat, Inc. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * Peter Jones <pjones@redhat.com>
21 * Jeremy Katz <katzj@redhat.com>
23 #include <sys/mount.h>
24 #include <sys/types.h>
26 #include <sys/param.h>
38 #include "closestream.h"
45 #define MNT_DETACH 0x00000002 /* Just detach from the tree */
48 /* remove all files/directories below dirName -- don't cross mountpoints */
49 static int recursiveRemove(int fd
)
56 if (!(dir
= fdopendir(fd
))) {
57 warn(_("failed to open directory"));
61 /* fdopendir() precludes us from continuing to use the input fd */
64 if (fstat(dfd
, &rb
)) {
65 warn(_("failed to stat directory"));
73 if (!(d
= readdir(dir
))) {
75 warn(_("failed to read directory"));
78 break; /* end of directory */
81 if (!strcmp(d
->d_name
, ".") || !strcmp(d
->d_name
, ".."))
84 if (d
->d_type
== DT_DIR
) {
87 if (fstatat(dfd
, d
->d_name
, &sb
, AT_SYMLINK_NOFOLLOW
)) {
88 warn(_("failed to stat %s"), d
->d_name
);
92 /* remove subdirectories if device is same as dir */
93 if (sb
.st_dev
== rb
.st_dev
) {
96 cfd
= openat(dfd
, d
->d_name
, O_RDONLY
);
105 if (unlinkat(dfd
, d
->d_name
,
106 d
->d_type
== DT_DIR
? AT_REMOVEDIR
: 0))
107 warn(_("failed to unlink %s"), d
->d_name
);
110 rc
= 0; /* success */
118 static int switchroot(const char *newroot
)
120 /* Don't try to unmount the old "/", there's no way to do it. */
121 const char *umounts
[] = { "/dev", "/proc", "/sys", "/run", NULL
};
125 struct stat newroot_stat
, sb
;
127 if (stat(newroot
, &newroot_stat
) != 0) {
128 warn(_("failed to stat directory %s"), newroot
);
132 for (i
= 0; umounts
[i
] != NULL
; i
++) {
133 char newmount
[PATH_MAX
];
135 snprintf(newmount
, sizeof(newmount
), "%s%s", newroot
, umounts
[i
]);
137 if ((stat(newmount
, &sb
) != 0) || (sb
.st_dev
!= newroot_stat
.st_dev
)) {
138 /* mount point seems to be mounted already or stat failed */
139 umount2(umounts
[i
], MNT_DETACH
);
143 if (mount(umounts
[i
], newmount
, NULL
, MS_MOVE
, NULL
) < 0) {
144 warn(_("failed to mount moving %s to %s"),
145 umounts
[i
], newmount
);
146 warnx(_("forcing unmount of %s"), umounts
[i
]);
147 umount2(umounts
[i
], MNT_FORCE
);
151 if (chdir(newroot
)) {
152 warn(_("failed to change directory to %s"), newroot
);
156 cfd
= open("/", O_RDONLY
);
158 if (mount(newroot
, "/", NULL
, MS_MOVE
, NULL
) < 0) {
160 warn(_("failed to mount moving %s to /"), newroot
);
166 warn(_("failed to change root"));
173 recursiveRemove(cfd
);
182 static void __attribute__((__noreturn__
)) usage(FILE *output
)
184 fputs(USAGE_HEADER
, output
);
185 fprintf(output
, _(" %s [options] <newrootdir> <init> <args to init>\n"),
186 program_invocation_short_name
);
187 fputs(USAGE_OPTIONS
, output
);
188 fputs(USAGE_HELP
, output
);
189 fputs(USAGE_VERSION
, output
);
190 fprintf(output
, USAGE_MAN_TAIL("switch_root(8)"));
192 exit(output
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
195 int main(int argc
, char *argv
[])
197 char *newroot
, *init
, **initargs
;
198 atexit(close_stdout
);
200 if (argv
[1] && (!strcmp(argv
[1], "--help") || !strcmp(argv
[1], "-h")))
202 if (argv
[1] && (!strcmp(argv
[1], "--version") || !strcmp(argv
[1], "-V"))) {
203 printf(UTIL_LINUX_VERSION
);
213 if (!*newroot
|| !*init
)
216 if (switchroot(newroot
))
217 errx(EXIT_FAILURE
, _("failed. Sorry."));
219 if (access(init
, X_OK
))
220 warn(_("cannot access %s"), init
);
222 execv(init
, initargs
);
223 err(EXIT_FAILURE
, _("failed to execute %s"), init
);