removed switch_root.c

master
Harald Hoyer 2011-04-11 13:11:18 +02:00
parent df60555037
commit e365864bbc
4 changed files with 6 additions and 309 deletions

View File

@ -12,13 +12,7 @@ manpages = dracut.8 dracut.kernel.7 dracut.conf.5 dracut-catimages.8 dracut-gen

.PHONY: install clean archive rpm testimage test all check AUTHORS

ifeq (1,${WITH_SWITCH_ROOT})
targets = modules.d/99base/switch_root
else
targets =
endif

all: $(targets) $(manpages) dracut.html
all: syncheck $(manpages) dracut.html

%: %.xml
xsltproc -o $@ -nonet http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl $<
@ -29,9 +23,6 @@ dracut.html: dracut.xml $(manpages)
--stringparam html.stylesheet http://docs.redhat.com/docs/en-US/Common_Content/css/default.css \
http://docbook.sourceforge.net/release/xsl/current/xhtml/docbook.xsl dracut.xml

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

install:
mkdir -p $(DESTDIR)$(pkglibdir)
mkdir -p $(DESTDIR)$(sbindir)
@ -43,9 +34,6 @@ install:
install -m 0755 dracut-catimages $(DESTDIR)$(sbindir)/dracut-catimages
install -m 0755 mkinitrd-dracut.sh $(DESTDIR)$(sbindir)/mkinitrd
install -m 0755 lsinitrd $(DESTDIR)$(sbindir)/lsinitrd
ifeq (1,${WITH_SWITCH_ROOT})
install -m 0755 modules.d/99base/switch_root $(DESTDIR)$(sbindir)/switch_root
endif
install -m 0644 dracut.conf $(DESTDIR)$(sysconfdir)/dracut.conf
mkdir -p $(DESTDIR)$(sysconfdir)/dracut.conf.d
install -m 0755 dracut-functions $(DESTDIR)$(pkglibdir)/dracut-functions
@ -56,15 +44,11 @@ endif
install -m 0644 dracut-gencmdline.8 $(DESTDIR)$(mandir)/man8
install -m 0644 dracut.conf.5 $(DESTDIR)$(mandir)/man5
install -m 0644 dracut.kernel.7 $(DESTDIR)$(mandir)/man7
ifeq (1,${WITH_SWITCH_ROOT})
rm $(DESTDIR)$(pkglibdir)/modules.d/99base/switch_root
endif

clean:
$(RM) *~
$(RM) */*~
$(RM) */*/*~
$(RM) modules.d/99base/switch_root
$(RM) test-*.img
$(RM) dracut-*.rpm dracut-*.tar.bz2
$(RM) $(manpages) dracut.html

View File

@ -165,14 +165,14 @@ This package contains tools to assemble the local initrd and host configuration.
%setup -q -n %{name}-%{version}%{?dashgittag}

%build
make WITH_SWITCH_ROOT=0%{?with_switch_root}
make

%install
%if 0%{?fedora}
rm -rf $RPM_BUILD_ROOT
%endif
make install DESTDIR=$RPM_BUILD_ROOT sbindir=/sbin \
sysconfdir=/etc mandir=%{_mandir} WITH_SWITCH_ROOT=0%{?with_switch_root}
sysconfdir=/etc mandir=%{_mandir}

echo %{name}-%{version}-%{release} > $RPM_BUILD_ROOT/%{_datadir}/dracut/modules.d/10rpmversion/dracut-version

@ -213,9 +213,6 @@ rm -rf $RPM_BUILD_ROOT
%defattr(-,root,root,0755)
%doc README HACKING TODO COPYING AUTHORS NEWS dracut.html dracut.png dracut.svg
/sbin/dracut
%if 0%{?with_switch_root}
/sbin/switch_root
%endif
%if 0%{?fedora} > 12 || 0%{?rhel} >= 6 || 0%{?suse_version} > 9999
/sbin/mkinitrd
/sbin/lsinitrd

View File

@ -33,14 +33,9 @@ install() {
done

mkdir -p ${initdir}/tmp
# Bail out if switch_root does not exist
if type -P switch_root >/dev/null; then
inst $(type -P switch_root) /sbin/switch_root \
|| dfatal "Failed to install switch_root"
else
inst "$moddir/switch_root" "/sbin/switch_root" \
|| dfatal "Failed to install switch_root"
fi

dracut_install switch_root || dfatal "Failed to install switch_root"

inst "$moddir/dracut-lib.sh" "/lib/dracut-lib.sh"
inst_hook cmdline 10 "$moddir/parse-root-opts.sh"
mkdir -p "${initdir}/var"

View File

@ -1,279 +0,0 @@
/*
* switchroot.c - switch to new root directory and start init.
*
* Copyright 2002-2009 Red Hat, Inc. All rights reserved.
*
* 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
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Peter Jones <pjones@redhat.com>
* Jeremy Katz <katzj@redhat.com>
*/
#include <sys/mount.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <dirent.h>
#include <err.h>
#include <libgen.h>

