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>
42 /* remove all files/directories below dirName -- don't cross mountpoints */
43 static int recursiveRemove(int fd)
50 if (!(dir = fdopendir(fd))) {
51 warn("failed to open directory");
55 /* fdopendir() precludes us from continuing to use the input fd */
58 if (fstat(dfd, &rb)) {
59 warn("failed to stat directory");
67 if (!(d = readdir(dir))) {
69 warn("failed to read directory");
72 break; /* end of directory */
75 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
78 if (d->d_type == DT_DIR) {
81 if (fstatat(dfd, d->d_name, &sb, AT_SYMLINK_NOFOLLOW)) {
82 warn("failed to stat %s", d->d_name);
86 /* remove subdirectories if device is same as dir */
87 if (sb.st_dev == rb.st_dev) {
90 cfd = openat(dfd, d->d_name, O_RDONLY);
99 if (unlinkat(dfd, d->d_name,
100 d->d_type == DT_DIR ? AT_REMOVEDIR : 0))
101 warn("failed to unlink %s", d->d_name);
104 rc = 0; /* success */
112 /* find the enclosing mount point of a path, by examining the backing device
113 * of parent directories until we reach / or find a parent with a differing
114 * device. Result must be freed.
116 static char *get_parent_mount(const char *path)
123 if (stat(path, &sb) != 0) {
124 warn("failed to stat %s", path);
128 inner_dev = sb.st_dev;
134 strncpy(tmp, dir, PATH_MAX);
135 tmp[PATH_MAX - 1] = '\0';
136 parent = dirname(tmp);
138 if (stat(parent, &sb) != 0) {
139 warn("failed to stat %s", parent);
142 if (sb.st_dev != inner_dev)
145 strncpy(dir, parent, PATH_MAX);
146 dir[PATH_MAX - 1] = '\0';
148 /* maybe we've reached / */
149 if (*dir == '/' && !*(dir + 1))
155 static int switchroot(const char *newroot)
157 /* Don't try to unmount the old "/", there's no way to do it. */
158 const char *umounts[] = { "/dev", "/proc", "/sys", NULL };
162 const char *chroot_path = NULL;
165 for (i = 0; umounts[i] != NULL; i++) {
166 char newmount[PATH_MAX];
168 snprintf(newmount, sizeof(newmount), "%s%s", newroot, umounts[i]);
170 if (mount(umounts[i], newmount, NULL, MS_MOVE, NULL) < 0) {
171 warn("failed to mount moving %s to %s",
172 umounts[i], newmount);
173 warnx("forcing unmount of %s", umounts[i]);
174 umount2(umounts[i], MNT_FORCE);
178 newroot_mnt = get_parent_mount(newroot);
179 if (newroot_mnt && strcmp(newroot, newroot_mnt)) {
180 /* newroot is not a mount point, so we have to MS_MOVE the
181 * parent mount point and then chroot in to the "subroot"
183 chroot_path = newroot + strlen(newroot_mnt);
184 newroot = newroot_mnt;
187 if (chdir(newroot)) {
188 warn("failed to change directory to %s", newroot);
192 cfd = open("/", O_RDONLY);
194 if (mount(newroot, "/", NULL, MS_MOVE, NULL) < 0) {
195 warn("failed to mount moving %s to /", newroot);
199 /* move to the real root of the device */
201 warn("failed to change root");
205 /* move to the subdirectory on the root device (subroot) */
207 if (chdir(chroot_path)) {
208 warn("failed to chdir to subroot %s", chroot_path);
212 warn("failed to change root to subroot %s", chroot_path);
220 recursiveRemove(cfd);
232 static void usage(FILE *output)
234 fprintf(output, "usage: %s <newrootdir> <init> <args to init>\n",
235 program_invocation_short_name);
236 exit(output == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
239 static void version(void)
241 fprintf(stdout, "%s from %s\n", program_invocation_short_name,
246 int main(int argc, char *argv[])
248 char *newroot, *init, **initargs;
250 if (argv[1] && (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")))
252 if (argv[1] && (!strcmp(argv[1], "--version") || !strcmp(argv[1], "-V")))
261 if (!*newroot || !*init)
264 if (switchroot(newroot))
265 errx(EXIT_FAILURE, "failed. Sorry.");
267 if (access(init, X_OK))
268 warn("cannot access %s", init);
270 execv(init, initargs);
271 err(EXIT_FAILURE, "failed to execute %s", init);