blob: bae695b0b6c39064f0d644f71fadbf36584b52f6 [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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +00009#include "libbb.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
Denis Vlasenko77ad97f2008-05-13 02:27:31 +000016#define RAMFS_MAGIC ((unsigned)0x858458f6)
Rob Landley0f34a822005-10-27 22:55:50 +000017#endif
18
19#ifndef TMPFS_MAGIC
Denis Vlasenko77ad97f2008-05-13 02:27:31 +000020#define TMPFS_MAGIC ((unsigned)0x01021994)
Rob Landley0f34a822005-10-27 22:55:50 +000021#endif
22
23#ifndef MS_MOVE
Denis Vlasenko77ad97f2008-05-13 02:27:31 +000024#define MS_MOVE 8192
Rob Landley0f34a822005-10-27 22:55:50 +000025#endif
26
Denis Vlasenko51742f42007-04-12 00:32:05 +000027static dev_t rootdev;
Rob Landley0f34a822005-10-27 22:55:50 +000028
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)) {
Denis Vlasenko51742f42007-04-12 00:32:05 +000042 dir = opendir(directory);
43 if (dir) {
Rob Landley0f34a822005-10-27 22:55:50 +000044 while ((d = readdir(dir))) {
Denis Vlasenko51742f42007-04-12 00:32:05 +000045 char *newdir = d->d_name;
Rob Landley0f34a822005-10-27 22:55:50 +000046
47 // Skip . and ..
Denis Vlasenko51742f42007-04-12 00:32:05 +000048 if (*newdir=='.' && (!newdir[1] || (newdir[1]=='.' && !newdir[2])))
Rob Landley0f34a822005-10-27 22:55:50 +000049 continue;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000050
Rob Landley0f34a822005-10-27 22:55:50 +000051 // Recurse to delete contents
52 newdir = alloca(strlen(directory) + strlen(d->d_name) + 2);
53 sprintf(newdir, "%s/%s", directory, d->d_name);
54 delete_contents(newdir);
55 }
56 closedir(dir);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000057
Rob Landley0f34a822005-10-27 22:55:50 +000058 // Directory should now be empty. Zap it.
Rob Landley5d84c232005-12-20 17:25:51 +000059 rmdir(directory);
Rob Landley0f34a822005-10-27 22:55:50 +000060 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000061
Rob Landley0f34a822005-10-27 22:55:50 +000062 // It wasn't a directory. Zap it.
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000063
Rob Landley5d84c232005-12-20 17:25:51 +000064 } else unlink(directory);
Rob Landley0f34a822005-10-27 22:55:50 +000065}
66
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000067int switch_root_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko68404f12008-03-17 09:00:54 +000068int switch_root_main(int argc ATTRIBUTE_UNUSED, char **argv)
Rob Landley0f34a822005-10-27 22:55:50 +000069{
Denis Vlasenko51742f42007-04-12 00:32:05 +000070 char *newroot, *console = NULL;
Rob Landley0f34a822005-10-27 22:55:50 +000071 struct stat st1, st2;
72 struct statfs stfs;
73
74 // Parse args (-c console)
75
Denis Vlasenko6dd03f02008-02-13 17:25:31 +000076 opt_complementary = "-2"; // minimum 2 params
77 getopt32(argv, "+c:", &console); // '+': stop parsing at first non-option
Denis Vlasenko3ace9fa2007-04-18 21:40:30 +000078 argv += optind;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000079
Rob Landley0f34a822005-10-27 22:55:50 +000080 // Change to new root directory and verify it's a different fs.
81
Denis Vlasenko3ace9fa2007-04-18 21:40:30 +000082 newroot = *argv++;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000083
Denis Vlasenko3ace9fa2007-04-18 21:40:30 +000084 xchdir(newroot);
85 if (lstat(".", &st1) || lstat("/", &st2) || st1.st_dev == st2.st_dev) {
Denis Vlasenko92258542006-11-01 10:25:35 +000086 bb_error_msg_and_die("bad newroot %s", newroot);
Rob Landley0f34a822005-10-27 22:55:50 +000087 }
Denis Vlasenko51742f42007-04-12 00:32:05 +000088 rootdev = st2.st_dev;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000089
Rob Landley0f34a822005-10-27 22:55:50 +000090 // Additional sanity checks: we're about to rm -rf /, so be REALLY SURE
91 // we mean it. (I could make this a CONFIG option, but I would get email
Denis Vlasenko51742f42007-04-12 00:32:05 +000092 // from all the people who WILL eat their filesystems.)
Rob Landley0f34a822005-10-27 22:55:50 +000093
Denis Vlasenko77ad97f2008-05-13 02:27:31 +000094 if (lstat("/init", &st1) || !S_ISREG(st1.st_mode) || statfs("/", &stfs)
95 || (((unsigned)stfs.f_type != RAMFS_MAGIC) && ((unsigned)stfs.f_type != TMPFS_MAGIC))
96 || (getpid() != 1)
97 ) {
Rob Landley0f34a822005-10-27 22:55:50 +000098 bb_error_msg_and_die("not rootfs");
99 }
100
101 // Zap everything out of rootdev
Rob Landley5d84c232005-12-20 17:25:51 +0000102
Rob Landley0f34a822005-10-27 22:55:50 +0000103 delete_contents("/");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000104
Rob Landley5d84c232005-12-20 17:25:51 +0000105 // Overmount / with newdir and chroot into it. The chdir is needed to
106 // recalculate "." and ".." links.
Rob Landley0f34a822005-10-27 22:55:50 +0000107
Denis Vlasenko394eebe2008-02-25 20:30:24 +0000108 if (mount(".", "/", NULL, MS_MOVE, NULL))
Denis Vlasenko51742f42007-04-12 00:32:05 +0000109 bb_error_msg_and_die("error moving root");
Denis Vlasenko394eebe2008-02-25 20:30:24 +0000110 xchroot(".");
Denis Vlasenko3ace9fa2007-04-18 21:40:30 +0000111 xchdir("/");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000112
Rob Landley5d84c232005-12-20 17:25:51 +0000113 // If a new console specified, redirect stdin/stdout/stderr to that.
114
115 if (console) {
116 close(0);
Denis Vlasenko51742f42007-04-12 00:32:05 +0000117 xopen(console, O_RDWR);
Rob Landley5d84c232005-12-20 17:25:51 +0000118 dup2(0, 1);
119 dup2(0, 2);
120 }
Rob Landley0f34a822005-10-27 22:55:50 +0000121
122 // Exec real init. (This is why we must be pid 1.)
Denis Vlasenko3ace9fa2007-04-18 21:40:30 +0000123 execv(argv[0], argv);
124 bb_perror_msg_and_die("bad init %s", argv[0]);
Rob Landley0f34a822005-10-27 22:55:50 +0000125}