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