2005-10-28 06:55:50 +08:00
|
|
|
/* vi:set ts=4:*/
|
2006-03-22 02:20:40 +08:00
|
|
|
/* Copyright 2005 Rob Landley <rob@landley.net>
|
|
|
|
*
|
|
|
|
* Switch from rootfs to another filesystem as the root of the mount tree.
|
|
|
|
*
|
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
|
|
|
*/
|
2005-10-28 06:55:50 +08:00
|
|
|
|
2006-06-15 23:49:36 +08:00
|
|
|
#include "busybox.h"
|
2005-10-28 06:55:50 +08:00
|
|
|
#include <fcntl.h>
|
2006-05-11 01:41:21 +08:00
|
|
|
#include <string.h>
|
2005-10-28 06:55:50 +08:00
|
|
|
#include <sys/vfs.h>
|
|
|
|
#include <unistd.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
|
|
|
|
|
|
|
|
dev_t rootdev;
|
|
|
|
|
|
|
|
// Recursively delete contents of rootfs.
|
|
|
|
|
|
|
|
static void delete_contents(char *directory)
|
|
|
|
{
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *d;
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
// Don't descend into other filesystems
|
2006-03-20 09:43:29 +08:00
|
|
|
if (lstat(directory,&st) || st.st_dev != rootdev) return;
|
2005-10-28 06:55:50 +08:00
|
|
|
|
|
|
|
// Recursively delete the contents of directories.
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
|
|
|
if((dir = opendir(directory))) {
|
|
|
|
while ((d = readdir(dir))) {
|
|
|
|
char *newdir=d->d_name;
|
|
|
|
|
|
|
|
// Skip . and ..
|
|
|
|
if(*newdir=='.' && (!newdir[1] || (newdir[1]=='.' && !newdir[2])))
|
|
|
|
continue;
|
2006-01-25 08:08:53 +08:00
|
|
|
|
2005-10-28 06:55:50 +08: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 08:08:53 +08:00
|
|
|
|
2005-10-28 06:55:50 +08:00
|
|
|
// Directory should now be empty. Zap it.
|
2005-12-21 01:25:51 +08:00
|
|
|
rmdir(directory);
|
2005-10-28 06:55:50 +08:00
|
|
|
}
|
2006-01-25 08:08:53 +08:00
|
|
|
|
2005-10-28 06:55:50 +08:00
|
|
|
// It wasn't a directory. Zap it.
|
2006-01-25 08:08:53 +08:00
|
|
|
|
2005-12-21 01:25:51 +08:00
|
|
|
} else unlink(directory);
|
2005-10-28 06:55:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int switch_root_main(int argc, char *argv[])
|
|
|
|
{
|
2005-12-21 01:25:51 +08:00
|
|
|
char *newroot, *console=NULL;
|
2005-10-28 06:55:50 +08:00
|
|
|
struct stat st1, st2;
|
|
|
|
struct statfs stfs;
|
|
|
|
|
|
|
|
// Parse args (-c console)
|
|
|
|
|
|
|
|
bb_opt_complementally="-2";
|
|
|
|
bb_getopt_ulflags(argc,argv,"c:",&console);
|
2006-01-25 08:08:53 +08:00
|
|
|
|
2005-10-28 06:55:50 +08:00
|
|
|
// Change to new root directory and verify it's a different fs.
|
|
|
|
|
|
|
|
newroot=argv[optind++];
|
2006-01-25 08:08:53 +08:00
|
|
|
|
2006-03-20 09:43:29 +08:00
|
|
|
if (chdir(newroot) || lstat(".", &st1) || lstat("/", &st2) ||
|
2005-10-28 06:55:50 +08:00
|
|
|
st1.st_dev == st2.st_dev)
|
|
|
|
{
|
|
|
|
bb_error_msg_and_die("bad newroot %s",newroot);
|
|
|
|
}
|
|
|
|
rootdev=st2.st_dev;
|
2006-01-25 08:08:53 +08:00
|
|
|
|
2005-10-28 06:55:50 +08: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 eat their filesystemss.)
|
|
|
|
|
2006-03-20 09:43:29 +08:00
|
|
|
if (lstat("/init", &st1) || !S_ISREG(st1.st_mode) || statfs("/", &stfs) ||
|
2005-10-28 06:55:50 +08: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-21 01:25:51 +08:00
|
|
|
|
2005-10-28 06:55:50 +08:00
|
|
|
delete_contents("/");
|
2006-01-25 08:08:53 +08:00
|
|
|
|
2005-12-21 01:25:51 +08:00
|
|
|
// Overmount / with newdir and chroot into it. The chdir is needed to
|
|
|
|
// recalculate "." and ".." links.
|
2005-10-28 06:55:50 +08:00
|
|
|
|
2005-12-21 01:25:51 +08:00
|
|
|
if (mount(".", "/", NULL, MS_MOVE, NULL) || chroot(".") || chdir("/"))
|
2005-10-28 06:55:50 +08:00
|
|
|
bb_error_msg_and_die("moving root");
|
2006-01-25 08:08:53 +08:00
|
|
|
|
2005-12-21 01:25:51 +08:00
|
|
|
// If a new console specified, redirect stdin/stdout/stderr to that.
|
|
|
|
|
|
|
|
if (console) {
|
|
|
|
close(0);
|
|
|
|
if(open(console, O_RDWR) < 0)
|
|
|
|
bb_error_msg_and_die("Bad console '%s'",console);
|
|
|
|
dup2(0, 1);
|
|
|
|
dup2(0, 2);
|
|
|
|
}
|
2005-10-28 06:55:50 +08:00
|
|
|
|
|
|
|
// Exec real init. (This is why we must be pid 1.)
|
2006-02-14 15:33:45 +08:00
|
|
|
execv(argv[optind],argv+optind);
|
2005-10-28 06:55:50 +08:00
|
|
|
bb_error_msg_and_die("Bad init '%s'",argv[optind]);
|
|
|
|
}
|