#!/bin/sh
mkdir -p /proc /newroot /dev
mount -t devtmpfs /dev /dev
mount -t proc /proc /proc
found=0
debug=0
while read maj min blk name other; do
	[ -n "${maj}" -a "${maj}" != "major" ] || continue
	umount=0
	mount -o ro "/dev/${name}" "/newroot" >/dev/null 2>&1 &&
		umount=1 &&
		[ -x /newroot/sbin/init ] && found=1 && break
	[ ${umount} -eq 0 ] || umount /newroot
done < /proc/partitions
read cmdline < /proc/cmdline
for arg in $cmdline; do
	case "$arg" in
		debug-initrd) debug=1;;
	esac
done

if [ $debug -eq 1 ]; then
	echo "dropping into initramfs debug shell"
	/bin/sh
fi

if [ $found -eq 1 ]; then
	echo "Used ${name} for root filesystem"
	umount /dev
	umount /proc
	cd /newroot
	# FIXME: pivot_root doesn't work anymore. probably want switch_root
	# but busybox-static (oneiric, 1:1.18.4-2ubuntu1) does not have
	# switch_root built in
	exec chroot . /sbin/init "$@" <./dev/console >./dev/console 2>&1
fi
echo "badness occurred in ramdisk"
exec /bin/sh
