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>
41 /* remove all files/directories below dirName -- don't cross mountpoints */
42 static int recursiveRemove(char *dirName
)
49 if (!(dir
= opendir(dirName
))) {
50 warn("failed to open %s", dirName
);
56 if (fstat(dfd
, &rb
)) {
57 warn("failed to stat %s", dirName
);
65 if (!(d
= readdir(dir
))) {
67 warn("failed to read %s", dirName
);
70 break; /* end of directory */
73 if (!strcmp(d
->d_name
, ".") || !strcmp(d
->d_name
, ".."))
76 if (d
->d_type
== DT_DIR
) {
79 if (fstatat(dfd
, d
->d_name
, &sb
, AT_SYMLINK_NOFOLLOW
)) {
80 warn("failed to stat %s/%s", dirName
, d
->d_name
);
84 /* remove subdirectories if device is same as dir */
85 if (sb
.st_dev
== rb
.st_dev
) {
86 char subdir
[ strlen(dirName
) +
87 strlen(d
->d_name
) + 2 ];
89 sprintf(subdir
, "%s/%s", dirName
, d
->d_name
);
90 recursiveRemove(subdir
);
95 if (unlinkat(dfd
, d
->d_name
,
96 d
->d_type
== DT_DIR
? AT_REMOVEDIR
: 0))
97 warn("failed to unlink %s/%s", dirName
, d
->d_name
);
100 rc
= 0; /* success */
108 static int switchroot(const char *newroot
)
110 /* Don't try to unmount the old "/", there's no way to do it. */
111 const char *umounts
[] = { "/dev", "/proc", "/sys", NULL
};
114 for (i
= 0; umounts
[i
] != NULL
; i
++) {
115 char newmount
[PATH_MAX
];
117 snprintf(newmount
, sizeof(newmount
), "%s%s", newroot
, umounts
[i
]);
119 if (mount(umounts
[i
], newmount
, NULL
, MS_MOVE
, NULL
) < 0) {
120 warn("failed to mount moving %s to %s",
121 umounts
[i
], newmount
);
122 warnx("forcing unmount of %s", umounts
[i
]);
123 umount2(umounts
[i
], MNT_FORCE
);
127 if (chdir(newroot
)) {
128 warn("failed to change directory to %s", newroot
);
132 recursiveRemove("/");
134 if (mount(newroot
, "/", NULL
, MS_MOVE
, NULL
) < 0) {
135 warn("failed to mount moving %s to /", newroot
);
140 warn("failed to change root");
146 static void usage(FILE *output
)
148 fprintf(output
, "usage: %s <newrootdir> <init> <args to init>\n",
149 program_invocation_short_name
);
150 exit(output
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
153 static void version(void)
155 fprintf(stdout
, "%s from %s\n", program_invocation_short_name
,
160 int main(int argc
, char *argv
[])
162 char *newroot
, *init
, **initargs
;
164 if (argv
[1] && (!strcmp(argv
[1], "--help") || !strcmp(argv
[1], "-h")))
166 if (argv
[1] && (!strcmp(argv
[1], "--version") || !strcmp(argv
[1], "-V")))
175 if (!*newroot
|| !*init
)
178 if (switchroot(newroot
))
179 errx(EXIT_FAILURE
, "failed. Sorry.");
181 if (access(init
, X_OK
))
182 warn("cannot access %s", init
);
184 /* get session leader */
187 /* set controlling terminal */
188 if (ioctl (0, TIOCSCTTY
, 1))
189 warn("failed to TIOCSCTTY");
191 execv(init
, initargs
);
192 err(EXIT_FAILURE
, "failed to execute %s", init
);