blob: 21cc99229752368d41db367976997411f8ba49dd [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// Make up for header deficiencies.
Rob Landley0f34a822005-10-27 22:55:50 +000013#ifndef RAMFS_MAGIC
Denis Vlasenko77ad97f2008-05-13 02:27:31 +000014#define RAMFS_MAGIC ((unsigned)0x858458f6)
Rob Landley0f34a822005-10-27 22:55:50 +000015#endif
16
17#ifndef TMPFS_MAGIC
Denis Vlasenko77ad97f2008-05-13 02:27:31 +000018#define TMPFS_MAGIC ((unsigned)0x01021994)
Rob Landley0f34a822005-10-27 22:55:50 +000019#endif
20
21#ifndef MS_MOVE
Denis Vlasenko77ad97f2008-05-13 02:27:31 +000022#define MS_MOVE 8192
Rob Landley0f34a822005-10-27 22:55:50 +000023#endif
24
Rob Landley0f34a822005-10-27 22:55:50 +000025// Recursively delete contents of rootfs.
Denis Vlasenko39acf452008-07-11 23:44:50 +000026static void delete_contents(const char *directory, dev_t rootdev)
Rob Landley0f34a822005-10-27 22:55:50 +000027{
28 DIR *dir;
29 struct dirent *d;
30 struct stat st;
31
32 // Don't descend into other filesystems
Denis Vlasenko39acf452008-07-11 23:44:50 +000033 if (lstat(directory, &st) || st.st_dev != rootdev)
34 return;
Rob Landley0f34a822005-10-27 22:55:50 +000035
36 // Recursively delete the contents of directories.
37 if (S_ISDIR(st.st_mode)) {
Denis Vlasenko51742f42007-04-12 00:32:05 +000038 dir = opendir(directory);
39 if (dir) {
Rob Landley0f34a822005-10-27 22:55:50 +000040 while ((d = readdir(dir))) {
Denis Vlasenko51742f42007-04-12 00:32:05 +000041 char *newdir = d->d_name;
Rob Landley0f34a822005-10-27 22:55:50 +000042
43 // Skip . and ..
Denis Vlasenko39acf452008-07-11 23:44:50 +000044 if (DOT_OR_DOTDOT(newdir))
Rob Landley0f34a822005-10-27 22:55:50 +000045 continue;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000046
Rob Landley0f34a822005-10-27 22:55:50 +000047 // Recurse to delete contents
Denis Vlasenko39acf452008-07-11 23:44:50 +000048 newdir = concat_path_file(directory, newdir);
49 delete_contents(newdir, rootdev);
50 free(newdir);
Rob Landley0f34a822005-10-27 22:55:50 +000051 }
52 closedir(dir);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000053
Rob Landley0f34a822005-10-27 22:55:50 +000054 // Directory should now be empty. Zap it.
Rob Landley5d84c232005-12-20 17:25:51 +000055 rmdir(directory);
Rob Landley0f34a822005-10-27 22:55:50 +000056 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000057
Rob Landley0f34a822005-10-27 22:55:50 +000058 // It wasn't a directory. Zap it.
Rob Landley5d84c232005-12-20 17:25:51 +000059 } else unlink(directory);
Rob Landley0f34a822005-10-27 22:55:50 +000060}
61
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000062int switch_root_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000063int switch_root_main(int argc UNUSED_PARAM, char **argv)
Rob Landley0f34a822005-10-27 22:55:50 +000064{
Denis Vlasenko51742f42007-04-12 00:32:05 +000065 char *newroot, *console = NULL;
Rob Landley0f34a822005-10-27 22:55:50 +000066 struct stat st1, st2;
67 struct statfs stfs;
Denis Vlasenko39acf452008-07-11 23:44:50 +000068 dev_t rootdev;
Rob Landley0f34a822005-10-27 22:55:50 +000069
70 // Parse args (-c console)
Denis Vlasenko6dd03f02008-02-13 17:25:31 +000071 opt_complementary = "-2"; // minimum 2 params
72 getopt32(argv, "+c:", &console); // '+': stop parsing at first non-option
Denis Vlasenko3ace9fa2007-04-18 21:40:30 +000073 argv += optind;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000074
Rob Landley0f34a822005-10-27 22:55:50 +000075 // Change to new root directory and verify it's a different fs.
Denis Vlasenko3ace9fa2007-04-18 21:40:30 +000076 newroot = *argv++;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000077
Denis Vlasenko3ace9fa2007-04-18 21:40:30 +000078 xchdir(newroot);
79 if (lstat(".", &st1) || lstat("/", &st2) || st1.st_dev == st2.st_dev) {
Denis Vlasenko92258542006-11-01 10:25:35 +000080 bb_error_msg_and_die("bad newroot %s", newroot);
Rob Landley0f34a822005-10-27 22:55:50 +000081 }
Denis Vlasenko51742f42007-04-12 00:32:05 +000082 rootdev = st2.st_dev;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000083
Rob Landley0f34a822005-10-27 22:55:50 +000084 // Additional sanity checks: we're about to rm -rf /, so be REALLY SURE
85 // we mean it. (I could make this a CONFIG option, but I would get email
Denis Vlasenko51742f42007-04-12 00:32:05 +000086 // from all the people who WILL eat their filesystems.)
Denis Vlasenko77ad97f2008-05-13 02:27:31 +000087 if (lstat("/init", &st1) || !S_ISREG(st1.st_mode) || statfs("/", &stfs)
88 || (((unsigned)stfs.f_type != RAMFS_MAGIC) && ((unsigned)stfs.f_type != TMPFS_MAGIC))
89 || (getpid() != 1)
90 ) {
Rob Landley0f34a822005-10-27 22:55:50 +000091 bb_error_msg_and_die("not rootfs");
92 }
93
94 // Zap everything out of rootdev
Denis Vlasenko39acf452008-07-11 23:44:50 +000095 delete_contents("/", rootdev);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000096
Rob Landley5d84c232005-12-20 17:25:51 +000097 // Overmount / with newdir and chroot into it. The chdir is needed to
98 // recalculate "." and ".." links.
Denis Vlasenko394eebe2008-02-25 20:30:24 +000099 if (mount(".", "/", NULL, MS_MOVE, NULL))
Denis Vlasenko51742f42007-04-12 00:32:05 +0000100 bb_error_msg_and_die("error moving root");
Denis Vlasenko394eebe2008-02-25 20:30:24 +0000101 xchroot(".");
Denis Vlasenko3ace9fa2007-04-18 21:40:30 +0000102 xchdir("/");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000103
Rob Landley5d84c232005-12-20 17:25:51 +0000104 // If a new console specified, redirect stdin/stdout/stderr to that.
Rob Landley5d84c232005-12-20 17:25:51 +0000105 if (console) {
106 close(0);
Denis Vlasenko51742f42007-04-12 00:32:05 +0000107 xopen(console, O_RDWR);
Denis Vlasenko39acf452008-07-11 23:44:50 +0000108 xdup2(0, 1);
109 xdup2(0, 2);
Rob Landley5d84c232005-12-20 17:25:51 +0000110 }
Rob Landley0f34a822005-10-27 22:55:50 +0000111
112 // Exec real init. (This is why we must be pid 1.)
Denis Vlasenko3ace9fa2007-04-18 21:40:30 +0000113 execv(argv[0], argv);
114 bb_perror_msg_and_die("bad init %s", argv[0]);
Rob Landley0f34a822005-10-27 22:55:50 +0000115}