#!/bin/sh
#
# Script to boot up a system with autodetection of CD location
# 
# Christoph Lameter <christoph@lameter.com>
# Daniel Robbins <drobbins@gentoo.org>
# (C) 2002 Lameter International Corporation.
# Copyright 2002 Gentoo Technologies, Inc.
# Distributed under the GPL
#
PATH=/usr/sbin:/usr/bin:/sbin:/bin

mount -oremount,rw /
mount /proc
if [ ! -e /dev/.devfsd ]
then
	#mount devfs
	mount -t devfs devfs /dev
fi

echo "--- Probing media drivers"
for i in `cat /driverlist`; do
	modprobe $i 2>/dev/null
done

findmount() {
  while ! mount -t iso9660 -r /dev/cdroms/* /newroot/cdroot; do
    shift
    if [ "$1" = "" ]; then
	echo "CD not found"
	umount /newroot
	ash
	umount /proc
	exec /linuxrc
     fi
  done
  if [ ! -d /newroot/cdroot/isolinux ]; then
    umount /newroot/cdroot
    findmount $*
  fi
  echo "CD medium found on /dev/$1"
}

# Create the new root FS
mkdir newroot
mount -t tmpfs tmpfs /newroot
cd /newroot

# Mount the CD
echo "---- Mounting the CD"
mkdir cdroot
findmount 

# Now the CD is mounted on /newroot/cdroot
echo "---- Copying and symlinking files from CD to populate /"

ln -s /cdroot/bin /cdroot/sbin /cdroot/lib /cdroot/boot /cdroot/usr /cdroot/opt .
mkdir floppy initrd proc cdrom etc var
(cd cdroot;cp -a dev mnt home root tmp etc ..)
ln -s /cdroot/var/db var
ln -s /cdroot/var/empty var
(cd cdroot/var;cp -a cache lib lock log run spool tmp /newroot/var)

echo "---- Switching to tmpfs root filesystem"
CDROOT=1
export CDROOT
umount /proc
pivot_root . initrd
exec chroot . /sbin/init <dev/console >dev/console 2>&1
