blob: e4e9e21ad4ce9d9306af81e06f5800145e7445c1 [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
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
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
Bernhard Reutner-Fischerfebe3c42007-04-04 20:52:03 +000067int switch_root_main(int argc, char **argv);
68int switch_root_main(int argc, 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 Vlasenko92258542006-11-01 10:25:35 +000076 opt_complementary = "-2";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000077 getopt32(argv, "c:", &console);
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
Rob Landleye2b428c2006-03-20 01:43:29 +000094 if (lstat("/init", &st1) || !S_ISREG(st1.st_mode) || statfs("/", &stfs) ||
Rob Landley0f34a822005-10-27 22:55:50 +000095 (stfs.f_type != RAMFS_MAGIC && stfs.f_type != TMPFS_MAGIC) ||
96 getpid() != 1)
97 {
98 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 Vlasenko3ace9fa2007-04-18 21:40:30 +0000108 if (mount(".", "/", NULL, MS_MOVE, NULL) || chroot("."))
Denis Vlasenko51742f42007-04-12 00:32:05 +0000109 bb_error_msg_and_die("error moving root");
Denis Vlasenko3ace9fa2007-04-18 21:40:30 +0000110 xchdir("/");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000111
Rob Landley5d84c232005-12-20 17:25:51 +0000112 // If a new console specified, redirect stdin/stdout/stderr to that.
113
114 if (console) {
115 close(0);
Denis Vlasenko51742f42007-04-12 00:32:05 +0000116 xopen(console, O_RDWR);
Rob Landley5d84c232005-12-20 17:25:51 +0000117 dup2(0, 1);
118 dup2(0, 2);
119 }
Rob Landley0f34a822005-10-27 22:55:50 +0000120
121 // Exec real init. (This is why we must be pid 1.)
Denis Vlasenko3ace9fa2007-04-18 21:40:30 +0000122 execv(argv[0], argv);
123 bb_perror_msg_and_die("bad init %s", argv[0]);
Rob Landley0f34a822005-10-27 22:55:50 +0000124}