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 */
66 if (fstat(dfd
, &rb
)) {
67 warn(_("stat failed"));
76 if (!(d
= readdir(dir
))) {
78 warn(_("failed to read directory"));
81 break; /* end of directory */
84 if (!strcmp(d
->d_name
, ".") || !strcmp(d
->d_name
, ".."))
86 #ifdef _DIRENT_HAVE_D_TYPE
87 if (d
->d_type
== DT_DIR
|| d
->d_type
== DT_UNKNOWN
)
92 if (fstatat(dfd
, d
->d_name
, &sb
, AT_SYMLINK_NOFOLLOW
)) {
93 warn(_("stat of %s failed"), d
->d_name
);
97 /* skip if device is not the same */
98 if (sb
.st_dev
!= rb
.st_dev
)
101 /* remove subdirectories */
102 if (S_ISDIR(sb
.st_mode
)) {
105 cfd
= openat(dfd
, d
->d_name
, O_RDONLY
);
107 recursiveRemove(cfd
); /* it closes cfd too */
112 if (unlinkat(dfd
, d
->d_name
, isdir
? AT_REMOVEDIR
: 0))
113 warn(_("failed to unlink %s"), d
->d_name
);
116 rc
= 0; /* success */
125 static int switchroot(const char *newroot
)
127 /* Don't try to unmount the old "/", there's no way to do it. */
128 const char *umounts
[] = { "/dev", "/proc", "/sys", "/run", NULL
};
131 struct stat newroot_stat
, oldroot_stat
, sb
;
133 if (stat("/", &oldroot_stat
) != 0) {
134 warn(_("stat of %s failed"), "/");
138 if (stat(newroot
, &newroot_stat
) != 0) {
139 warn(_("stat of %s failed"), newroot
);
143 for (i
= 0; umounts
[i
] != NULL
; i
++) {
144 char newmount
[PATH_MAX
];
146 snprintf(newmount
, sizeof(newmount
), "%s%s", newroot
, umounts
[i
]);
148 if ((stat(umounts
[i
], &sb
) == 0) && sb
.st_dev
== oldroot_stat
.st_dev
) {
149 /* mount point to move seems to be a normal directory or stat failed */
153 if ((stat(newmount
, &sb
) != 0) || (sb
.st_dev
!= newroot_stat
.st_dev
)) {
154 /* mount point seems to be mounted already or stat failed */
155 umount2(umounts
[i
], MNT_DETACH
);
159 if (mount(umounts
[i
], newmount
, NULL
, MS_MOVE
, NULL
) < 0) {
160 warn(_("failed to mount moving %s to %s"),
161 umounts
[i
], newmount
);
162 warnx(_("forcing unmount of %s"), umounts
[i
]);
163 umount2(umounts
[i
], MNT_FORCE
);
167 if (chdir(newroot
)) {
168 warn(_("failed to change directory to %s"), newroot
);
172 cfd
= open("/", O_RDONLY
);
174 warn(_("cannot open %s"), "/");
178 if (mount(newroot
, "/", NULL
, MS_MOVE
, NULL
) < 0) {
179 warn(_("failed to mount moving %s to /"), newroot
);
184 warn(_("failed to change root"));
189 warn(_("cannot change directory to %s"), "/");
198 if (fstatfs(cfd
, &stfs
) == 0 &&
199 (F_TYPE_EQUAL(stfs
.f_type
, STATFS_RAMFS_MAGIC
) ||
200 F_TYPE_EQUAL(stfs
.f_type
, STATFS_TMPFS_MAGIC
)))
201 recursiveRemove(cfd
);
203 warn(_("old root filesystem is not an initramfs"));
211 default: /* parent */
222 static void __attribute__((__noreturn__
)) usage(void)
224 FILE *output
= stdout
;
225 fputs(USAGE_HEADER
, output
);
226 fprintf(output
, _(" %s [options] <newrootdir> <init> <args to init>\n"),
227 program_invocation_short_name
);
229 fputs(USAGE_SEPARATOR
, output
);
230 fputs(_("Switch to another filesystem as the root of the mount tree.\n"), output
);
232 fputs(USAGE_OPTIONS
, output
);
233 fprintf(output
, USAGE_HELP_OPTIONS(16));
234 fprintf(output
, USAGE_MAN_TAIL("switch_root(8)"));
239 int main(int argc
, char *argv
[])
241 char *newroot
, *init
, **initargs
;
243 static const struct option longopts
[] = {
244 {"version", no_argument
, NULL
, 'V'},
245 {"help", no_argument
, NULL
, 'h'},
249 close_stdout_atexit();
251 while ((c
= getopt_long(argc
, argv
, "+Vh", longopts
, NULL
)) != -1)
254 print_version(EXIT_SUCCESS
);
258 errtryhelp(EXIT_FAILURE
);
261 warnx(_("not enough arguments"));
262 errtryhelp(EXIT_FAILURE
);
269 if (!*newroot
|| !*init
) {
270 warnx(_("bad usage"));
271 errtryhelp(EXIT_FAILURE
);
274 if (switchroot(newroot
))
275 errx(EXIT_FAILURE
, _("failed. Sorry."));
277 if (access(init
, X_OK
))
278 warn(_("cannot access %s"), init
);
280 execv(init
, initargs
);