From: Alex Hornung Date: Sun, 27 Jun 2010 00:51:13 +0000 (+0100) Subject: mkinitrd - add tool and infrastructure for initrd X-Git-Url: https://gitweb.dragonflybsd.org/~lentferj/dragonfly.git/commitdiff_plain/ae14c9db8e2fcad505fc56fbb1c091229828d8f1 mkinitrd - add tool and infrastructure for initrd * Add the tool and necessary infrastructure to be able to easily build initial ramdisks. --- diff --git a/etc/mtree/BSD.usr.dist b/etc/mtree/BSD.usr.dist index 6d3d08eb86..2867a456cb 100644 --- a/etc/mtree/BSD.usr.dist +++ b/etc/mtree/BSD.usr.dist @@ -347,6 +347,12 @@ .. info .. + initrd + etc + .. + bin + .. + .. installer .. isdn diff --git a/sbin/Makefile b/sbin/Makefile index 4da27c90b8..36bb84b27d 100644 --- a/sbin/Makefile +++ b/sbin/Makefile @@ -43,6 +43,7 @@ SUBDIR= adjkerntz \ kldunload \ ldconfig \ md5 \ + mkinitrd \ mknod \ mountctl \ mount \ diff --git a/sbin/mkinitrd/Makefile b/sbin/mkinitrd/Makefile new file mode 100644 index 0000000000..26f383bb73 --- /dev/null +++ b/sbin/mkinitrd/Makefile @@ -0,0 +1,6 @@ +SUBDIR=mini_init + +SCRIPTS=mkinitrd +MAN=mkinitrd.8 + +.include diff --git a/sbin/mkinitrd/mini_init/Makefile b/sbin/mkinitrd/mini_init/Makefile new file mode 100644 index 0000000000..7244fdcbf8 --- /dev/null +++ b/sbin/mkinitrd/mini_init/Makefile @@ -0,0 +1,13 @@ +# @(#)Makefile 8.1 (Berkeley) 7/19/93 +# $FreeBSD: src/sbin/init/Makefile,v 1.20.2.4 2001/08/01 06:37:01 obrien Exp $ +# $DragonFly: src/sbin/init/Makefile,v 1.5 2006/10/17 00:55:41 pavalos Exp $ + +PROG= oinit +NOSHARED=yes +NOMAN= +BINMODE=500 +BINDIR=${SHAREDIR}/initrd/sbin +DPADD= ${LIBUTIL} +LDADD= -lutil + +.include diff --git a/sbin/mkinitrd/mini_init/oinit.c b/sbin/mkinitrd/mini_init/oinit.c new file mode 100644 index 0000000000..3c99220872 --- /dev/null +++ b/sbin/mkinitrd/mini_init/oinit.c @@ -0,0 +1,153 @@ +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Donn Seeley at Berkeley Software Design, Inc. + * + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + * + * @(#) Copyright (c) 1991, 1993 The Regents of the University of California. All rights reserved. + * @(#)init.c 8.1 (Berkeley) 7/15/93 + * $FreeBSD: src/sbin/init/init.c,v 1.38.2.8 2001/10/22 11:27:32 des Exp $ + * $DragonFly: src/sbin/init/init.c,v 1.10 2007/11/25 18:10:07 swildner Exp $ + */ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __STDC__ +#include +#else +#include +#endif + +#include "pathnames.h" + +static int setctty(const char *); +static void runcom(char **); + +/* + * The mother of all processes. + */ +int +main(int argc __unused, char **argv) +{ + /* Dispose of random users. */ + if (getuid() != 0) + errx(1, "%s", strerror(EPERM)); + + runcom(argv); + return 1; +} + +static int +setctty(const char *name) +{ + int fd; + + revoke(name); + if ((fd = open(name, O_RDWR)) == -1) { + exit(1); + } + + if (login_tty(fd) == -1) { + exit(1); + } + + return fd; +} + + +static void +runcom(char **argv_orig) +{ + pid_t pid, wpid; + int status, fd, error; + const char *argv[4]; + struct sigaction sa; + + fd = -1; + + if ((pid = fork()) == 0) { + sigemptyset(&sa.sa_mask); + sa.sa_flags = 0; + sa.sa_handler = SIG_IGN; + sigaction(SIGTSTP, &sa, NULL); + sigaction(SIGHUP, &sa, NULL); + + setctty(_PATH_CONSOLE); + + printf("Child talking, about to call /etc/rc\n"); + argv[0] = "sh"; + argv[1] = _PATH_RUNCOM; + argv[2] = "autoboot" ; + argv[3] = 0; + + sigprocmask(SIG_SETMASK, &sa.sa_mask, NULL); + + execv(_PATH_BSHELL, __DECONST(char **, argv)); + exit(1); /* force single user mode */ + } + + do { + wpid = waitpid(-1, &status, WUNTRACED); + } while (wpid != pid); + + error = chroot_kernel("/new_root"); + if (error) + exit(1); + + error = chroot("/new_root"); + if (error) + exit(1); + + execv("/sbin/init", __DECONST(char **, argv_orig)); +} + diff --git a/sbin/mkinitrd/mini_init/pathnames.h b/sbin/mkinitrd/mini_init/pathnames.h new file mode 100644 index 0000000000..27837f680b --- /dev/null +++ b/sbin/mkinitrd/mini_init/pathnames.h @@ -0,0 +1,43 @@ +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Donn Seeley at Berkeley Software Design, Inc. + * + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + * + * @(#)pathnames.h 8.1 (Berkeley) 6/5/93 + */ + +#include + +#define _PATH_SLOGGER "/sbin/session_logger" +#define _PATH_RUNCOM "/etc/rc" +#define _PATH_RUNDOWN "/etc/rc.shutdown" diff --git a/sbin/mkinitrd/mkinitrd b/sbin/mkinitrd/mkinitrd new file mode 100755 index 0000000000..15906cc3ae --- /dev/null +++ b/sbin/mkinitrd/mkinitrd @@ -0,0 +1,81 @@ +#!/bin/sh + +BUILD_DIR="/tmp/initrd" +INITRD_SIZE="15m" +BIN_TOOLS="echo ln ls mv rm rmdir sh" +SBIN_TOOLS="mount mount_devfs mount_hammer mount_nfs mount_null mount_procfs mount_tmpfs umount iscontrol" +INITRD_DIRS="bin boot dev etc mnt proc sbin tmp var new_root" +CONTENT_DIRS="/usr/share/initrd" + +if [ -e /etc/defaults/mkinitrd.conf ]; then + . /etc/defaults/mkinitrd.conf + echo "Loaded configuration from /etc/defaults/mkinitrd.conf" +fi + + +if [ -e /etc/mkinitrd.conf ]; then + . /etc/mkinitrd.conf + echo "Loaded configuration from /etc/mkinitrd.conf" +fi + +VN_DEV="" + +create_vn() { + if [ ! -d $BUILD_DIR ]; then + mkdir -p $BUILD_DIR + echo "Created build directory $BUILD_DIR" + fi + VN_DEV=`vnconfig -c -S ${INITRD_SIZE} -Z -T vn initrd.img | cut -f 2 -d ' '` + echo "Configured $VN_DEV" + newfs /dev/${VN_DEV}s0 + echo "Formatted initrd image with UFS" + mount /dev/${VN_DEV}s0 $BUILD_DIR + echo "Mounted initrd image on ${BUILD_DIR}" +} + +destroy_vn() { + umount /dev/${VN_DEV}s0 + echo "Unmounted initrd image" + vnconfig -u $VN_DEV + echo "Unconfigured $VN_DEV" +} + +make_hier() { + for dir in ${INITRD_DIRS}; do + mkdir -p ${BUILD_DIR}/${dir} + done + + echo "Created directory structure" +} + +copy_tools() { + for tool in ${BIN_TOOLS}; do + cp /bin/${tool} ${BUILD_DIR}/bin + done + + for tool in ${SBIN_TOOLS}; do + cp /sbin/${tool} ${BUILD_DIR}/sbin + done + + echo "Copied essential tools" +} + +copy_content() { + for dir in ${CONTENT_DIRS}; do + cp -R ${dir}/* ${BUILD_DIR}/ + done +} + +print_info() { + tree ${BUILD_DIR} + df -h | head -n 1 + df -h | grep $VN_DEV +} + +create_vn +make_hier +copy_tools +copy_content +print_info +destroy_vn +mv initrd.img /boot/initrd.img diff --git a/sbin/mkinitrd/mkinitrd.8 b/sbin/mkinitrd/mkinitrd.8 new file mode 100644 index 0000000000..3810d56d05 --- /dev/null +++ b/sbin/mkinitrd/mkinitrd.8 @@ -0,0 +1,88 @@ +.\" +.\" Copyright (c) 2010 The DragonFly Project. 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. +.\" 3. Neither the name of The DragonFly Project nor the names of its +.\" contributors may be used to endorse or promote products derived +.\" from this software without specific, prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 +.\" COPYRIGHT HOLDERS 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. +.\" +.Dd July 1, 2010 +.Os +.Dt MKINITRD 8 +.Sh NAME +.Nm mkinitrd +.Nd build an initial ramdisk image for use as early userland +.Sh SYNOPSIS +.Nm +.\".Op Ar options ... +.\".Ar target ... +.Sh DESCRIPTION +The +.Nm +script builds a ramdisk (md) image based on the UFS filesystem containing +only the most basic tools, such as a minimal +.Xr init 8 , +.Xr sh 1 +as well as many +.Xr mount 8 +utilities. +.Pp +It will also copy the contents of +.Pa /usr/share/initrd +onto the ramdisk, maintaining the same hierarchy. +.Pp +On completion, the final image will be copied to +.Pa /boot/initrd.img , +ready to be used as an early userland. +.Pp +The +.Nm +script will check for +.Pa /etc/defaults/mkinitrd.conf +and +.Pa /etc/mkinitrd.conf +and include the configuration from these files in the given order, allowing +the user to override or add to the following variables: +.Bd -literal -offset indent +BUILD_DIR +INITRD_SIZE +BIN_TOOLS +SBIN_TOOLS +INITRD_DIRS +CONTENT_DIRS +.Ed +.Pp +Adding the following lines to +.Pa /boot/loader.conf +will enable the use of the created initrd image: +.Bd -literal -offset indent +initrd.img_load="YES" +initrd.img_type="md_image" +vfs.root.mountfrom="ufs:md0s0" +.Ed + +.Sh SEE ALSO +.Xr md 4 , +.Xr loader.conf 5 diff --git a/share/Makefile b/share/Makefile index bcb3b12dd9..e0ae871673 100644 --- a/share/Makefile +++ b/share/Makefile @@ -4,7 +4,7 @@ # Do not include `info' in the SUBDIR list, it is handled separately. -SUBDIR= colldef dict doc examples i18n locale man me misc mk monetdef \ +SUBDIR= colldef dict doc examples i18n initrd locale man me misc mk monetdef \ msgdef numericdef skel syscons tabset termcap timedef zoneinfo .if !defined(NO_I4B) diff --git a/share/initrd/Makefile b/share/initrd/Makefile new file mode 100644 index 0000000000..6ed633b6ca --- /dev/null +++ b/share/initrd/Makefile @@ -0,0 +1,16 @@ + +#If you want to add files to other subdirectories, please use a similar +#approach as below, for example for var: +# +#add VAR_FILES= foo +#add ${VAR_FILES} to FILES= +#add a for loop for ${VAR_FILES} + +ETC_FILES= rc +FILES= ${ETC_FILES} + +.for file in ${ETC_FILES} +FILESDIR_${file}=${SHAREDIR}/initrd/etc +.endfor + +.include diff --git a/share/initrd/rc b/share/initrd/rc new file mode 100755 index 0000000000..24e185a96f --- /dev/null +++ b/share/initrd/rc @@ -0,0 +1,11 @@ +#!/bin/sh + +# echo "Starting shell from md boot" +# exec sh + +echo "Mounting new root" +/sbin/mount -t hammer /dev/da0s1d /new_root + +echo "Mounting devfs on new root" +#/sbin/mount_devfs /new_root/dev +/sbin/mount_null /dev /new_root/dev