]>
git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - src/dracut/switch_root.c
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>
23 #include <sys/mount.h>
24 #include <sys/types.h>
26 #include <sys/param.h>
46 /* remove all files/directories below dirName -- don't cross mountpoints */
48 recursiveRemove(char * dirName
)
53 char * strBuf
= alloca(strlen(dirName
) + 1024);
55 if (!(dir
= opendir(dirName
))) {
56 printf("error opening %s: %m\n", dirName
);
60 if (fstat(dirfd(dir
),&rb
)) {
61 printf("unable to stat %s: %m\n", dirName
);
68 while ((d
= readdir(dir
))) {
71 if (!strcmp(d
->d_name
, ".") || !strcmp(d
->d_name
, "..")) {
76 strcpy(strBuf
, dirName
);
78 strcat(strBuf
, d
->d_name
);
80 if (lstat(strBuf
, &sb
)) {
81 printf("failed to stat %s: %m\n", strBuf
);
86 /* only descend into subdirectories if device is same as dir */
87 if (S_ISDIR(sb
.st_mode
)) {
88 if (sb
.st_dev
== rb
.st_dev
) {
89 recursiveRemove(strBuf
);
91 printf("failed to rmdir %s: %m\n", strBuf
);
97 printf("failed to remove %s: %m\n", strBuf
);
105 printf("error reading from %s: %m\n", dirName
);
113 static int switchroot(const char *newroot
)
115 /* Don't try to unmount the old "/", there's no way to do it. */
116 const char *umounts
[] = { "/dev", "/proc", "/sys", NULL
};
120 for (i
= 0; umounts
[i
] != NULL
; i
++) {
121 char newmount
[PATH_MAX
];
122 strcpy(newmount
, newroot
);
123 strcat(newmount
, umounts
[i
]);
124 if (mount(umounts
[i
], newmount
, NULL
, MS_MOVE
, NULL
) < 0) {
125 fprintf(stderr
, "Error mount moving old %s %s %m\n",
126 umounts
[i
], newmount
);
127 fprintf(stderr
, "Forcing unmount of %s\n", umounts
[i
]);
128 umount2(umounts
[i
], MNT_FORCE
);
132 if (chdir(newroot
) < 0) {
134 fprintf(stderr
, "switchroot: chdir failed: %m\n");
138 recursiveRemove("/");
139 if (mount(newroot
, "/", NULL
, MS_MOVE
, NULL
) < 0) {
141 fprintf(stderr
, "switchroot: mount failed: %m\n");
148 fprintf(stderr
, "switchroot: chroot failed: %m\n");
155 static void usage(FILE *output
)
157 fprintf(output
, "usage: switchroot <newrootdir> <init> <args to init>\n");
158 if (output
== stderr
)
163 int main(int argc
, char *argv
[])
165 char *newroot
= argv
[1];
166 char *init
= argv
[2];
167 char **initargs
= &argv
[2];
169 if (newroot
== NULL
|| newroot
[0] == '\0' ||
170 init
== NULL
|| init
[0] == '\0' ) {
174 if (switchroot(newroot
) < 0) {
175 fprintf(stderr
, "switchroot has failed. Sorry.\n");
178 if (access(initargs
[0], X_OK
))
179 fprintf(stderr
, "WARNING: can't access %s\n", initargs
[0]);
181 /* get session leader */
183 /* set controlling terminal */
184 ioctl (0, TIOCSCTTY
, 1);
186 execv(initargs
[0], initargs
);