blob: 9815a6d9e637b3c0c1a45f077b62288aaaf1842a [file] [log] [blame]
Rob Landley0f34a822005-10-27 22:55:50 +00001/* vi:set ts=4:*/
Rob Landley3ea05d32006-03-21 18:20:40 +00002/* 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 Landley0f34a822005-10-27 22:55:50 +00008
9#include <dirent.h>
10#include <fcntl.h>
11#include <stdio.h>
Rob Landley094b9f72006-05-10 17:41:21 +000012#include <string.h>
Rob Landley0f34a822005-10-27 22:55:50 +000013#include <sys/mount.h>
14#include <sys/stat.h>
15#include <sys/types.h>
16#include <sys/vfs.h>
17#include <unistd.h>
18
19#include "busybox.h"
20
21// Make up for header deficiencies.
22
23#ifndef RAMFS_MAGIC
24#define RAMFS_MAGIC 0x858458f6
25#endif
26
27#ifndef TMPFS_MAGIC
28#define TMPFS_MAGIC 0x01021994
29#endif
30
31#ifndef MS_MOVE
32#define MS_MOVE 8192
33#endif
34
35dev_t rootdev;
36
37// Recursively delete contents of rootfs.
38
39static void delete_contents(char *directory)
40{
41 DIR *dir;
42 struct dirent *d;
43 struct stat st;
44
45 // Don't descend into other filesystems
Rob Landleye2b428c2006-03-20 01:43:29 +000046 if (lstat(directory,&st) || st.st_dev != rootdev) return;
Rob Landley0f34a822005-10-27 22:55:50 +000047
48 // Recursively delete the contents of directories.
49 if (S_ISDIR(st.st_mode)) {
50 if((dir = opendir(directory))) {
51 while ((d = readdir(dir))) {
52 char *newdir=d->d_name;
53
54 // Skip . and ..
55 if(*newdir=='.' && (!newdir[1] || (newdir[1]=='.' && !newdir[2])))
56 continue;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000057
Rob Landley0f34a822005-10-27 22:55:50 +000058 // Recurse to delete contents
59 newdir = alloca(strlen(directory) + strlen(d->d_name) + 2);
60 sprintf(newdir, "%s/%s", directory, d->d_name);
61 delete_contents(newdir);
62 }
63 closedir(dir);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000064
Rob Landley0f34a822005-10-27 22:55:50 +000065 // Directory should now be empty. Zap it.
Rob Landley5d84c232005-12-20 17:25:51 +000066 rmdir(directory);
Rob Landley0f34a822005-10-27 22:55:50 +000067 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000068
Rob Landley0f34a822005-10-27 22:55:50 +000069 // It wasn't a directory. Zap it.
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000070
Rob Landley5d84c232005-12-20 17:25:51 +000071 } else unlink(directory);
Rob Landley0f34a822005-10-27 22:55:50 +000072}
73
74int switch_root_main(int argc, char *argv[])
75{
Rob Landley5d84c232005-12-20 17:25:51 +000076 char *newroot, *console=NULL;
Rob Landley0f34a822005-10-27 22:55:50 +000077 struct stat st1, st2;
78 struct statfs stfs;
79
80 // Parse args (-c console)
81
82 bb_opt_complementally="-2";
83 bb_getopt_ulflags(argc,argv,"c:",&console);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000084
Rob Landley0f34a822005-10-27 22:55:50 +000085 // Change to new root directory and verify it's a different fs.
86
87 newroot=argv[optind++];
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000088
Rob Landleye2b428c2006-03-20 01:43:29 +000089 if (chdir(newroot) || lstat(".", &st1) || lstat("/", &st2) ||
Rob Landley0f34a822005-10-27 22:55:50 +000090 st1.st_dev == st2.st_dev)
91 {
92 bb_error_msg_and_die("bad newroot %s",newroot);
93 }
94 rootdev=st2.st_dev;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000095
Rob Landley0f34a822005-10-27 22:55:50 +000096 // Additional sanity checks: we're about to rm -rf /, so be REALLY SURE
97 // we mean it. (I could make this a CONFIG option, but I would get email
98 // from all the people who WILL eat their filesystemss.)
99
Rob Landleye2b428c2006-03-20 01:43:29 +0000100 if (lstat("/init", &st1) || !S_ISREG(st1.st_mode) || statfs("/", &stfs) ||
Rob Landley0f34a822005-10-27 22:55:50 +0000101 (stfs.f_type != RAMFS_MAGIC && stfs.f_type != TMPFS_MAGIC) ||
102 getpid() != 1)
103 {
104 bb_error_msg_and_die("not rootfs");
105 }
106
107 // Zap everything out of rootdev
Rob Landley5d84c232005-12-20 17:25:51 +0000108
Rob Landley0f34a822005-10-27 22:55:50 +0000109 delete_contents("/");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000110
Rob Landley5d84c232005-12-20 17:25:51 +0000111 // Overmount / with newdir and chroot into it. The chdir is needed to
112 // recalculate "." and ".." links.
Rob Landley0f34a822005-10-27 22:55:50 +0000113
Rob Landley5d84c232005-12-20 17:25:51 +0000114 if (mount(".", "/", NULL, MS_MOVE, NULL) || chroot(".") || chdir("/"))
Rob Landley0f34a822005-10-27 22:55:50 +0000115 bb_error_msg_and_die("moving root");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000116
Rob Landley5d84c232005-12-20 17:25:51 +0000117 // If a new console specified, redirect stdin/stdout/stderr to that.
118
119 if (console) {
120 close(0);
121 if(open(console, O_RDWR) < 0)
122 bb_error_msg_and_die("Bad console '%s'",console);
123 dup2(0, 1);
124 dup2(0, 2);
125 }
Rob Landley0f34a822005-10-27 22:55:50 +0000126
127 // Exec real init. (This is why we must be pid 1.)
Rob Landleyd05981e2006-02-14 07:33:45 +0000128 execv(argv[optind],argv+optind);
Rob Landley0f34a822005-10-27 22:55:50 +0000129 bb_error_msg_and_die("Bad init '%s'",argv[optind]);
130}