blob: 5a01fd6f7b8b80db85dcab78d6a85277e5e040bb [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Rob Landley3ea05d32006-03-21 18:20:40 +00002/* Copyright 2005 Rob Landley <rob@landley.net>
3 *
4 * Switch from rootfs to another filesystem as the root of the mount tree.
5 *
Rob Landleye9a7a622006-09-22 02:52:41 +00006 * Licensed under GPL version 2, see file LICENSE in this tarball for details.
Rob Landley3ea05d32006-03-21 18:20:40 +00007 */
Rob Landley0f34a822005-10-27 22:55:50 +00008
Rob Landley22d26fc2006-06-15 15:49:36 +00009#include "busybox.h"
Rob Landley0f34a822005-10-27 22:55:50 +000010#include <sys/vfs.h>
Rob Landley0f34a822005-10-27 22:55:50 +000011
Rob Landley0f34a822005-10-27 22:55:50 +000012
13// Make up for header deficiencies.
14
15#ifndef RAMFS_MAGIC
16#define RAMFS_MAGIC 0x858458f6
17#endif
18
19#ifndef TMPFS_MAGIC
20#define TMPFS_MAGIC 0x01021994
21#endif
22
23#ifndef MS_MOVE
24#define MS_MOVE 8192
25#endif
26
27dev_t rootdev;
28
29// Recursively delete contents of rootfs.
30
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000031static void delete_contents(const char *directory)
Rob Landley0f34a822005-10-27 22:55:50 +000032{
33 DIR *dir;
34 struct dirent *d;
35 struct stat st;
36
37 // Don't descend into other filesystems
Denis Vlasenko92258542006-11-01 10:25:35 +000038 if (lstat(directory, &st) || st.st_dev != rootdev) return;
Rob Landley0f34a822005-10-27 22:55:50 +000039
40 // Recursively delete the contents of directories.
41 if (S_ISDIR(st.st_mode)) {
42 if((dir = opendir(directory))) {
43 while ((d = readdir(dir))) {
44 char *newdir=d->d_name;
45
46 // Skip . and ..
47 if(*newdir=='.' && (!newdir[1] || (newdir[1]=='.' && !newdir[2])))
48 continue;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000049
Rob Landley0f34a822005-10-27 22:55:50 +000050 // Recurse to delete contents
51 newdir = alloca(strlen(directory) + strlen(d->d_name) + 2);
52 sprintf(newdir, "%s/%s", directory, d->d_name);
53 delete_contents(newdir);
54 }
55 closedir(dir);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000056
Rob Landley0f34a822005-10-27 22:55:50 +000057 // Directory should now be empty. Zap it.
Rob Landley5d84c232005-12-20 17:25:51 +000058 rmdir(directory);
Rob Landley0f34a822005-10-27 22:55:50 +000059 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000060
Rob Landley0f34a822005-10-27 22:55:50 +000061 // It wasn't a directory. Zap it.
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000062
Rob Landley5d84c232005-12-20 17:25:51 +000063 } else unlink(directory);
Rob Landley0f34a822005-10-27 22:55:50 +000064}
65
Denis Vlasenko06af2162007-02-03 17:28:39 +000066int switch_root_main(int argc, char *argv[]);
Rob Landley0f34a822005-10-27 22:55:50 +000067int switch_root_main(int argc, char *argv[])
68{
Rob Landley5d84c232005-12-20 17:25:51 +000069 char *newroot, *console=NULL;
Rob Landley0f34a822005-10-27 22:55:50 +000070 struct stat st1, st2;
71 struct statfs stfs;
72
73 // Parse args (-c console)
74
Denis Vlasenko92258542006-11-01 10:25:35 +000075 opt_complementary = "-2";
76 getopt32(argc, argv, "c:", &console);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000077
Rob Landley0f34a822005-10-27 22:55:50 +000078 // Change to new root directory and verify it's a different fs.
79
80 newroot=argv[optind++];
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000081
Rob Landleye2b428c2006-03-20 01:43:29 +000082 if (chdir(newroot) || lstat(".", &st1) || lstat("/", &st2) ||
Rob Landley0f34a822005-10-27 22:55:50 +000083 st1.st_dev == st2.st_dev)
84 {
Denis Vlasenko92258542006-11-01 10:25:35 +000085 bb_error_msg_and_die("bad newroot %s", newroot);
Rob Landley0f34a822005-10-27 22:55:50 +000086 }
87 rootdev=st2.st_dev;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000088
Rob Landley0f34a822005-10-27 22:55:50 +000089 // Additional sanity checks: we're about to rm -rf /, so be REALLY SURE
90 // we mean it. (I could make this a CONFIG option, but I would get email
91 // from all the people who WILL eat their filesystemss.)
92
Rob Landleye2b428c2006-03-20 01:43:29 +000093 if (lstat("/init", &st1) || !S_ISREG(st1.st_mode) || statfs("/", &stfs) ||
Rob Landley0f34a822005-10-27 22:55:50 +000094 (stfs.f_type != RAMFS_MAGIC && stfs.f_type != TMPFS_MAGIC) ||
95 getpid() != 1)
96 {
97 bb_error_msg_and_die("not rootfs");
98 }
99
100 // Zap everything out of rootdev
Rob Landley5d84c232005-12-20 17:25:51 +0000101
Rob Landley0f34a822005-10-27 22:55:50 +0000102 delete_contents("/");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000103
Rob Landley5d84c232005-12-20 17:25:51 +0000104 // Overmount / with newdir and chroot into it. The chdir is needed to
105 // recalculate "." and ".." links.
Rob Landley0f34a822005-10-27 22:55:50 +0000106
Rob Landley5d84c232005-12-20 17:25:51 +0000107 if (mount(".", "/", NULL, MS_MOVE, NULL) || chroot(".") || chdir("/"))
Rob Landley0f34a822005-10-27 22:55:50 +0000108 bb_error_msg_and_die("moving root");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000109
Rob Landley5d84c232005-12-20 17:25:51 +0000110 // If a new console specified, redirect stdin/stdout/stderr to that.
111
112 if (console) {
113 close(0);
114 if(open(console, O_RDWR) < 0)
Denis Vlasenko92258542006-11-01 10:25:35 +0000115 bb_error_msg_and_die("bad console '%s'", console);
Rob Landley5d84c232005-12-20 17:25:51 +0000116 dup2(0, 1);
117 dup2(0, 2);
118 }
Rob Landley0f34a822005-10-27 22:55:50 +0000119
120 // Exec real init. (This is why we must be pid 1.)
Denis Vlasenko92258542006-11-01 10:25:35 +0000121 execv(argv[optind], argv+optind);
122 bb_error_msg_and_die("bad init '%s'", argv[optind]);
Rob Landley0f34a822005-10-27 22:55:50 +0000123}