use 128M for the test virtual machines instead of 512M
[dracut/plouj.git] / switch_root.c
blobef7a0fb5da7bc5d2b9c66becded6763b8279731f
1 /*
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/>.
19 * Authors:
20 * Peter Jones <pjones@redhat.com>
21 * Jeremy Katz <katzj@redhat.com>
24 #define _GNU_SOURCE 1
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 #include <dirent.h>
39 #ifndef MS_MOVE
40 #define MS_MOVE 8192
41 #endif
43 #ifndef MNT_DETACH
44 #define MNT_DETACH 0x2
45 #endif
47 enum {
48 ok,
49 err_no_directory,
50 err_usage,
53 /* remove all files/directories below dirName -- don't cross mountpoints */
54 static int
55 recursiveRemove(char * dirName)
57 struct stat sb,rb;
58 DIR * dir;
59 struct dirent * d;
60 char * strBuf = alloca(strlen(dirName) + 1024);
62 if (!(dir = opendir(dirName))) {
63 printf("error opening %s: %m\n", dirName);
64 return 0;
67 if (fstat(dirfd(dir),&rb)) {
68 printf("unable to stat %s: %m\n", dirName);
69 closedir(dir);
70 return 0;
73 errno = 0;
74 while ((d = readdir(dir))) {
75 errno = 0;
77 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) {
78 errno = 0;
79 continue;
82 strcpy(strBuf, dirName);
83 strcat(strBuf, "/");
84 strcat(strBuf, d->d_name);
86 if (lstat(strBuf, &sb)) {
87 printf("failed to stat %s: %m\n", strBuf);
88 errno = 0;
89 continue;
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);
96 if (rmdir(strBuf))
97 printf("failed to rmdir %s: %m\n", strBuf);
99 errno = 0;
100 continue;
102 if (unlink(strBuf)) {
103 printf("failed to remove %s: %m\n", strBuf);
104 errno = 0;
105 continue;
109 if (errno) {
110 closedir(dir);
111 printf("error reading from %s: %m\n", dirName);
112 return 1;
115 closedir(dir);
117 return 0;
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 };
124 int errnum;
125 int i;
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) {
140 errnum=errno;
141 fprintf(stderr, "switchroot: chdir failed: %m\n");
142 errno=errnum;
143 return -1;
145 recursiveRemove("/");
146 if (mount(newroot, "/", NULL, MS_MOVE, NULL) < 0) {
147 errnum = errno;
148 fprintf(stderr, "switchroot: mount failed: %m\n");
149 errno = errnum;
150 return -1;
153 if (chroot(".")) {
154 errnum = errno;
155 fprintf(stderr, "switchroot: chroot failed: %m\n");
156 errno = errnum;
157 return -2;
159 return 1;
162 static void usage(FILE *output)
164 fprintf(output, "usage: switchroot <newrootdir> <init> <args to init>\n");
165 if (output == stderr)
166 exit(err_usage);
167 exit(ok);
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' ) {
178 usage(stderr);
181 if (switchroot(newroot) < 0) {
182 fprintf(stderr, "switchroot has failed. Sorry.\n");
183 return 1;
185 if (access(initargs[0], X_OK))
186 fprintf(stderr, "WARNING: can't access %s\n", initargs[0]);
188 /* get session leader */
189 setsid();
190 /* set controlling terminal */
191 ioctl (0, TIOCSCTTY, 1);
193 execv(initargs[0], initargs);
197 * vim:noet:ts=8:sw=8:sts=8