]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/switch_root.c
Merge branch 'sys-utils-3rd' of https://github.com/kerolasa/lelux-utiliteetit
[thirdparty/util-linux.git] / sys-utils / switch_root.c
1 /*
2 * switchroot.c - switch to new root directory and start init.
3 *
4 * Copyright 2002-2009 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 #include <sys/mount.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <sys/param.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <ctype.h>
34 #include <dirent.h>
35
36 #include "c.h"
37 #include "nls.h"
38
39 #ifndef MS_MOVE
40 #define MS_MOVE 8192
41 #endif
42
43 /* remove all files/directories below dirName -- don't cross mountpoints */
44 static int recursiveRemove(int fd)
45 {
46 struct stat rb;
47 DIR *dir;
48 int rc = -1;
49 int dfd;
50
51 if (!(dir = fdopendir(fd))) {
52 warn(_("failed to open directory"));
53 goto done;
54 }
55
56 /* fdopendir() precludes us from continuing to use the input fd */
57 dfd = dirfd(dir);
58
59 if (fstat(dfd, &rb)) {
60 warn(_("failed to stat directory"));
61 goto done;
62 }
63
64 while(1) {
65 struct dirent *d;
66
67 errno = 0;
68 if (!(d = readdir(dir))) {
69 if (errno) {
70 warn(_("failed to read directory"));
71 goto done;
72 }
73 break; /* end of directory */
74 }
75
76 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
77 continue;
78
79 if (d->d_type == DT_DIR) {
80 struct stat sb;
81
82 if (fstatat(dfd, d->d_name, &sb, AT_SYMLINK_NOFOLLOW)) {
83 warn(_("failed to stat %s"), d->d_name);
84 continue;
85 }
86
87 /* remove subdirectories if device is same as dir */
88 if (sb.st_dev == rb.st_dev) {
89 int cfd;
90
91 cfd = openat(dfd, d->d_name, O_RDONLY);
92 if (cfd >= 0) {
93 recursiveRemove(cfd);
94 close(cfd);
95 }
96 } else
97 continue;
98 }
99
100 if (unlinkat(dfd, d->d_name,
101 d->d_type == DT_DIR ? AT_REMOVEDIR : 0))
102 warn(_("failed to unlink %s"), d->d_name);
103 }
104
105 rc = 0; /* success */
106
107 done:
108 if (dir)
109 closedir(dir);
110 return rc;
111 }
112
113 static int switchroot(const char *newroot)
114 {
115 /* Don't try to unmount the old "/", there's no way to do it. */
116 const char *umounts[] = { "/dev", "/proc", "/sys", "/run", NULL };
117 int i;
118 int cfd;
119 pid_t pid;
120 struct stat newroot_stat, sb;
121
122 if (stat(newroot, &newroot_stat) != 0) {
123 warn(_("failed to stat directory %s"), newroot);
124 return -1;
125 }
126
127 for (i = 0; umounts[i] != NULL; i++) {
128 char newmount[PATH_MAX];
129
130 snprintf(newmount, sizeof(newmount), "%s%s", newroot, umounts[i]);
131
132 if ((stat(newmount, &sb) != 0) || (sb.st_dev != newroot_stat.st_dev)) {
133 /* mount point seems to be mounted already or stat failed */
134 umount(umounts[i]);
135 continue;
136 }
137
138 if (mount(umounts[i], newmount, NULL, MS_MOVE, NULL) < 0) {
139 warn(_("failed to mount moving %s to %s"),
140 umounts[i], newmount);
141 warnx(_("forcing unmount of %s"), umounts[i]);
142 umount2(umounts[i], MNT_FORCE);
143 }
144 }
145
146 if (chdir(newroot)) {
147 warn(_("failed to change directory to %s"), newroot);
148 return -1;
149 }
150
151 cfd = open("/", O_RDONLY);
152
153 if (mount(newroot, "/", NULL, MS_MOVE, NULL) < 0) {
154 warn(_("failed to mount moving %s to /"), newroot);
155 return -1;
156 }
157
158 if (chroot(".")) {
159 warn(_("failed to change root"));
160 return -1;
161 }
162
163 if (cfd >= 0) {
164 pid = fork();
165 if (pid <= 0) {
166 recursiveRemove(cfd);
167 if (pid == 0)
168 exit(EXIT_SUCCESS);
169 }
170 close(cfd);
171 }
172 return 0;
173 }
174
175 static void __attribute__((__noreturn__)) usage(FILE *output)
176 {
177 fputs(USAGE_HEADER, output);
178 fprintf(output, _(" %s [options] <newrootdir> <init> <args to init>\n"),
179 program_invocation_short_name);
180 fputs(USAGE_OPTIONS, output);
181 fputs(USAGE_HELP, output);
182 fputs(USAGE_VERSION, output);
183 fprintf(output, USAGE_MAN_TAIL("switch_root(8)"));
184
185 exit(output == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
186 }
187
188 int main(int argc, char *argv[])
189 {
190 char *newroot, *init, **initargs;
191
192 if (argv[1] && (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")))
193 usage(stdout);
194 if (argv[1] && (!strcmp(argv[1], "--version") || !strcmp(argv[1], "-V"))) {
195 printf(UTIL_LINUX_VERSION);
196 return EXIT_SUCCESS;
197 }
198 if (argc < 3)
199 usage(stderr);
200
201 newroot = argv[1];
202 init = argv[2];
203 initargs = &argv[2];
204
205 if (!*newroot || !*init)
206 usage(stderr);
207
208 if (switchroot(newroot))
209 errx(EXIT_FAILURE, _("failed. Sorry."));
210
211 if (access(init, X_OK))
212 warn(_("cannot access %s"), init);
213
214 execv(init, initargs);
215 err(EXIT_FAILURE, _("failed to execute %s"), init);
216 }
217