#ifndef MS_MOVE
#define MS_MOVE 8192
#endif

/* find the enclosing mount point of a path, by examining the backing device
* of parent directories until we reach / or find a parent with a differing
* device.
* result must be freed.
*/
static char *get_parent_mount(const char *path)
{
struct stat sb;
char dir[PATH_MAX];
char tmp[PATH_MAX];
dev_t inner_dev;
int r;

r = stat(path, &sb);
if (r != 0) {
warn("failed to stat %s", path);
return NULL;
}
inner_dev = sb.st_dev;

/* dirname has some annoying properties of modifying the input... */
strncpy(dir, path, PATH_MAX);
dir[PATH_MAX - 1] = 0; /* for safety */

while (1) {
char *parent;

strncpy(tmp, dir, PATH_MAX);
tmp[PATH_MAX - 1] = 0;
parent = dirname(tmp);

r = stat(parent, &sb);
if (r != 0) {
warn("failed to stat %s", parent);
return NULL;
}

/* if the parent directory's device differs then we have found a mount
* point */
if (sb.st_dev != inner_dev)
return strdup(dir);

strncpy(dir, parent, PATH_MAX);
dir[PATH_MAX - 1] = 0;

/* maybe we've reached / */
if (strlen(dir) == 1)
return strdup(dir);
}
}

/* remove all files/directories below dirName -- don't cross mountpoints */
static int recursiveRemove(char *dirName)
{
struct stat rb;
DIR *dir;
int rc = -1;
int dfd;

if (!(dir = opendir(dirName))) {
warn("failed to open %s", dirName);
goto done;
}

dfd = dirfd(dir);

if (fstat(dfd, &rb)) {
warn("failed to stat %s", dirName);
goto done;
}

while(1) {
struct dirent *d;

errno = 0;
if (!(d = readdir(dir))) {
if (errno) {
warn("failed to read %s", dirName);
goto done;
}
break; /* end of directory */
}

if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
continue;

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)
{
/* Don't try to unmount the old "/", there's no way to do it. */
const char *umounts[] = { "/dev", "/proc", "/sys", NULL };
char *newroot_mnt;
const char *chroot_path = NULL;
int i;
int r = -1;

for (i = 0; umounts[i] != NULL; i++) {
char newmount[PATH_MAX];

snprintf(newmount, sizeof(newmount), "%s%s", newroot, umounts[i]);

if (mount(umounts[i], newmount, NULL, MS_MOVE, NULL) < 0) {
warn("failed to mount moving %s to %s",
umounts[i], newmount);
warnx("forcing unmount of %s", umounts[i]);
umount2(umounts[i], MNT_FORCE);
}
}

if (chdir(newroot)) {
warn("failed to change directory to %s", newroot);
return -1;
}

recursiveRemove("/");

newroot_mnt = get_parent_mount(newroot);
if (newroot_mnt && strcmp(newroot, newroot_mnt)) {
/* newroot is not a mount point, so we have to MS_MOVE the parent
* mount point and then chroot in to the "subroot" */
chroot_path = newroot + strlen(newroot_mnt);
newroot = newroot_mnt;

if (chdir(newroot)) {
warn("failed to chdir to newroot mount %s", newroot);
goto err;
}
}

if (mount(newroot, "/", NULL, MS_MOVE, NULL) < 0) {
warn("failed to mount moving %s to /", newroot);
goto err;
}

if (chroot(".")) {
warn("failed to change root");
goto err;
}

if (chroot_path) {
if (chdir(chroot_path)) {
warn("failed to chdir to subroot %s", chroot_path);
goto err;
}

if (chroot(".")) {
warn("failed to change root to subroot");
goto err;
}
}

r = 0;
err:
if (newroot_mnt)
free(newroot_mnt);
return r;
}

static void usage(FILE *output)
{
fprintf(output, "usage: %s <newrootdir> <init> <args to init>\n",
program_invocation_short_name);
exit(output == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
}

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[])
{
char *newroot, *init, **initargs;

if (argv[1] && (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")))
usage(stdout);
if (argv[1] && (!strcmp(argv[1], "--version") || !strcmp(argv[1], "-V")))
version();
if (argc < 3)
usage(stderr);

newroot = argv[1];
init = argv[2];
initargs = &argv[2];

if (!*newroot || !*init)
usage(stderr);

if (switchroot(newroot))
errx(EXIT_FAILURE, "failed. Sorry.");

if (access(init, X_OK))
warn("cannot access %s", init);

/* get session leader */
setsid();

/* set controlling terminal */
if (ioctl (0, TIOCSCTTY, 1))
warn("failed to TIOCSCTTY");

execv(init, initargs);
err(EXIT_FAILURE, "failed to execute %s", init);
}