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