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/statfs.h>
27 #include <sys/param.h>
40 #include "closestream.h"
41 #include "statfs_magic.h"
48 #define MNT_DETACH 0x00000002 /* Just detach from the tree */
51 /* remove all files/directories below dirName -- don't cross mountpoints */
52 static int recursiveRemove(int fd
)
59 if (!(dir
= fdopendir(fd
))) {
60 warn(_("failed to open directory"));
64 /* fdopendir() precludes us from continuing to use the input fd */
67 if (fstat(dfd
, &rb
)) {
68 warn(_("stat failed"));
77 if (!(d
= readdir(dir
))) {
79 warn(_("failed to read directory"));
82 break; /* end of directory */
85 if (!strcmp(d
->d_name
, ".") || !strcmp(d
->d_name
, ".."))
87 #ifdef _DIRENT_HAVE_D_TYPE
88 if (d
->d_type
== DT_DIR
|| d
->d_type
== DT_UNKNOWN
)
93 if (fstatat(dfd
, d
->d_name
, &sb
, AT_SYMLINK_NOFOLLOW
)) {
94 warn(_("stat of %s failed"), d
->d_name
);
98 /* skip if device is not the same */
99 if (sb
.st_dev
!= rb
.st_dev
)
102 /* remove subdirectories */
103 if (S_ISDIR(sb
.st_mode
)) {
106 cfd
= openat(dfd
, d
->d_name
, O_RDONLY
);
108 recursiveRemove(cfd
);
115 if (unlinkat(dfd
, d
->d_name
, isdir
? AT_REMOVEDIR
: 0))
116 warn(_("failed to unlink %s"), d
->d_name
);
119 rc
= 0; /* success */
127 static int switchroot(const char *newroot
)
129 /* Don't try to unmount the old "/", there's no way to do it. */
130 const char *umounts
[] = { "/dev", "/proc", "/sys", "/run", NULL
};
134 struct stat newroot_stat
, sb
;
136 if (stat(newroot
, &newroot_stat
) != 0) {
137 warn(_("stat of %s failed"), newroot
);
141 for (i
= 0; umounts
[i
] != NULL
; i
++) {
142 char newmount
[PATH_MAX
];
144 snprintf(newmount
, sizeof(newmount
), "%s%s", newroot
, umounts
[i
]);
146 if ((stat(newmount
, &sb
) != 0) || (sb
.st_dev
!= newroot_stat
.st_dev
)) {
147 /* mount point seems to be mounted already or stat failed */
148 umount2(umounts
[i
], MNT_DETACH
);
152 if (mount(umounts
[i
], newmount
, NULL
, MS_MOVE
, NULL
) < 0) {
153 warn(_("failed to mount moving %s to %s"),
154 umounts
[i
], newmount
);
155 warnx(_("forcing unmount of %s"), umounts
[i
]);
156 umount2(umounts
[i
], MNT_FORCE
);
160 if (chdir(newroot
)) {
161 warn(_("failed to change directory to %s"), newroot
);
165 cfd
= open("/", O_RDONLY
);
167 warn(_("cannot open %s"), "/");
171 if (mount(newroot
, "/", NULL
, MS_MOVE
, NULL
) < 0) {
173 warn(_("failed to mount moving %s to /"), newroot
);
179 warn(_("failed to change root"));
187 if (fstatfs(cfd
, &stfs
) == 0 &&
188 (F_TYPE_EQUAL(stfs
.f_type
, STATFS_RAMFS_MAGIC
) ||
189 F_TYPE_EQUAL(stfs
.f_type
, STATFS_TMPFS_MAGIC
)))
190 recursiveRemove(cfd
);
192 warn(_("old root filesystem is not an initramfs"));
201 static void __attribute__((__noreturn__
)) usage(void)
203 FILE *output
= stdout
;
204 fputs(USAGE_HEADER
, output
);
205 fprintf(output
, _(" %s [options] <newrootdir> <init> <args to init>\n"),
206 program_invocation_short_name
);
208 fputs(USAGE_SEPARATOR
, output
);
209 fputs(_("Switch to another filesystem as the root of the mount tree.\n"), output
);
211 fputs(USAGE_OPTIONS
, output
);
212 printf(USAGE_HELP_OPTIONS(16));
213 printf(USAGE_MAN_TAIL("switch_root(8)"));
218 int main(int argc
, char *argv
[])
220 char *newroot
, *init
, **initargs
;
222 static const struct option longopts
[] = {
223 {"version", no_argument
, NULL
, 'V'},
224 {"help", no_argument
, NULL
, 'h'},
228 atexit(close_stdout
);
230 while ((c
= getopt_long(argc
, argv
, "Vh", longopts
, NULL
)) != -1)
233 printf(UTIL_LINUX_VERSION
);
238 errtryhelp(EXIT_FAILURE
);
241 warnx(_("not enough arguments"));
242 errtryhelp(EXIT_FAILURE
);
249 if (!*newroot
|| !*init
) {
250 warnx(_("bad usage"));
251 errtryhelp(EXIT_FAILURE
);
254 if (switchroot(newroot
))
255 errx(EXIT_FAILURE
, _("failed. Sorry."));
257 if (access(init
, X_OK
))
258 warn(_("cannot access %s"), init
);
260 execv(init
, initargs
);
261 err(EXIT_FAILURE
, _("failed to execute %s"), init
);