"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Rob Landley | 3ea05d3 | 2006-03-21 18:20:40 +0000 | [diff] [blame] | 2 | /* Copyright 2005 Rob Landley <rob@landley.net> |
| 3 | * |
| 4 | * Switch from rootfs to another filesystem as the root of the mount tree. |
| 5 | * |
Rob Landley | e9a7a62 | 2006-09-22 02:52:41 +0000 | [diff] [blame] | 6 | * Licensed under GPL version 2, see file LICENSE in this tarball for details. |
Rob Landley | 3ea05d3 | 2006-03-21 18:20:40 +0000 | [diff] [blame] | 7 | */ |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 8 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 9 | #include "libbb.h" |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 10 | #include <sys/vfs.h> |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 11 | |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 12 | // Make up for header deficiencies. |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 13 | #ifndef RAMFS_MAGIC |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 14 | #define RAMFS_MAGIC ((unsigned)0x858458f6) |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 15 | #endif |
| 16 | |
| 17 | #ifndef TMPFS_MAGIC |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 18 | #define TMPFS_MAGIC ((unsigned)0x01021994) |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 19 | #endif |
| 20 | |
| 21 | #ifndef MS_MOVE |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 22 | #define MS_MOVE 8192 |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 23 | #endif |
| 24 | |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 25 | // Recursively delete contents of rootfs. |
Denis Vlasenko | 39acf45 | 2008-07-11 23:44:50 +0000 | [diff] [blame] | 26 | static void delete_contents(const char *directory, dev_t rootdev) |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 27 | { |
| 28 | DIR *dir; |
| 29 | struct dirent *d; |
| 30 | struct stat st; |
| 31 | |
| 32 | // Don't descend into other filesystems |
Denis Vlasenko | 39acf45 | 2008-07-11 23:44:50 +0000 | [diff] [blame] | 33 | if (lstat(directory, &st) || st.st_dev != rootdev) |
| 34 | return; |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 35 | |
| 36 | // Recursively delete the contents of directories. |
| 37 | if (S_ISDIR(st.st_mode)) { |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 38 | dir = opendir(directory); |
| 39 | if (dir) { |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 40 | while ((d = readdir(dir))) { |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 41 | char *newdir = d->d_name; |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 42 | |
| 43 | // Skip . and .. |
Denis Vlasenko | 39acf45 | 2008-07-11 23:44:50 +0000 | [diff] [blame] | 44 | if (DOT_OR_DOTDOT(newdir)) |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 45 | continue; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 46 | |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 47 | // Recurse to delete contents |
Denis Vlasenko | 39acf45 | 2008-07-11 23:44:50 +0000 | [diff] [blame] | 48 | newdir = concat_path_file(directory, newdir); |
| 49 | delete_contents(newdir, rootdev); |
| 50 | free(newdir); |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 51 | } |
| 52 | closedir(dir); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 53 | |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 54 | // Directory should now be empty. Zap it. |
Rob Landley | 5d84c23 | 2005-12-20 17:25:51 +0000 | [diff] [blame] | 55 | rmdir(directory); |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 56 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 57 | |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 58 | // It wasn't a directory. Zap it. |
Rob Landley | 5d84c23 | 2005-12-20 17:25:51 +0000 | [diff] [blame] | 59 | } else unlink(directory); |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 62 | int switch_root_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 63 | int switch_root_main(int argc UNUSED_PARAM, char **argv) |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 64 | { |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 65 | char *newroot, *console = NULL; |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 66 | struct stat st1, st2; |
| 67 | struct statfs stfs; |
Denis Vlasenko | 39acf45 | 2008-07-11 23:44:50 +0000 | [diff] [blame] | 68 | dev_t rootdev; |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 69 | |
| 70 | // Parse args (-c console) |
Denis Vlasenko | 6dd03f0 | 2008-02-13 17:25:31 +0000 | [diff] [blame] | 71 | opt_complementary = "-2"; // minimum 2 params |
| 72 | getopt32(argv, "+c:", &console); // '+': stop parsing at first non-option |
Denis Vlasenko | 3ace9fa | 2007-04-18 21:40:30 +0000 | [diff] [blame] | 73 | argv += optind; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 74 | |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 75 | // Change to new root directory and verify it's a different fs. |
Denis Vlasenko | 3ace9fa | 2007-04-18 21:40:30 +0000 | [diff] [blame] | 76 | newroot = *argv++; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 77 | |
Denis Vlasenko | 3ace9fa | 2007-04-18 21:40:30 +0000 | [diff] [blame] | 78 | xchdir(newroot); |
| 79 | if (lstat(".", &st1) || lstat("/", &st2) || st1.st_dev == st2.st_dev) { |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 80 | bb_error_msg_and_die("bad newroot %s", newroot); |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 81 | } |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 82 | rootdev = st2.st_dev; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 83 | |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 84 | // 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 Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 86 | // from all the people who WILL eat their filesystems.) |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 87 | 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 Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 91 | bb_error_msg_and_die("not rootfs"); |
| 92 | } |
| 93 | |
| 94 | // Zap everything out of rootdev |
Denis Vlasenko | 39acf45 | 2008-07-11 23:44:50 +0000 | [diff] [blame] | 95 | delete_contents("/", rootdev); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 96 | |
Rob Landley | 5d84c23 | 2005-12-20 17:25:51 +0000 | [diff] [blame] | 97 | // Overmount / with newdir and chroot into it. The chdir is needed to |
| 98 | // recalculate "." and ".." links. |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 99 | if (mount(".", "/", NULL, MS_MOVE, NULL)) |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 100 | bb_error_msg_and_die("error moving root"); |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 101 | xchroot("."); |
Denis Vlasenko | 3ace9fa | 2007-04-18 21:40:30 +0000 | [diff] [blame] | 102 | xchdir("/"); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 103 | |
Rob Landley | 5d84c23 | 2005-12-20 17:25:51 +0000 | [diff] [blame] | 104 | // If a new console specified, redirect stdin/stdout/stderr to that. |
Rob Landley | 5d84c23 | 2005-12-20 17:25:51 +0000 | [diff] [blame] | 105 | if (console) { |
| 106 | close(0); |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 107 | xopen(console, O_RDWR); |
Denis Vlasenko | 39acf45 | 2008-07-11 23:44:50 +0000 | [diff] [blame] | 108 | xdup2(0, 1); |
| 109 | xdup2(0, 2); |
Rob Landley | 5d84c23 | 2005-12-20 17:25:51 +0000 | [diff] [blame] | 110 | } |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 111 | |
| 112 | // Exec real init. (This is why we must be pid 1.) |
Denis Vlasenko | 3ace9fa | 2007-04-18 21:40:30 +0000 | [diff] [blame] | 113 | execv(argv[0], argv); |
| 114 | bb_perror_msg_and_die("bad init %s", argv[0]); |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 115 | } |