busybox/util-linux/switch_root.c

125 lines
2.8 KiB
C
Raw Normal View History

/* vi: set sw=4 ts=4: */
/* Copyright 2005 Rob Landley <rob@landley.net>
*
* Switch from rootfs to another filesystem as the root of the mount tree.
*
* Licensed under GPL version 2, see file LICENSE in this tarball for details.
*/
#include "busybox.h"
#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 06:02:05 +05:30
static dev_t rootdev;
// Recursively delete contents of rootfs.
static void delete_contents(const char *directory)
{
DIR *dir;
struct dirent *d;
struct stat st;
// Don't descend into other filesystems
2006-11-01 15:55:35 +05:30
if (lstat(directory, &st) || st.st_dev != rootdev) return;
// Recursively delete the contents of directories.
if (S_ISDIR(st.st_mode)) {
2007-04-12 06:02:05 +05:30
dir = opendir(directory);
if (dir) {
while ((d = readdir(dir))) {
2007-04-12 06:02:05 +05:30
char *newdir = d->d_name;
// Skip . and ..
2007-04-12 06:02:05 +05:30
if (*newdir=='.' && (!newdir[1] || (newdir[1]=='.' && !newdir[2])))
continue;
2006-01-25 05:38:53 +05:30
// 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 05:38:53 +05:30
// Directory should now be empty. Zap it.
rmdir(directory);
}
2006-01-25 05:38:53 +05:30
// It wasn't a directory. Zap it.
2006-01-25 05:38:53 +05:30
} else unlink(directory);
}
int switch_root_main(int argc, char **argv);
int switch_root_main(int argc, char **argv)
{
2007-04-12 06:02:05 +05:30
char *newroot, *console = NULL;
struct stat st1, st2;
struct statfs stfs;
// Parse args (-c console)
2006-11-01 15:55:35 +05:30
opt_complementary = "-2";
getopt32(argc, argv, "c:", &console);
argv += optind;
2006-01-25 05:38:53 +05:30
// Change to new root directory and verify it's a different fs.
newroot = *argv++;
2006-01-25 05:38:53 +05:30
xchdir(newroot);
if (lstat(".", &st1) || lstat("/", &st2) || st1.st_dev == st2.st_dev) {
2006-11-01 15:55:35 +05:30
bb_error_msg_and_die("bad newroot %s", newroot);
}
2007-04-12 06:02:05 +05:30
rootdev = st2.st_dev;
2006-01-25 05:38:53 +05:30
// 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 06:02:05 +05:30
// from all the people who WILL eat their filesystems.)
if (lstat("/init", &st1) || !S_ISREG(st1.st_mode) || statfs("/", &stfs) ||
(stfs.f_type != RAMFS_MAGIC && stfs.f_type != TMPFS_MAGIC) ||
getpid() != 1)
{
bb_error_msg_and_die("not rootfs");
}
// Zap everything out of rootdev
delete_contents("/");
2006-01-25 05:38:53 +05:30
// Overmount / with newdir and chroot into it. The chdir is needed to
// recalculate "." and ".." links.
if (mount(".", "/", NULL, MS_MOVE, NULL) || chroot("."))
2007-04-12 06:02:05 +05:30
bb_error_msg_and_die("error moving root");
xchdir("/");
2006-01-25 05:38:53 +05:30
// If a new console specified, redirect stdin/stdout/stderr to that.
if (console) {
close(0);
2007-04-12 06:02:05 +05:30
xopen(console, O_RDWR);
dup2(0, 1);
dup2(0, 2);
}
// Exec real init. (This is why we must be pid 1.)
execv(argv[0], argv);
bb_perror_msg_and_die("bad init %s", argv[0]);
}