"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 | * |
| 6 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 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 <fcntl.h> |
Rob Landley | 094b9f7 | 2006-05-10 17:41:21 +0000 | [diff] [blame] | 11 | #include <string.h> |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 12 | #include <sys/vfs.h> |
| 13 | #include <unistd.h> |
| 14 | |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 15 | |
| 16 | // Make up for header deficiencies. |
| 17 | |
| 18 | #ifndef RAMFS_MAGIC |
| 19 | #define RAMFS_MAGIC 0x858458f6 |
| 20 | #endif |
| 21 | |
| 22 | #ifndef TMPFS_MAGIC |
| 23 | #define TMPFS_MAGIC 0x01021994 |
| 24 | #endif |
| 25 | |
| 26 | #ifndef MS_MOVE |
| 27 | #define MS_MOVE 8192 |
| 28 | #endif |
| 29 | |
| 30 | dev_t rootdev; |
| 31 | |
| 32 | // Recursively delete contents of rootfs. |
| 33 | |
| 34 | static void delete_contents(char *directory) |
| 35 | { |
| 36 | DIR *dir; |
| 37 | struct dirent *d; |
| 38 | struct stat st; |
| 39 | |
| 40 | // Don't descend into other filesystems |
Rob Landley | e2b428c | 2006-03-20 01:43:29 +0000 | [diff] [blame] | 41 | if (lstat(directory,&st) || st.st_dev != rootdev) return; |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 42 | |
| 43 | // Recursively delete the contents of directories. |
| 44 | if (S_ISDIR(st.st_mode)) { |
| 45 | if((dir = opendir(directory))) { |
| 46 | while ((d = readdir(dir))) { |
| 47 | char *newdir=d->d_name; |
| 48 | |
| 49 | // Skip . and .. |
| 50 | if(*newdir=='.' && (!newdir[1] || (newdir[1]=='.' && !newdir[2]))) |
| 51 | continue; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 52 | |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 53 | // Recurse to delete contents |
| 54 | newdir = alloca(strlen(directory) + strlen(d->d_name) + 2); |
| 55 | sprintf(newdir, "%s/%s", directory, d->d_name); |
| 56 | delete_contents(newdir); |
| 57 | } |
| 58 | closedir(dir); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 59 | |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 60 | // Directory should now be empty. Zap it. |
Rob Landley | 5d84c23 | 2005-12-20 17:25:51 +0000 | [diff] [blame] | 61 | rmdir(directory); |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 62 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 63 | |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 64 | // It wasn't a directory. Zap it. |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 65 | |
Rob Landley | 5d84c23 | 2005-12-20 17:25:51 +0000 | [diff] [blame] | 66 | } else unlink(directory); |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | int switch_root_main(int argc, char *argv[]) |
| 70 | { |
Rob Landley | 5d84c23 | 2005-12-20 17:25:51 +0000 | [diff] [blame] | 71 | char *newroot, *console=NULL; |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 72 | struct stat st1, st2; |
| 73 | struct statfs stfs; |
| 74 | |
| 75 | // Parse args (-c console) |
| 76 | |
| 77 | bb_opt_complementally="-2"; |
| 78 | bb_getopt_ulflags(argc,argv,"c:",&console); |
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 | |
| 82 | newroot=argv[optind++]; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 83 | |
Rob Landley | e2b428c | 2006-03-20 01:43:29 +0000 | [diff] [blame] | 84 | if (chdir(newroot) || lstat(".", &st1) || lstat("/", &st2) || |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 85 | st1.st_dev == st2.st_dev) |
| 86 | { |
| 87 | bb_error_msg_and_die("bad newroot %s",newroot); |
| 88 | } |
| 89 | rootdev=st2.st_dev; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 90 | |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 91 | // Additional sanity checks: we're about to rm -rf /, so be REALLY SURE |
| 92 | // we mean it. (I could make this a CONFIG option, but I would get email |
| 93 | // from all the people who WILL eat their filesystemss.) |
| 94 | |
Rob Landley | e2b428c | 2006-03-20 01:43:29 +0000 | [diff] [blame] | 95 | if (lstat("/init", &st1) || !S_ISREG(st1.st_mode) || statfs("/", &stfs) || |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 96 | (stfs.f_type != RAMFS_MAGIC && stfs.f_type != TMPFS_MAGIC) || |
| 97 | getpid() != 1) |
| 98 | { |
| 99 | bb_error_msg_and_die("not rootfs"); |
| 100 | } |
| 101 | |
| 102 | // Zap everything out of rootdev |
Rob Landley | 5d84c23 | 2005-12-20 17:25:51 +0000 | [diff] [blame] | 103 | |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 104 | delete_contents("/"); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 105 | |
Rob Landley | 5d84c23 | 2005-12-20 17:25:51 +0000 | [diff] [blame] | 106 | // Overmount / with newdir and chroot into it. The chdir is needed to |
| 107 | // recalculate "." and ".." links. |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 108 | |
Rob Landley | 5d84c23 | 2005-12-20 17:25:51 +0000 | [diff] [blame] | 109 | if (mount(".", "/", NULL, MS_MOVE, NULL) || chroot(".") || chdir("/")) |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 110 | bb_error_msg_and_die("moving root"); |
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); |
| 116 | if(open(console, O_RDWR) < 0) |
| 117 | bb_error_msg_and_die("Bad console '%s'",console); |
| 118 | dup2(0, 1); |
| 119 | dup2(0, 2); |
| 120 | } |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 121 | |
| 122 | // Exec real init. (This is why we must be pid 1.) |
Rob Landley | d05981e | 2006-02-14 07:33:45 +0000 | [diff] [blame] | 123 | execv(argv[optind],argv+optind); |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 124 | bb_error_msg_and_die("Bad init '%s'",argv[optind]); |
| 125 | } |