"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 | |
Rob Landley | 22d26fc | 2006-06-15 15:49:36 +0000 | [diff] [blame] | 9 | #include "busybox.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 | |
| 27 | dev_t rootdev; |
| 28 | |
| 29 | // Recursively delete contents of rootfs. |
| 30 | |
| 31 | static void delete_contents(char *directory) |
| 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)) { |
| 42 | if((dir = opendir(directory))) { |
| 43 | while ((d = readdir(dir))) { |
| 44 | char *newdir=d->d_name; |
| 45 | |
| 46 | // Skip . and .. |
| 47 | if(*newdir=='.' && (!newdir[1] || (newdir[1]=='.' && !newdir[2]))) |
| 48 | continue; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 49 | |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 50 | // Recurse to delete contents |
| 51 | newdir = alloca(strlen(directory) + strlen(d->d_name) + 2); |
| 52 | sprintf(newdir, "%s/%s", directory, d->d_name); |
| 53 | delete_contents(newdir); |
| 54 | } |
| 55 | closedir(dir); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 56 | |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 57 | // Directory should now be empty. Zap it. |
Rob Landley | 5d84c23 | 2005-12-20 17:25:51 +0000 | [diff] [blame] | 58 | rmdir(directory); |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 59 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 60 | |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 61 | // It wasn't a directory. Zap it. |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 62 | |
Rob Landley | 5d84c23 | 2005-12-20 17:25:51 +0000 | [diff] [blame] | 63 | } else unlink(directory); |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | int switch_root_main(int argc, char *argv[]) |
| 67 | { |
Rob Landley | 5d84c23 | 2005-12-20 17:25:51 +0000 | [diff] [blame] | 68 | char *newroot, *console=NULL; |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 69 | struct stat st1, st2; |
| 70 | struct statfs stfs; |
| 71 | |
| 72 | // Parse args (-c console) |
| 73 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 74 | opt_complementary = "-2"; |
| 75 | getopt32(argc, argv, "c:", &console); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 76 | |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 77 | // Change to new root directory and verify it's a different fs. |
| 78 | |
| 79 | newroot=argv[optind++]; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 80 | |
Rob Landley | e2b428c | 2006-03-20 01:43:29 +0000 | [diff] [blame] | 81 | if (chdir(newroot) || lstat(".", &st1) || lstat("/", &st2) || |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 82 | st1.st_dev == st2.st_dev) |
| 83 | { |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 84 | bb_error_msg_and_die("bad newroot %s", newroot); |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 85 | } |
| 86 | rootdev=st2.st_dev; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 87 | |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 88 | // Additional sanity checks: we're about to rm -rf /, so be REALLY SURE |
| 89 | // we mean it. (I could make this a CONFIG option, but I would get email |
| 90 | // from all the people who WILL eat their filesystemss.) |
| 91 | |
Rob Landley | e2b428c | 2006-03-20 01:43:29 +0000 | [diff] [blame] | 92 | if (lstat("/init", &st1) || !S_ISREG(st1.st_mode) || statfs("/", &stfs) || |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 93 | (stfs.f_type != RAMFS_MAGIC && stfs.f_type != TMPFS_MAGIC) || |
| 94 | getpid() != 1) |
| 95 | { |
| 96 | bb_error_msg_and_die("not rootfs"); |
| 97 | } |
| 98 | |
| 99 | // Zap everything out of rootdev |
Rob Landley | 5d84c23 | 2005-12-20 17:25:51 +0000 | [diff] [blame] | 100 | |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 101 | delete_contents("/"); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 102 | |
Rob Landley | 5d84c23 | 2005-12-20 17:25:51 +0000 | [diff] [blame] | 103 | // Overmount / with newdir and chroot into it. The chdir is needed to |
| 104 | // recalculate "." and ".." links. |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 105 | |
Rob Landley | 5d84c23 | 2005-12-20 17:25:51 +0000 | [diff] [blame] | 106 | if (mount(".", "/", NULL, MS_MOVE, NULL) || chroot(".") || chdir("/")) |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 107 | bb_error_msg_and_die("moving root"); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 108 | |
Rob Landley | 5d84c23 | 2005-12-20 17:25:51 +0000 | [diff] [blame] | 109 | // If a new console specified, redirect stdin/stdout/stderr to that. |
| 110 | |
| 111 | if (console) { |
| 112 | close(0); |
| 113 | if(open(console, O_RDWR) < 0) |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 114 | bb_error_msg_and_die("bad console '%s'", console); |
Rob Landley | 5d84c23 | 2005-12-20 17:25:51 +0000 | [diff] [blame] | 115 | dup2(0, 1); |
| 116 | dup2(0, 2); |
| 117 | } |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 118 | |
| 119 | // Exec real init. (This is why we must be pid 1.) |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 120 | execv(argv[optind], argv+optind); |
| 121 | bb_error_msg_and_die("bad init '%s'", argv[optind]); |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 122 | } |