parent
30f9bb2cb1
commit
d49449cd4e
2
Makefile
2
Makefile
|
@ -9,7 +9,7 @@ sbindir = ${prefix}/sbin
|
||||||
mandir = ${prefix}/share/man
|
mandir = ${prefix}/share/man
|
||||||
|
|
||||||
modules.d/99base/switch_root: switch_root.c
|
modules.d/99base/switch_root: switch_root.c
|
||||||
gcc -o modules.d/99base/switch_root switch_root.c
|
gcc -D _GNU_SOURCE -D 'PACKAGE_STRING="dracut"' -std=gnu99 -fsigned-char -g -O2 -o modules.d/99base/switch_root switch_root.c
|
||||||
|
|
||||||
all: modules.d/99base/switch_root
|
all: modules.d/99base/switch_root
|
||||||
|
|
||||||
|
|
204
switch_root.c
204
switch_root.c
|
@ -1,7 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* switchroot.c - switch to new root directory and start init.
|
* switchroot.c - switch to new root directory and start init.
|
||||||
*
|
*
|
||||||
* Copyright 2002-2008 Red Hat, Inc. All rights reserved.
|
* Copyright 2002-2009 Red Hat, Inc. All rights reserved.
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* This program is free software; you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -20,9 +20,6 @@
|
||||||
* Peter Jones <pjones@redhat.com>
|
* Peter Jones <pjones@redhat.com>
|
||||||
* Jeremy Katz <katzj@redhat.com>
|
* Jeremy Katz <katzj@redhat.com>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define _GNU_SOURCE 1
|
|
||||||
|
|
||||||
#include <sys/mount.h>
|
#include <sys/mount.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
@ -35,164 +32,163 @@
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
|
#include <err.h>
|
||||||
|
|
||||||
#ifndef MS_MOVE
|
#ifndef MS_MOVE
|
||||||
#define MS_MOVE 8192
|
#define MS_MOVE 8192
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef MNT_DETACH
|
|
||||||
#define MNT_DETACH 0x2
|
|
||||||
#endif
|
|
||||||
|
|
||||||
enum {
|
|
||||||
ok,
|
|
||||||
err_no_directory,
|
|
||||||
err_usage,
|
|
||||||
};
|
|
||||||
|
|
||||||
/* remove all files/directories below dirName -- don't cross mountpoints */
|
/* remove all files/directories below dirName -- don't cross mountpoints */
|
||||||
static int
|
static int recursiveRemove(char *dirName)
|
||||||
recursiveRemove(char * dirName)
|
|
||||||
{
|
{
|
||||||
struct stat sb,rb;
|
struct stat rb;
|
||||||
DIR *dir;
|
DIR *dir;
|
||||||
struct dirent * d;
|
int rc = -1;
|
||||||
char * strBuf = alloca(strlen(dirName) + 1024);
|
int dfd;
|
||||||
|
|
||||||
if (!(dir = opendir(dirName))) {
|
if (!(dir = opendir(dirName))) {
|
||||||
printf("error opening %s: %m\n", dirName);
|
warn("failed to open %s", dirName);
|
||||||
return 0;
|
goto done;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fstat(dirfd(dir),&rb)) {
|
dfd = dirfd(dir);
|
||||||
printf("unable to stat %s: %m\n", dirName);
|
|
||||||
closedir(dir);
|
if (fstat(dfd, &rb)) {
|
||||||
return 0;
|
warn("failed to stat %s", dirName);
|
||||||
|
goto done;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
while(1) {
|
||||||
|
struct dirent *d;
|
||||||
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
while ((d = readdir(dir))) {
|
if (!(d = readdir(dir))) {
|
||||||
errno = 0;
|
|
||||||
|
|
||||||
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) {
|
|
||||||
errno = 0;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
strcpy(strBuf, dirName);
|
|
||||||
strcat(strBuf, "/");
|
|
||||||
strcat(strBuf, d->d_name);
|
|
||||||
|
|
||||||
if (lstat(strBuf, &sb)) {
|
|
||||||
printf("failed to stat %s: %m\n", strBuf);
|
|
||||||
errno = 0;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* only descend into subdirectories if device is same as dir */
|
|
||||||
if (S_ISDIR(sb.st_mode)) {
|
|
||||||
if (sb.st_dev == rb.st_dev) {
|
|
||||||
recursiveRemove(strBuf);
|
|
||||||
if (rmdir(strBuf))
|
|
||||||
printf("failed to rmdir %s: %m\n", strBuf);
|
|
||||||
}
|
|
||||||
errno = 0;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (unlink(strBuf)) {
|
|
||||||
printf("failed to remove %s: %m\n", strBuf);
|
|
||||||
errno = 0;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (errno) {
|
if (errno) {
|
||||||
closedir(dir);
|
warn("failed to read %s", dirName);
|
||||||
printf("error reading from %s: %m\n", dirName);
|
goto done;
|
||||||
return 1;
|
}
|
||||||
|
break; /* end of directory */
|
||||||
}
|
}
|
||||||
|
|
||||||
closedir(dir);
|
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
|
||||||
|
continue;
|
||||||
|
|
||||||
return 0;
|
if (d->d_type == DT_DIR) {
|
||||||
|
struct stat sb;
|
||||||
|
|
||||||
|
if (fstatat(dfd, d->d_name, &sb, AT_SYMLINK_NOFOLLOW)) {
|
||||||
|
warn("failed to stat %s/%s", dirName, d->d_name);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* remove subdirectories if device is same as dir */
|
||||||
|
if (sb.st_dev == rb.st_dev) {
|
||||||
|
char subdir[ strlen(dirName) +
|
||||||
|
strlen(d->d_name) + 2 ];
|
||||||
|
|
||||||
|
sprintf(subdir, "%s/%s", dirName, d->d_name);
|
||||||
|
recursiveRemove(subdir);
|
||||||
|
} else
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (unlinkat(dfd, d->d_name,
|
||||||
|
d->d_type == DT_DIR ? AT_REMOVEDIR : 0))
|
||||||
|
warn("failed to unlink %s/%s", dirName, d->d_name);
|
||||||
|
}
|
||||||
|
|
||||||
|
rc = 0; /* success */
|
||||||
|
|
||||||
|
done:
|
||||||
|
if (dir)
|
||||||
|
closedir(dir);
|
||||||
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int switchroot(const char *newroot)
|
static int switchroot(const char *newroot)
|
||||||
{
|
{
|
||||||
/* Don't try to unmount the old "/", there's no way to do it. */
|
/* Don't try to unmount the old "/", there's no way to do it. */
|
||||||
const char *umounts[] = { "/dev", "/proc", "/sys", NULL };
|
const char *umounts[] = { "/dev", "/proc", "/sys", NULL };
|
||||||
int errnum;
|
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for (i = 0; umounts[i] != NULL; i++) {
|
for (i = 0; umounts[i] != NULL; i++) {
|
||||||
char newmount[PATH_MAX];
|
char newmount[PATH_MAX];
|
||||||
strcpy(newmount, newroot);
|
|
||||||
strcat(newmount, umounts[i]);
|
snprintf(newmount, sizeof(newmount), "%s%s", newroot, umounts[i]);
|
||||||
|
|
||||||
if (mount(umounts[i], newmount, NULL, MS_MOVE, NULL) < 0) {
|
if (mount(umounts[i], newmount, NULL, MS_MOVE, NULL) < 0) {
|
||||||
fprintf(stderr, "Error mount moving old %s %s %m\n",
|
warn("failed to mount moving %s to %s",
|
||||||
umounts[i], newmount);
|
umounts[i], newmount);
|
||||||
fprintf(stderr, "Forcing unmount of %s\n", umounts[i]);
|
warnx("forcing unmount of %s", umounts[i]);
|
||||||
umount2(umounts[i], MNT_FORCE);
|
umount2(umounts[i], MNT_FORCE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (chdir(newroot) < 0) {
|
if (chdir(newroot)) {
|
||||||
errnum=errno;
|
warn("failed to change directory to %s", newroot);
|
||||||
fprintf(stderr, "switchroot: chdir failed: %m\n");
|
|
||||||
errno=errnum;
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
recursiveRemove("/");
|
recursiveRemove("/");
|
||||||
|
|
||||||
if (mount(newroot, "/", NULL, MS_MOVE, NULL) < 0) {
|
if (mount(newroot, "/", NULL, MS_MOVE, NULL) < 0) {
|
||||||
errnum = errno;
|
warn("failed to mount moving %s to /", newroot);
|
||||||
fprintf(stderr, "switchroot: mount failed: %m\n");
|
|
||||||
errno = errnum;
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (chroot(".")) {
|
if (chroot(".")) {
|
||||||
errnum = errno;
|
warn("failed to change root");
|
||||||
fprintf(stderr, "switchroot: chroot failed: %m\n");
|
return -1;
|
||||||
errno = errnum;
|
|
||||||
return -2;
|
|
||||||
}
|
}
|
||||||
return 1;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void usage(FILE *output)
|
static void usage(FILE *output)
|
||||||
{
|
{
|
||||||
fprintf(output, "usage: switchroot <newrootdir> <init> <args to init>\n");
|
fprintf(output, "usage: %s <newrootdir> <init> <args to init>\n",
|
||||||
if (output == stderr)
|
program_invocation_short_name);
|
||||||
exit(err_usage);
|
exit(output == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
|
||||||
exit(ok);
|
}
|
||||||
|
|
||||||
|
static void version(void)
|
||||||
|
{
|
||||||
|
fprintf(stdout, "%s from %s\n", program_invocation_short_name,
|
||||||
|
PACKAGE_STRING);
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
char *newroot = argv[1];
|
char *newroot, *init, **initargs;
|
||||||
char *init = argv[2];
|
|
||||||
char **initargs = &argv[2];
|
|
||||||
|
|
||||||
if (newroot == NULL || newroot[0] == '\0' ||
|
if (argv[1] && (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")))
|
||||||
init == NULL || init[0] == '\0' ) {
|
usage(stdout);
|
||||||
|
if (argv[1] && (!strcmp(argv[1], "--version") || !strcmp(argv[1], "-V")))
|
||||||
|
version();
|
||||||
|
if (argc < 3)
|
||||||
usage(stderr);
|
usage(stderr);
|
||||||
}
|
|
||||||
|
|
||||||
if (switchroot(newroot) < 0) {
|
newroot = argv[1];
|
||||||
fprintf(stderr, "switchroot has failed. Sorry.\n");
|
init = argv[2];
|
||||||
return 1;
|
initargs = &argv[2];
|
||||||
}
|
|
||||||
if (access(initargs[0], X_OK))
|
if (!*newroot || !*init)
|
||||||
fprintf(stderr, "WARNING: can't access %s\n", initargs[0]);
|
usage(stderr);
|
||||||
|
|
||||||
|
if (switchroot(newroot))
|
||||||
|
errx(EXIT_FAILURE, "failed. Sorry.");
|
||||||
|
|
||||||
|
if (access(init, X_OK))
|
||||||
|
warn("cannot access %s", init);
|
||||||
|
|
||||||
/* get session leader */
|
/* get session leader */
|
||||||
setsid();
|
setsid();
|
||||||
/* set controlling terminal */
|
|
||||||
ioctl (0, TIOCSCTTY, 1);
|
|
||||||
|
|
||||||
execv(initargs[0], initargs);
|
/* set controlling terminal */
|
||||||
|
if (ioctl (0, TIOCSCTTY, 1))
|
||||||
|
warn("failed to TIOCSCTTY");
|
||||||
|
|
||||||
|
execv(init, initargs);
|
||||||
|
err(EXIT_FAILURE, "failed to execute %s", init);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* vim:noet:ts=8:sw=8:sts=8
|
|
||||||
*/
|
|
||||||
|
|
Loading…
Reference in New Issue