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
|
|
|
*/
|
2007-05-26 19:00:18 +00:00
|
|
|
#include "libbb.h"
|
2005-10-27 22:55:50 +00:00
|
|
|
#include <sys/vfs.h>
|
|
|
|
|
2009-04-21 20:40:51 +00:00
|
|
|
// Make up for header deficiencies
|
2005-10-27 22:55:50 +00:00
|
|
|
#ifndef RAMFS_MAGIC
|
2008-05-13 02:27:31 +00:00
|
|
|
#define RAMFS_MAGIC ((unsigned)0x858458f6)
|
2005-10-27 22:55:50 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef TMPFS_MAGIC
|
2008-05-13 02:27:31 +00:00
|
|
|
#define TMPFS_MAGIC ((unsigned)0x01021994)
|
2005-10-27 22:55:50 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MS_MOVE
|
2008-05-13 02:27:31 +00:00
|
|
|
#define MS_MOVE 8192
|
2005-10-27 22:55:50 +00:00
|
|
|
#endif
|
|
|
|
|
2009-04-21 20:40:51 +00:00
|
|
|
// Recursively delete contents of rootfs
|
2008-07-11 23:44:50 +00:00
|
|
|
static void delete_contents(const char *directory, dev_t rootdev)
|
2005-10-27 22:55:50 +00:00
|
|
|
{
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *d;
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
// Don't descend into other filesystems
|
2008-07-11 23:44:50 +00:00
|
|
|
if (lstat(directory, &st) || st.st_dev != rootdev)
|
|
|
|
return;
|
2005-10-27 22:55:50 +00:00
|
|
|
|
2009-04-21 20:40:51 +00:00
|
|
|
// Recursively delete the contents of directories
|
2005-10-27 22:55:50 +00:00
|
|
|
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 ..
|
2008-07-11 23:44:50 +00:00
|
|
|
if (DOT_OR_DOTDOT(newdir))
|
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
|
2008-07-11 23:44:50 +00:00
|
|
|
newdir = concat_path_file(directory, newdir);
|
|
|
|
delete_contents(newdir, rootdev);
|
|
|
|
free(newdir);
|
2005-10-27 22:55:50 +00:00
|
|
|
}
|
|
|
|
closedir(dir);
|
2006-01-25 00:08:53 +00:00
|
|
|
|
2009-04-21 20:40:51 +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
|
|
|
}
|
2009-04-21 20:40:51 +00:00
|
|
|
} else {
|
|
|
|
// It wasn't a directory, zap it
|
|
|
|
unlink(directory);
|
|
|
|
}
|
2005-10-27 22:55:50 +00:00
|
|
|
}
|
|
|
|
|
2007-10-11 10:05:36 +00:00
|
|
|
int switch_root_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 09:18:54 +00:00
|
|
|
int switch_root_main(int argc UNUSED_PARAM, char **argv)
|
2005-10-27 22:55:50 +00:00
|
|
|
{
|
2007-04-12 00:32:05 +00:00
|
|
|
char *newroot, *console = NULL;
|
2009-04-21 20:40:51 +00:00
|
|
|
struct stat st;
|
2005-10-27 22:55:50 +00:00
|
|
|
struct statfs stfs;
|
2008-07-11 23:44:50 +00:00
|
|
|
dev_t rootdev;
|
2005-10-27 22:55:50 +00:00
|
|
|
|
|
|
|
// Parse args (-c console)
|
2008-02-13 17:25:31 +00:00
|
|
|
opt_complementary = "-2"; // minimum 2 params
|
2009-04-21 20:40:51 +00:00
|
|
|
getopt32(argv, "+c:", &console); // '+': stop at first non-option
|
2007-04-18 21:40:30 +00:00
|
|
|
argv += optind;
|
|
|
|
newroot = *argv++;
|
2006-01-25 00:08:53 +00:00
|
|
|
|
2009-04-21 20:40:51 +00:00
|
|
|
// Change to new root directory and verify it's a different fs
|
2007-04-18 21:40:30 +00:00
|
|
|
xchdir(newroot);
|
2009-04-21 20:40:51 +00:00
|
|
|
xstat("/", &st);
|
|
|
|
rootdev = st.st_dev;
|
|
|
|
xstat(".", &st);
|
|
|
|
if (st.st_dev == rootdev || getpid() != 1) {
|
|
|
|
// Show usage, it says new root must be a mountpoint
|
|
|
|
// and we must be PID 1
|
|
|
|
bb_show_usage();
|
2005-10-27 22:55:50 +00:00
|
|
|
}
|
2009-04-21 20:40:51 +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
|
|
|
|
// from all the people who WILL destroy their filesystems.
|
|
|
|
statfs("/", &stfs); // this never fails
|
|
|
|
if (lstat("/init", &st) != 0 || !S_ISREG(st.st_mode)
|
|
|
|
|| ((unsigned)stfs.f_type != RAMFS_MAGIC
|
|
|
|
&& (unsigned)stfs.f_type != TMPFS_MAGIC)
|
2008-05-13 02:27:31 +00:00
|
|
|
) {
|
2005-10-27 22:55:50 +00:00
|
|
|
bb_error_msg_and_die("not rootfs");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Zap everything out of rootdev
|
2008-07-11 23:44:50 +00:00
|
|
|
delete_contents("/", rootdev);
|
2006-01-25 00:08:53 +00:00
|
|
|
|
2009-04-21 20:40:51 +00:00
|
|
|
// Overmount / with newdir and chroot into it
|
|
|
|
if (mount(".", "/", NULL, MS_MOVE, NULL)) {
|
|
|
|
// For example, fails when newroot is not a mountpoint
|
2009-04-21 00:29:17 +00:00
|
|
|
bb_perror_msg_and_die("error moving root");
|
2009-04-21 20:40:51 +00:00
|
|
|
}
|
2008-02-25 20:30:24 +00:00
|
|
|
xchroot(".");
|
2009-04-21 20:52:58 +00:00
|
|
|
// The chdir is needed to recalculate "." and ".." links
|
2007-04-18 21:40:30 +00:00
|
|
|
xchdir("/");
|
2006-01-25 00:08:53 +00:00
|
|
|
|
2009-04-21 20:40:51 +00:00
|
|
|
// If a new console specified, redirect stdin/stdout/stderr to it
|
2005-12-20 17:25:51 +00:00
|
|
|
if (console) {
|
|
|
|
close(0);
|
2007-04-12 00:32:05 +00:00
|
|
|
xopen(console, O_RDWR);
|
2008-07-11 23:44:50 +00:00
|
|
|
xdup2(0, 1);
|
|
|
|
xdup2(0, 2);
|
2005-12-20 17:25:51 +00:00
|
|
|
}
|
2005-10-27 22:55:50 +00:00
|
|
|
|
2009-04-21 20:40:51 +00:00
|
|
|
// Exec real init
|
2007-04-18 21:40:30 +00:00
|
|
|
execv(argv[0], argv);
|
2009-04-21 20:40:51 +00:00
|
|
|
bb_perror_msg_and_die("can't execute '%s'", argv[0]);
|
2005-10-27 22:55:50 +00:00
|
|
|
}
|