2006-07-02 19:47:05 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
2006-03-21 18:20:40 +00:00
|
|
|
/* Copyright 2005 Rob Landley <rob@landley.net>
|
|
|
|
*
|
|
|
|
* Switch from rootfs to another filesystem as the root of the mount tree.
|
|
|
|
*
|
2006-09-22 02:52:41 +00:00
|
|
|
* Licensed under GPL version 2, see file LICENSE in this tarball for details.
|
2006-03-21 18:20:40 +00:00
|
|
|
*/
|
2005-10-27 22:55:50 +00:00
|
|
|
|
2007-05-26 19:00:18 +00:00
|
|
|
#include "libbb.h"
|
2005-10-27 22:55:50 +00:00
|
|
|
#include <sys/vfs.h>
|
|
|
|
|
|
|
|
|
|
|
|
// Make up for header deficiencies.
|
|
|
|
|
|
|
|
#ifndef RAMFS_MAGIC
|
|
|
|
#define RAMFS_MAGIC 0x858458f6
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef TMPFS_MAGIC
|
|
|
|
#define TMPFS_MAGIC 0x01021994
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MS_MOVE
|
|
|
|
#define MS_MOVE 8192
|
|
|
|
#endif
|
|
|
|
|
2007-04-12 00:32:05 +00:00
|
|
|
static dev_t rootdev;
|
2005-10-27 22:55:50 +00:00
|
|
|
|
|
|
|
// Recursively delete contents of rootfs.
|
|
|
|
|
2007-01-29 22:51:25 +00:00
|
|
|
static void delete_contents(const char *directory)
|
2005-10-27 22:55:50 +00:00
|
|
|
{
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *d;
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
// Don't descend into other filesystems
|
2006-11-01 10:25:35 +00:00
|
|
|
if (lstat(directory, &st) || st.st_dev != rootdev) return;
|
2005-10-27 22:55:50 +00:00
|
|
|
|
|
|
|
// Recursively delete the contents of directories.
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
2007-04-12 00:32:05 +00:00
|
|
|
dir = opendir(directory);
|
|
|
|
if (dir) {
|
2005-10-27 22:55:50 +00:00
|
|
|
while ((d = readdir(dir))) {
|
2007-04-12 00:32:05 +00:00
|
|
|
char *newdir = d->d_name;
|
2005-10-27 22:55:50 +00:00
|
|
|
|
|
|
|
// Skip . and ..
|
2007-04-12 00:32:05 +00:00
|
|
|
if (*newdir=='.' && (!newdir[1] || (newdir[1]=='.' && !newdir[2])))
|
2005-10-27 22:55:50 +00:00
|
|
|
continue;
|
2006-01-25 00:08:53 +00:00
|
|
|
|
2005-10-27 22:55:50 +00:00
|
|
|
// Recurse to delete contents
|
|
|
|
newdir = alloca(strlen(directory) + strlen(d->d_name) + 2);
|
|
|
|
sprintf(newdir, "%s/%s", directory, d->d_name);
|
|
|
|
delete_contents(newdir);
|
|
|
|
}
|
|
|
|
closedir(dir);
|
2006-01-25 00:08:53 +00:00
|
|
|
|
2005-10-27 22:55:50 +00:00
|
|
|
// Directory should now be empty. Zap it.
|
2005-12-20 17:25:51 +00:00
|
|
|
rmdir(directory);
|
2005-10-27 22:55:50 +00:00
|
|
|
}
|
2006-01-25 00:08:53 +00:00
|
|
|
|
2005-10-27 22:55:50 +00:00
|
|
|
// It wasn't a directory. Zap it.
|
2006-01-25 00:08:53 +00:00
|
|
|
|
2005-12-20 17:25:51 +00:00
|
|
|
} else unlink(directory);
|
2005-10-27 22:55:50 +00:00
|
|
|
}
|
|
|
|
|
2007-04-04 20:52:03 +00:00
|
|
|
int switch_root_main(int argc, char **argv);
|
|
|
|
int switch_root_main(int argc, char **argv)
|
2005-10-27 22:55:50 +00:00
|
|
|
{
|
2007-04-12 00:32:05 +00:00
|
|
|
char *newroot, *console = NULL;
|
2005-10-27 22:55:50 +00:00
|
|
|
struct stat st1, st2;
|
|
|
|
struct statfs stfs;
|
|
|
|
|
|
|
|
// Parse args (-c console)
|
|
|
|
|
2006-11-01 10:25:35 +00:00
|
|
|
opt_complementary = "-2";
|
2007-08-18 15:32:12 +00:00
|
|
|
getopt32(argv, "c:", &console);
|
2007-04-18 21:40:30 +00:00
|
|
|
argv += optind;
|
2006-01-25 00:08:53 +00:00
|
|
|
|
2005-10-27 22:55:50 +00:00
|
|
|
// Change to new root directory and verify it's a different fs.
|
|
|
|
|
2007-04-18 21:40:30 +00:00
|
|
|
newroot = *argv++;
|
2006-01-25 00:08:53 +00:00
|
|
|
|
2007-04-18 21:40:30 +00:00
|
|
|
xchdir(newroot);
|
|
|
|
if (lstat(".", &st1) || lstat("/", &st2) || st1.st_dev == st2.st_dev) {
|
2006-11-01 10:25:35 +00:00
|
|
|
bb_error_msg_and_die("bad newroot %s", newroot);
|
2005-10-27 22:55:50 +00:00
|
|
|
}
|
2007-04-12 00:32:05 +00:00
|
|
|
rootdev = st2.st_dev;
|
2006-01-25 00:08:53 +00:00
|
|
|
|
2005-10-27 22:55:50 +00:00
|
|
|
// Additional sanity checks: we're about to rm -rf /, so be REALLY SURE
|
|
|
|
// we mean it. (I could make this a CONFIG option, but I would get email
|
2007-04-12 00:32:05 +00:00
|
|
|
// from all the people who WILL eat their filesystems.)
|
2005-10-27 22:55:50 +00:00
|
|
|
|
2006-03-20 01:43:29 +00:00
|
|
|
if (lstat("/init", &st1) || !S_ISREG(st1.st_mode) || statfs("/", &stfs) ||
|
2005-10-27 22:55:50 +00:00
|
|
|
(stfs.f_type != RAMFS_MAGIC && stfs.f_type != TMPFS_MAGIC) ||
|
|
|
|
getpid() != 1)
|
|
|
|
{
|
|
|
|
bb_error_msg_and_die("not rootfs");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Zap everything out of rootdev
|
2005-12-20 17:25:51 +00:00
|
|
|
|
2005-10-27 22:55:50 +00:00
|
|
|
delete_contents("/");
|
2006-01-25 00:08:53 +00:00
|
|
|
|
2005-12-20 17:25:51 +00:00
|
|
|
// Overmount / with newdir and chroot into it. The chdir is needed to
|
|
|
|
// recalculate "." and ".." links.
|
2005-10-27 22:55:50 +00:00
|
|
|
|
2007-04-18 21:40:30 +00:00
|
|
|
if (mount(".", "/", NULL, MS_MOVE, NULL) || chroot("."))
|
2007-04-12 00:32:05 +00:00
|
|
|
bb_error_msg_and_die("error moving root");
|
2007-04-18 21:40:30 +00:00
|
|
|
xchdir("/");
|
2006-01-25 00:08:53 +00:00
|
|
|
|
2005-12-20 17:25:51 +00:00
|
|
|
// If a new console specified, redirect stdin/stdout/stderr to that.
|
|
|
|
|
|
|
|
if (console) {
|
|
|
|
close(0);
|
2007-04-12 00:32:05 +00:00
|
|
|
xopen(console, O_RDWR);
|
2005-12-20 17:25:51 +00:00
|
|
|
dup2(0, 1);
|
|
|
|
dup2(0, 2);
|
|
|
|
}
|
2005-10-27 22:55:50 +00:00
|
|
|
|
|
|
|
// Exec real init. (This is why we must be pid 1.)
|
2007-04-18 21:40:30 +00:00
|
|
|
execv(argv[0], argv);
|
|
|
|
bb_perror_msg_and_die("bad init %s", argv[0]);
|
2005-10-27 22:55:50 +00:00
|
|
|
}
|