From ec9315e56222d38fdbfca5f8e47f05c156ce4927 Mon Sep 17 00:00:00 2001 From: Jeremy Katz Date: Wed, 10 Dec 2008 16:32:21 -0500 Subject: [PATCH] Initial commit First pass at a new initrd implementation. Right now, it's a very simple /init and we use nash's switchroot command via switch_root. The idea is to get a switchroot binary into util-linux that can be used instead generate.sh will let you generate an initrd image. Note that the generator is intentionally super simple right now and is an area that will need a lot of work once we're happier with how the /init process runs --- generate.sh | 36 ++++++++++++++++++++++++++++++++++ init | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++ switch_root | 3 +++ 3 files changed, 95 insertions(+) create mode 100755 generate.sh create mode 100755 init create mode 100755 switch_root diff --git a/generate.sh b/generate.sh new file mode 100755 index 0000000000..c96e9cf6de --- /dev/null +++ b/generate.sh @@ -0,0 +1,36 @@ +#/bin/bash +# Simple script that creates the tree to use for a new initrd +# note that this is not intended to be pretty, nice or anything +# of the sort. the important thing is working + + +source /usr/libexec/initrd-functions + +INITRDOUT=$1 +if [ -z "$INITRDOUT" ]; then + echo "Please specify an initrd file to output" + exit 1 +fi + +tmpdir=$(mktemp -d) + +# executables that we have to have +exe="/bin/bash /bin/mount /bin/mknod /bin/mkdir /sbin/modprobe /sbin/udevd /sbin/udevadm /sbin/nash" +# and some things that are nice for debugging +debugexe="/bin/ls /bin/cat /bin/ln /bin/ps /bin/grep /usr/bin/less" + +# install base files +for binary in $exe $debugexe ; do + inst $binary $tmpdir +done + +# install our files +inst init $tmpdir/init +inst switch_root $tmpdir/sbin/switch_root + +# FIXME: we don't install modules right now, but for the testing we're doing +# everything is already built-in + +pushd $tmpdir >/dev/null +find . |cpio -H newc -o |gzip -9 > $INITRDOUT +popd >/dev/null diff --git a/init b/init new file mode 100755 index 0000000000..5bf9cb9702 --- /dev/null +++ b/init @@ -0,0 +1,56 @@ +#!/bin/bash + +emergency_shell() +{ + echo "Bug in initramfs /init detected. Dropping to a shell. Good luck!" + echo + bash +} +trap "emergency_shell" 0 2 + +echo "Starting initrd..." +export PATH=/sbin:/bin:/usr/sbin:/usr/bin +export TERM=linux + +# /dev/console comes from the built-in initramfs crud in the kernel +# someday, we may need to mkdir /dev first here +exec > /dev/console 2>&1 + +# mount some important things +mkdir /proc +mount -t proc /proc /proc +mkdir /sys +mount -t sysfs /sys /sys +mount -t tmpfs -omode=0755 udev /dev + +# start up udev and trigger cold plugs +/sbin/udevd --daemon +/sbin/udevadm trigger +# FIXME: should we really wait for the queue to settle or just try to +# find the rootfs? +/sbin/udevadm settle --timeout=30 || : + + +NEWROOT=/sysroot +# mount the rootfs +mkdir $NEWROOT +# FIXME: obviously we need to parse this from /proc/cmdline +mount -o ro -t ext3 /dev/sda1 $NEWROOT + +# now we need to prepare to switchroot +mount --bind /dev $NEWROOT/dev + +# FIXME: now for a bunch of boiler-plate mounts. really, we should have +# some like /etc/fstab.sys that's provided by filesystem/initscripts +# and then do mount -f /etc/fstab.sys -a +mount -t proc /proc $NEWROOT/proc +mount -t sysfs /sys $NEWROOT/sys + +# FIXME: load selinux policy + +# FIXME: nash die die die +exec /sbin/switch_root +# davej doesn't like initrd bugs +echo "Something went very badly wrong in the initrd. Please " +echo "file a bug against mkinitrd." +exit 1 diff --git a/switch_root b/switch_root new file mode 100755 index 0000000000..6664ff5b8e --- /dev/null +++ b/switch_root @@ -0,0 +1,3 @@ +#!/sbin/nash + +switchroot