"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 | |
| 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 Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 27 | static dev_t rootdev; |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 28 | |
| 29 | // Recursively delete contents of rootfs. |
| 30 | |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 31 | static void delete_contents(const char *directory) |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 32 | { |
| 33 | DIR *dir; |
| 34 | struct dirent *d; |
| 35 | struct stat st; |
| 36 | |
| 37 | // Don't descend into other filesystems |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 38 | if (lstat(directory, &st) || st.st_dev != rootdev) return; |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 39 | |
| 40 | // Recursively delete the contents of directories. |
| 41 | if (S_ISDIR(st.st_mode)) { |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 42 | dir = opendir(directory); |
| 43 | if (dir) { |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 44 | while ((d = readdir(dir))) { |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 45 | char *newdir = d->d_name; |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 46 | |
| 47 | // Skip . and .. |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 48 | if (*newdir=='.' && (!newdir[1] || (newdir[1]=='.' && !newdir[2]))) |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 49 | continue; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 50 | |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 51 | // 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 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 | // Directory should now be empty. Zap it. |
Rob Landley | 5d84c23 | 2005-12-20 17:25:51 +0000 | [diff] [blame] | 59 | rmdir(directory); |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 60 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 61 | |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 62 | // It wasn't a directory. Zap it. |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 63 | |
Rob Landley | 5d84c23 | 2005-12-20 17:25:51 +0000 | [diff] [blame] | 64 | } else unlink(directory); |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 67 | int switch_root_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Bernhard Reutner-Fischer | febe3c4 | 2007-04-04 20:52:03 +0000 | [diff] [blame] | 68 | int switch_root_main(int argc, char **argv) |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 69 | { |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 70 | char *newroot, *console = NULL; |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 71 | struct stat st1, st2; |
| 72 | struct statfs stfs; |
| 73 | |
| 74 | // Parse args (-c console) |
| 75 | |
Denis Vlasenko | 6dd03f0 | 2008-02-13 17:25:31 +0000 | [diff] [blame] | 76 | opt_complementary = "-2"; // minimum 2 params |
| 77 | getopt32(argv, "+c:", &console); // '+': stop parsing at first non-option |
Denis Vlasenko | 3ace9fa | 2007-04-18 21:40:30 +0000 | [diff] [blame] | 78 | argv += optind; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 79 | |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 80 | // Change to new root directory and verify it's a different fs. |
| 81 | |
Denis Vlasenko | 3ace9fa | 2007-04-18 21:40:30 +0000 | [diff] [blame] | 82 | newroot = *argv++; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 83 | |
Denis Vlasenko | 3ace9fa | 2007-04-18 21:40:30 +0000 | [diff] [blame] | 84 | xchdir(newroot); |
| 85 | if (lstat(".", &st1) || lstat("/", &st2) || st1.st_dev == st2.st_dev) { |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 86 | bb_error_msg_and_die("bad newroot %s", newroot); |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 87 | } |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 88 | rootdev = st2.st_dev; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 89 | |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 90 | // 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 Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 92 | // from all the people who WILL eat their filesystems.) |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 93 | |
Rob Landley | e2b428c | 2006-03-20 01:43:29 +0000 | [diff] [blame] | 94 | if (lstat("/init", &st1) || !S_ISREG(st1.st_mode) || statfs("/", &stfs) || |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 95 | (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 Landley | 5d84c23 | 2005-12-20 17:25:51 +0000 | [diff] [blame] | 102 | |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 103 | delete_contents("/"); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 104 | |
Rob Landley | 5d84c23 | 2005-12-20 17:25:51 +0000 | [diff] [blame] | 105 | // Overmount / with newdir and chroot into it. The chdir is needed to |
| 106 | // recalculate "." and ".." links. |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 107 | |
Denis Vlasenko | 3ace9fa | 2007-04-18 21:40:30 +0000 | [diff] [blame] | 108 | if (mount(".", "/", NULL, MS_MOVE, NULL) || chroot(".")) |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 109 | bb_error_msg_and_die("error moving root"); |
Denis Vlasenko | 3ace9fa | 2007-04-18 21:40:30 +0000 | [diff] [blame] | 110 | xchdir("/"); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 111 | |
Rob Landley | 5d84c23 | 2005-12-20 17:25:51 +0000 | [diff] [blame] | 112 | // If a new console specified, redirect stdin/stdout/stderr to that. |
| 113 | |
| 114 | if (console) { |
| 115 | close(0); |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 116 | xopen(console, O_RDWR); |
Rob Landley | 5d84c23 | 2005-12-20 17:25:51 +0000 | [diff] [blame] | 117 | dup2(0, 1); |
| 118 | dup2(0, 2); |
| 119 | } |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 120 | |
| 121 | // Exec real init. (This is why we must be pid 1.) |
Denis Vlasenko | 3ace9fa | 2007-04-18 21:40:30 +0000 | [diff] [blame] | 122 | execv(argv[0], argv); |
| 123 | bb_perror_msg_and_die("bad init %s", argv[0]); |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 124 | } |