2 * switchroot.c - switch to new root directory and start init.
4 * Copyright 2002-2008 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>
26 #include <sys/mount.h>
27 #include <sys/types.h>
29 #include <sys/param.h>
44 #define MNT_DETACH 0x2
53 /* remove all files/directories below dirName -- don't cross mountpoints */
55 recursiveRemove(char * dirName
)
60 char * strBuf
= alloca(strlen(dirName
) + 1024);
62 if (!(dir
= opendir(dirName
))) {
63 printf("error opening %s: %m\n", dirName
);
67 if (fstat(dirfd(dir
),&rb
)) {
68 printf("unable to stat %s: %m\n", dirName
);
74 while ((d
= readdir(dir
))) {
77 if (!strcmp(d
->d_name
, ".") || !strcmp(d
->d_name
, "..")) {
82 strcpy(strBuf
, dirName
);
84 strcat(strBuf
, d
->d_name
);
86 if (lstat(strBuf
, &sb
)) {
87 printf("failed to stat %s: %m\n", strBuf
);
92 /* only descend into subdirectories if device is same as dir */
93 if (S_ISDIR(sb
.st_mode
)) {
94 if (sb
.st_dev
== rb
.st_dev
) {
95 recursiveRemove(strBuf
);
97 printf("failed to rmdir %s: %m\n", strBuf
);
102 if (unlink(strBuf
)) {
103 printf("failed to remove %s: %m\n", strBuf
);
111 printf("error reading from %s: %m\n", dirName
);
120 static int switchroot(const char *newroot
)
122 /* Don't try to unmount the old "/", there's no way to do it. */
123 const char *umounts
[] = { "/dev", "/proc", "/sys", NULL
};
127 for (i
= 0; umounts
[i
] != NULL
; i
++) {
128 char newmount
[PATH_MAX
];
129 strcpy(newmount
, newroot
);
130 strcat(newmount
, umounts
[i
]);
131 if (mount(umounts
[i
], newmount
, NULL
, MS_MOVE
, NULL
) < 0) {
132 fprintf(stderr
, "Error mount moving old %s %s %m\n",
133 umounts
[i
], newmount
);
134 fprintf(stderr
, "Forcing unmount of %s\n", umounts
[i
]);
135 umount2(umounts
[i
], MNT_FORCE
);
139 if (chdir(newroot
) < 0) {
141 fprintf(stderr
, "switchroot: chdir failed: %m\n");
145 recursiveRemove("/");
146 if (mount(newroot
, "/", NULL
, MS_MOVE
, NULL
) < 0) {
148 fprintf(stderr
, "switchroot: mount failed: %m\n");
155 fprintf(stderr
, "switchroot: chroot failed: %m\n");
162 static void usage(FILE *output
)
164 fprintf(output
, "usage: switchroot <newrootdir> <init> <args to init>\n");
165 if (output
== stderr
)
170 int main(int argc
, char *argv
[])
172 char *newroot
= argv
[1];
173 char *init
= argv
[2];
174 char **initargs
= &argv
[2];
176 if (newroot
== NULL
|| newroot
[0] == '\0' ||
177 init
== NULL
|| init
[0] == '\0' ) {
181 if (switchroot(newroot
) < 0) {
182 fprintf(stderr
, "switchroot has failed. Sorry.\n");
185 if (access(initargs
[0], X_OK
))
186 fprintf(stderr
, "WARNING: can't access %s\n", initargs
[0]);
188 /* get session leader */
190 /* set controlling terminal */
191 ioctl (0, TIOCSCTTY
, 1);
193 execv(initargs
[0], initargs
);
197 * vim:noet:ts=8:sw=8:sts=8