]> git.ipfire.org Git - thirdparty/dracut.git/blob - switch_root.c
fixed Makefile and removed dracut.conf obsoleted by skip-missing
[thirdparty/dracut.git] / switch_root.c
1 /*
2 * switchroot.c - switch to new root directory and start init.
3 *
4 * Copyright 2002-2008 Red Hat, Inc. All rights reserved.
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Authors:
20 * Peter Jones <pjones@redhat.com>
21 * Jeremy Katz <katzj@redhat.com>
22 */
23
24 #define _GNU_SOURCE 1
25
26 #include <sys/mount.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/param.h>
30 #include <fcntl.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <ctype.h>
37
38 #ifndef MS_MOVE
39 #define MS_MOVE 8192
40 #endif
41
42 #ifndef MNT_DETACH
43 #define MNT_DETACH 0x2
44 #endif
45
46 enum {
47 ok,
48 err_no_directory,
49 err_usage,
50 };
51
52 static int switchroot(const char *newroot)
53 {
54 /* Don't try to unmount the old "/", there's no way to do it. */
55 const char *umounts[] = { "/dev", "/proc", "/sys", NULL };
56 int errnum;
57 int i;
58
59 for (i = 0; umounts[i] != NULL; i++) {
60 char newmount[PATH_MAX];
61 strcpy(newmount, newroot);
62 strcat(newmount, umounts[i]);
63 if (mount(umounts[i], newmount, NULL, MS_MOVE, NULL) < 0) {
64 fprintf(stderr, "Error mount moving old %s %s %m\n",
65 umounts[i], newmount);
66 fprintf(stderr, "Forcing unmount of %s\n", umounts[i]);
67 umount2(umounts[i], MNT_FORCE);
68 }
69 }
70
71 if (chdir(newroot) < 0) {
72 errnum=errno;
73 fprintf(stderr, "switchroot: chdir failed: %m\n");
74 errno=errnum;
75 return -1;
76 }
77
78 if (mount(newroot, "/", NULL, MS_MOVE, NULL) < 0) {
79 errnum = errno;
80 fprintf(stderr, "switchroot: mount failed: %m\n");
81 errno = errnum;
82 return -1;
83 }
84
85 if (chroot(".")) {
86 errnum = errno;
87 fprintf(stderr, "switchroot: chroot failed: %m\n");
88 errno = errnum;
89 return -2;
90 }
91 return 1;
92 }
93
94 static void usage(FILE *output)
95 {
96 fprintf(output, "usage: switchroot <newrootdir> <init> <args to init>\n");
97 if (output == stderr)
98 exit(err_usage);
99 exit(ok);
100 }
101
102 int main(int argc, char *argv[])
103 {
104 char *newroot = argv[1];
105 char *init = argv[2];
106 char **initargs = &argv[2];
107
108 if (newroot == NULL || newroot[0] == '\0' ||
109 init == NULL || init[0] == '\0' ) {
110 usage(stderr);
111 }
112
113 if (switchroot(newroot) < 0) {
114 fprintf(stderr, "switchroot has failed. Sorry.\n");
115 return 1;
116 }
117 if (access(initargs[0], X_OK))
118 fprintf(stderr, "WARNING: can't access %s\n", initargs[0]);
119 execv(initargs[0], initargs);
120 }
121
122 /*
123 * vim:noet:ts=8:sw=8:sts=8
124 */