#!/bin/sh
# Set up filesystem as root, parse the kernel arguments

export PATH=/sbin:/bin:/usr/sbin:/usr/bin
exec < /dev/console > /dev/console 2> /dev/console

trap "echo \"Something wicked happened.  Press enter for rescue shell.\" ; read NULL ; exec sh" 0
set -e

. /lib/debian-installer/init-debug
debugshell "just booted"

echo "Setting default module loading path"
sysctl "kern.module_path=/lib/modules/`uname -r`;/boot/kernel" > /dev/null

echo "Setting up filesystem, please wait ..."
mount -o remount,rw -t ufs /dev/md0 /
mount /dev
mount /dev/fd
mount /proc
mount /sys
mount -t tmpfs tmpfs /var/cache/anna
mount -t tmpfs tmpfs /var/lib/cdebconf
ln -s /var/run/log /dev/log

# Populate the /var/cache/anna/exclude file
# :XXX: this does not belong here
cat > /var/cache/anna/exclude << EOF
# Uninstallable udebs that are not expected to work, but would take up
# valuable ramdisk space.  Based on http://d-i.debian.org/edos/ reports.
console-setup-linux-fonts-udeb
debian-edu-install-udeb
espeakup-udeb
ltsp-client-builder
lvmcfg
lvmcfg-utils
mdcfg-utils
partman-auto-crypto
partman-auto-lvm
partman-auto-raid
partman-btrfs
partman-crypto-dm
partman-ext3
partman-iscsi
partman-jfs
partman-lvm
partman-md
partman-multipath
partman-nbd
partman-xfs
rescue-mode
EOF

# Get all kernel parameters that can be exported as environment variables
envvars="$(kenv | egrep '^[-_/[:alnum:]]+=".*"$' || true)"

# Parse kernel parameters
init='/bin/busybox init'
for i in $envvars ; do
    case "$i" in
        init="/sbin/init"|init="init")
            # Avoid endless loop
            : ;;
        init=*)
            init=${i#init=}
            ;;
        noshell=*)
            sed -i '/^ttyv[12]/s/^/#/' /etc/inittab
            ;;
    esac
done

# Start devd to process all pending events before init (see #493865)
devd

# Close all open files on the initrd, export kernel parameters and run
# busybox init
debugshell "before init"
eval exec env - $envvars $init < /dev/console > /dev/console 2>&1
