#!/bin/sh

# mkbootdisk
# 11.05.2002 -- version 2.0.2; fixed initrd location by:
# GoTaR <gotar@priv0.onet.pl>
#
# 20.03.2002 -- version 2.0.1; merged some RH changes by:
# GoTaR <gotar@priv0.onet.pl>
#
# 03.02.2002 -- PLDized by:
# djrzulf <przemyslaw@knycz.net>
#
# 19.05.2001 -- updated by:
# GoTaR <gotar@priv0.onet.pl>
#
# basing on mkbootdisk written by:
# Erik Troan <ewt@redhat.com>
# Jakub Jelinek <jj@ultra.linux.cz>

pause=yes
unset kernel
device=/dev/fd0
unset verbose
unset witheth
unset geninitrdargs

MOUNTDIR=${TMPDIR:-/tmp}/mkbootdisk
PATH=/sbin:$PATH
export PATH

VERSION=2.0.1

usage () {
	cat >&2 <<EOF
usage: `basename $0` [--version] [--noprompt] [--geninitrdargs <args>]
	[--device <devicefile>] [--verbose -v] <kernel>
	(ex: `basename $0` --device /dev/fd0 2.2.20-10)
EOF
	exit $1
}

while [ $# -gt 0 ]; do
case $1 in
	--device)
		shift
		device=$1
		;;
	--geninitrdargs)
		shift
		geninitrdargs=$1
		;;
	--help)
		usage 0
		;;
	--noprompt)
		unset pause
		;;
	-v)
		verbose=true
		;;
	--verbose)
		verbose=true
		;;
	--version)
		echo "mkbootdisk: version $VERSION"
		exit 0
		;;
	*)
		if [ -z "$kernel" ]; then
			kernel=$1
		else
			usage
		fi
		;;
esac
shift
done

[ -z "$kernel" ] && usage 1

[ -d /lib/modules/$kernel ] || {
	echo "/lib/modules/$kernel is not a directory." >&2
	exit 1
}

[ -f /boot/vmlinuz-$kernel ] || {
	echo "/boot/vmlinuz-$kernel does not exist." >&2
	exit 1
}

[ -f /etc/modules.conf -a -f /lib/modules/$kernel/modules.dep ] && {
	ethmodule=`awk '/^alias[ \t]*eth0/ {print $3; exit 0}' /etc/modules.conf`
	[ -n "$ethmodule" ] && {
		ethmodule=${ethmodule%.o}
		deps=`grep "/net/$ethmodule.o" /lib/modules/$kernel/modules.dep | head -1 | cut -d: -f2`
		for n in $deps; do
			mod=`basename $n | cut -d. -f1`
			witheth="$witheth --with $mod"
		done
		witheth="$witheth --with $ethmodule"
	}
}

rootdev=`awk '$1 ~ /^[^#]/ && $2 ~ /^\/$/ {print $1; exit 0}' /etc/fstab`

if [ $(echo $rootdev | cut -c1-6) = "LABEL=" ]; then
	rootlabel=$(echo $rootdev | cut -c7-)

	# whee, now we have to look through every partition looking for
	# the thing called $rootlabel, which could be raid. Ick.

	list=$(tail +3 /proc/partitions | awk '{print $4}' | grep '^md')
	list="$list $(tail +3 /proc/partitions |
		awk '{print $4}' | grep -v '^md')"
	rootdev=""
	for dev in $list; do
		if tune2fs -l /dev/$dev >/dev/null 2>&1; then
			label=$(tune2fs -l /dev/$dev 2>/dev/null |
				grep "Filesystem volume name" | awk '{print $4}')
			if [ "$label" = $rootlabel ]; then
				rootdev=/dev/$dev
				break
			fi
		fi
	done
fi

[ -z $rootdev ] && {
	echo 'Cannot find root partition in /etc/fstab.' >&2
	exit 1
}

