RFC: enhancing the root mount logic

Ed Schouten ed at 80386.nl
Tue Aug 24 08:03:10 UTC 2010


* Ed Schouten <ed at 80386.nl> wrote:
> See the attachment.

It seems like Mailman ate the attachment.

%%%
/*-
 * Copyright (c) 2010 Ed Schouten <ed at FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/param.h>
#include <sys/linker.h>
#include <sys/mount.h>
#include <sys/uio.h>

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

static void
die(const char *msg)
{
	int fd, serrno;

	serrno = errno;
	fd = open("/dev/console", O_RDWR);
	if (fd != -1 && fd != STDERR_FILENO)
		dup2(fd, STDERR_FILENO);
	errno = serrno;
	perror(msg);
	sleep(10);
	exit(1);
}

static void
domount(const char * const list[], unsigned int elems)
{
	struct iovec iov[elems];
	unsigned int i;

	for (i = 0; i < elems; i++) {
		iov[i].iov_base = (char *)list[i];
		iov[i].iov_len = strlen(list[i]) + 1;
	}

	if (nmount(iov, elems, 0) != 0)
		die(list[1]);
}

static char const * const cdfs[] = {
    "fstype", "cd9660", "from", "/dev/iso9660/freebsd", "fspath", "/ro"
};
static char const * const tmpfs[] = {
    "fstype", "tmpfs", "fspath", "/rw"
};
static char const * const unionfs[] = {
    "fstype", "unionfs", "from", "/ro", "fspath", "/rw", "below", "",
    "whiteout", "whenneeded"
};
static char const * const devfs[] = {
    "fstype", "devfs", "fspath", "/rw/dev"
};

int
main(int argc, char *argv[])
{

	/* Prevent foot shooting. */
	if (getpid() != 1)
		return (1);

	/* Perform mounts. */
	domount(cdfs, sizeof cdfs / sizeof(char *));
	domount(tmpfs, sizeof tmpfs / sizeof(char *));
	domount(unionfs, sizeof unionfs / sizeof(char *));
	domount(devfs, sizeof devfs / sizeof(char *));

	/* chroot() into system and continue boot process. */
	if (chroot("/rw") != 0)
		die("chroot");
	chdir("/");

	/* Execute the real /sbin/init. */
	execv(argv[0], argv);
	die("execv");
	return (1);
}
%%%

-- 
 Ed Schouten <ed at 80386.nl>
 WWW: http://80386.nl/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-arch/attachments/20100824/4926a8ae/attachment.pgp


More information about the freebsd-arch mailing list