[ -n "$pause" ] && {
	echo "Insert a disk in $device. Any information on the disk will be lost."
	echo -n "Press <Enter> to continue or ^C to abort: "
	read aline
}

TMPFILE=`mktemp ${TMPDIR:-/tmp}/mkbootdisk-tmp.XXXXXX` || exit 1

[ -n "$verbose" ] && echo -n "Creating filesystem on temp file... "
dd if=/dev/zero of=$TMPFILE bs=1k count=3500 >/dev/null 2>&1
mke2fs -m 0 -q $TMPFILE 3500 >/dev/null 2>&1 <<EOF
y
EOF
[ -n "$verbose" ] && echo "done."

rm -rf $MOUNTDIR
mkdir $MOUNTDIR || {
	echo "Failed to create $MOUNTDIR" >&2
	rm -f $TMPFILE
	exit 1
}
[ -d $MOUNTDIR ] || {
	echo "$MOUNTDIR is not a directory!" >&2
	rm -f $TMPFILE
	exit 1
}

mount -o loop -t ext2 $TMPFILE $MOUNTDIR || {
	rmdir $MOUNTDIR
	rm -f $TMPFILE
	exit 1
}

mkdir $MOUNTDIR/boot
mkdir $MOUNTDIR/etc
[ -n "$verbose" ] && echo -n "Copying /boot/vmlinuz-$kernel... "
cp -p /boot/vmlinuz-$kernel $MOUNTDIR/boot
[ -n "$verbose" ] && echo "done."

[ -n "$verbose" ] && echo -n "Creating initrd image... "
/sbin/geninitrd $geninitrdargs $witheth --ifneeded $MOUNTDIR/boot/initrd.gz $kernel
[ -n "$verbose" ] && echo "done."

cp -p /boot/fd.b $MOUNTDIR/boot
cp -p /boot/second.b $MOUNTDIR/boot

[ -n "$verbose" ] && echo -n "Setting up silo... "

cat > $MOUNTDIR/etc/silo.conf <<EOF
partition=1
prompt
timeout=100
message=/boot/message
image=/boot/vmlinuz-$kernel
	label=linux
	root=$rootdev
EOF
[ -f $MOUNTDIR/boot/initrd.gz ] && echo "	initrd=/boot/initrd.gz" >> $MOUNTDIR/etc/silo.conf

cat >> $MOUNTDIR/etc/silo.conf <<EOF
image=/boot/vmlinuz-$kernel
	label=rescue
	append="load_ramdisk=1 prompt_ramdisk=1"
	root=/dev/fd0
EOF
[ -f $MOUNTDIR/boot/initrd.gz ] && echo "	initrd=/boot/initrd.gz" >> $MOUNTDIR/etc/silo.conf
chmod 644 $MOUNTDIR/etc/silo.conf

cat >> $MOUNTDIR/boot/message <<EOF
Press <return> (or wait 10 seconds) to boot your PLD/Linux system from
$rootdev. You may override the default linux kernel parameters by
typing "linux <params>", followed by <return> if you like. If you want
to use a rescue disk, type "rescue" now.

EOF

[ -n "$verbose" ] && echo "done."

[ -n "$verbose" ] && echo -n "Creating romfs image on $device... "

genromfs -V "Rescue Boot Floppy" -d $MOUNTDIR/ -f $device -a 512 -A 2048,/..

[ -n "$verbose" ] && echo "done."

umount $MOUNTDIR

mount -t romfs $device $MOUNTDIR || {
	rmdir $MOUNTDIR
	rm -f $TMPFILE
	exit 1
}

if [ -n "$verbose" ]; then
	/sbin/silo -r $MOUNTDIR -i /boot/fd.b -b /boot/second.b -C /etc/silo.conf -F
else
	/sbin/silo -r $MOUNTDIR -i /boot/fd.b -b /boot/second.b -C /etc/silo.conf -F >/dev/null
fi

umount $MOUNTDIR
rmdir $MOUNTDIR
rm -f $TMPFILE
