#!/bin/sh
#
# $Id: init 2086 2008-12-14 14:43:11Z bruno $
#
# init script launched during the restore process
#------------------------------------------------------------


CaughtSoftReset() {
	trap SIGTERM
	reboot
}


ConfigureLoggingDaemons() {
	echo -en "Running klogd..."
	klogd -c 2 > /dev/null 2> /dev/null
	echo -en "Done.\nRunning syslogd..."
	syslogd > /dev/null 2> /dev/null
	echo "Done."
	LogIt "klogd and syslogd have been started."
}



CopyBootDevEntry() {
	local outfile devfile
	devfile=/dev/boot_device
	outfile=`cat /BOOTLOADER.DEVICE 2> /dev/null`
	[ ! "$outfile" ] && return 0
	echo -en "Copying boot device to $outfile..."
	if [ -e "$outfile" ] ; then
		echo "OK."
		return 0
	fi
	mkdir -p $outfile
	rmdir $outfile
	cp -pRdf $devfile $outfile
	if [ "$?" -ne "0" ] ; then
		echo "Failed."
		return 1
	else
		echo "OK."
		return 0
	fi
}



Die() {
	LogIt "Fatal error! $1" 1
	exit 1
}



ExtractDevTarballs() {
	cd /
	for fname in ataraid.tgz ida.tgz i2o.tgz rd.tgz raw.tgz cciss.tgz nst.tgz dm.tgz vc.tgz ; do
		if [ ! -e "/$fname" ] ; then
		    LogIt "/$fname not found; cannot extract to /." 1
		else
		    echo -en "\rExtracting /$fname...     "
		    tar -zxf /$fname || LogIt "Error occurred while extracting /$fname"
		fi
	done
	echo -en "\r"
	LogIt "Extracted additional /dev entries OK.     " 1
}



LaunchTerminals() {
	openvt 2 /bin/sh
	openvt 3 /bin/sh
	openvt 4 /bin/sh
	openvt 5 /bin/sh
	openvt 6 /bin/sh
	openvt 7 /bin/sh /sbin/wait-for-petris
	openvt 8 /usr/bin/tail -f $LOGFILE
	# May avoid shell error messages
	chmod 666 /dev/tty* /dev/console
}


LoadKeymap() {
	local fname
	fname=`cat /tmp/KEYMAP-LIVES-HERE 2> /dev/null`
	[ "$fname" = "" ] && return
	if which loadkeys > /dev/null 2> /dev/null ; then
		loadkeys $fname
# loadkmap != loadkeys
#    elif which loadkmap > /dev/null 2> /dev/null ; then
#        loadkmap < $fname
	else
		LogIt "Using default keyboard map." 1
	fi
}


UntarTapeStuff() {
	local old_pwd res
	old_pwd=`pwd`
	cd $GROOVY
	[ "$1" != "" ] && tapedev=$1
	[ ! "$tapedev" ] && tapedev=`grep media-dev /tmp/mondo-restore.cfg 2>/dev/null | tr -s ' ' ' ' | cut -d' ' -f2`
	mt -f $tapedev rewind
	mt -f $tapedev fsf 2
	dd if=$tapedev bs=32k count=1024 | tar -zx
	res=$?
	if [ "$res" -eq "0" ] ; then
		# Store the dev name in case we changed it interactively
		if [ -f "/tmp/mondo-restore.cfg" ]; then
			sed -i "s/^media-dev .*$/media-dev  $tapedev/" /tmp/mondo-restore.cfg
		fi
	fi
	cd $old_pwd
	return $res
}

HandleTape() {
	local res tapedev
	tapedev="" ; # will be set by UntarTapeStuff()

	# Here we can add the USB storage module, it's working even with OBDR !
	modprobe -q usb-storage
	# Tape takes some time to appear
	sleep 10
	cd $GROOVY
	UntarTapeStuff $tapedev
	res=$?
	while [ "$res" -ne "0" ] ; do
		LogIt "$tapedev failed to act as extended data disk for booting." 1
		LogIt "Please specify an alternate tape device," 1
		LogIt "or hit <Enter> to boot from another media." 1
		echo -en "---> "
		read tapedev
		if [ "$tapedev" ] ; then
		    LogIt "User specified $tapedev instead"
		    UntarTapeStuff $tapedev
		    res=$?
		else
		    LogIt "User opted not to specify an alternate tapedev"
		    res=1
	    break
		fi
	done
	# For post-init
	export TAPEDEV=$tapedev

	if [ "$res" -ne "0" ] ; then
		cd /
		LogIt "Failed to use tape as extended datadisk. Reverting to another media." 1
		HandleCDROM
		res=$?
	else
		LogIt "Using tape as extended datadisk. Good." 3
		echo "Using tape as extd ddisk." > /tmp/TAPEDEV-HAS-DATA-DISKS
		res=0
		CD_MOUNTED_OK=yes
	fi
	return $res
}



HandleCDROM() {
	# Just in case we have an iLO ensure we will map it correctly
	echo "$DENY_MODS" | grep -q "usb-storage "
	if [ $? -eq 0 ]; then
		return
	fi
	echo "Activating a potential USB Storage device"
	modprobe -q usb-storage
	for i in 1 2 3 4 5 6 7 8 9 10 ; do
		sleep 1
		echo -en "."
	done

	find-and-mount-cdrom
	res=$?
	if [ "$res" -ne "0" ] ; then
		LogIt "First call to find-and-mount-cdrom failed." 1
		LogIt "Sleeping for 3 seconds and trying again." 1
		sleep 3
		find-and-mount-cdrom
		res=$?
	fi
	if [ "$res" -eq "0" ] ; then
		LogIt "OK, I am running on a CD-ROM. Good." 3
		CD_MOUNTED_OK=yes
	else
		LogIt "You probably not have the right drivers" 3
		LogIt "to support the hardware on which we are running" 3
		LogIt "Your archives are probably fine but" 3
		LogIt "your tape streamer and/or CD-ROM drive are unsupported at that point." 3
		CD_MOUNTED_OK=""
	fi
	return 0
}


PauseForRaids() {
	if [ "`dmesg | grep -i "RAID Controller"`" != "" ] || [ "`dmesg | grep -i "Vendor: 3ware"`" != "" ] ; then
		LogIt "RAID controller(s) detected. Pausing 10 seconds to let them warm up." 1
		echo -en "Pausing..."
		for i in 1 2 3 4 5 6 7 8 9 10 ; do
		    sleep 1
		    echo -en "$(($i*10))%..."
		done
		echo "Done."
	fi
}


# 06/13/2002 --- DuckX's & Hugo's new routine
# 10/11/2003 --- added a simple devfsd.conf file
# 01/24/2005 --- cater for arbitrary locations of devfsd.conf by using the
#                config file path compiled into devfsd
RunDevfsd() {
	loc=`which devfsd 2> /dev/null`
	if [ "$loc" != "" ] ; then
	LogIt "Devfs found. Testing kernel support..."
	if [ ! -e "/dev/.devfsd" ] ; then
	    mount -t devfs devfs /dev 2>> $LOGFILE
		    if [ "$?" -ne "0" ] ; then
		LogIt "Error while trying to mount devfs"
	    else
		LogIt "Devfs mounted OK"
	    fi
	fi
	#check if the kernel supports devfs
	if [ -e "/dev/.devfsd" ] ; then
	    [ -d "/proc/1" ] || mount -n /proc
	    LogIt "Kernel support found. Creating config file and starting devfsd"
	    conffile=`strings $loc | grep -E "devfsd.conf$"`
	    [ "$conffile" ] || conffile="/etc/devfsd.conf"
	    confpath=`echo $conffile | sed "s/\/devfsd\.conf$//"`
	    [ -d "$confpath" ] || mkdir -p $confpath
	    echo -en "REGISTER .* MKOLDCOMPAT\nUNREGISTER .* RMOLDCOMPAT\n" > $conffile
	    devfsd /dev &
	    sleep 5
	else
	    LogIt "No devfs kernel support."
	fi
	fi
}

# Taken from udev management in Mandriva 2008.0. Kudos guys
make_extra_nodes () {
	# there are a few things that sysfs does not export for us.
	# these things are listed in /etc/udev/links.conf
	grep '^[^#]' /etc/udev-links.conf | \
	while read type name arg1; do
		[ "$type" -a "$name" -a ! -e "/dev/$name" -a ! -L "/dev/$name" ] ||continue
		case "$type" in
			L) ln -s $arg1 /dev/$name ;;
			D) mkdir -p /dev/$name ;;
			M) mknod /dev/$name $arg1 && chmod 600 /dev/$name;;
			*) echo "udev-links.conf: unparseable line (%s %s %s)\n" "$type" "$name" "$arg1" ;;
		esac
	done
}

create_dev_makedev() {
  if [ -e /sbin/MAKEDEV ]; then
    ln -sf /sbin/MAKEDEV /dev/MAKEDEV
  else
    ln -sf /bin/true /dev/MAKEDEV
  fi
}

RunUdevd() {
	# Inspiration from Mandriva 2008.0 startup script
	echo "Preparing udev environment..."
	mv /dev /dev.static
	mkdir /dev
	mount -n -o size=5M,mode=0755 -t tmpfs none /dev
	make_extra_nodes
	mount -n -t devpts -o mode=620 none /dev/pts
	mount -n -t tmpfs  none /dev/shm
	if [ -e /proc/sys/kernel/hotplug ]; then
		echo > /proc/sys/kernel/hotplug
	fi
	PKLVL=`cut -f1 /proc/sys/kernel/printk`
	echo 0 > /proc/sys/kernel/printk
	# Many possibilities depending on udev versions
	if [ -x /sbin/startudev ]; then
		/sbin/startudev
	fi
	if [ -x /sbin/start_udev ]; then
		/sbin/start_udev
	fi
	# Depending on udevd version it gives back the hand or not :-(
	/sbin/udevd --daemon &
	sleep 5
	mkdir -p /dev/.udev/queue/
	if [ -x /sbin/udevtrigger ]; then
		/sbin/udevtrigger
	fi
	create_dev_makedev
	if [ -x /sbin/udevsettle ]; then
		/sbin/udevsettle
	fi
	# It seems we need to have more static devs on some distro were
	# udev as some other requirements to be covered later on.
	# So in the mean time:
	for d in `ls /dev.static`; do
		if [ ! -e /dev/$d ]; then
			mv /dev.static/$d /dev
		fi
	done
	echo $PKLVL > /proc/sys/kernel/printk
}

RstHW() {

	# Restore the HW configuration if available (NOT by default)
	answer="NO"

	grep -q nohw /proc/cmdline
	if [ "$?" -eq 0 ]; then
		return
	fi
	if [ -x ./mindi-rsthw ]; then
		grep -q RESTORE /proc/cmdline
		if [ "$?" -ne 0 ]; then
			#clear
			echo "*********************************************************************"
			echo "Do you want to restore the HW configuration of the original machine ?"
			echo "(This may dammage your hardware so be sure to check twice before saying yes)"
			echo "*********************************************************************"
			echo "Please confirm by typing YES exactly as written here (NO by default)"
			echo -n "--> "
			read answer
		else
			answer="YES"
		fi
		if [ "$answer" = "YES" ] ; then
			./mindi-rsthw
		fi
	fi
}


#RunDevfsd() {
#    loc=`which devfsd 2> /dev/null`
#    if [ "$loc" != "" ] ; then
#        LogIt "Starting devfsd"
#        devfsd /dev &
#        sleep 5
#    fi
#}



StartUSBKbd() {
# Prepare minimal USB env in case we have USB kbd such as with iLO
[ -d /proc/bus/usb ] && ! grep -q /proc/bus/usb /proc/mounts && mount -t usbfs none /proc/bus/usb
echo "$DENY_MODS" | grep -Eq 'uhcd_hci |usbhid |usbcore '
if [ $? -eq 0 ]; then
	return
fi
modprobe -q uhcd_hci
modprobe -q usbhid
}



StartLvms() {
	if [ -e "/tmp/i-want-my-lvm" ] ; then
		LogIt "Scanning LVM's..." 1
		if which lvm ; then
	    	modprobe -q dm-mod
	    	modprobe -q dm_mod
	    	lvm vgscan --mknodes
		else
	    	vgscan
		fi
		grep -E "^#.*vgchange" /tmp/i-want-my-lvm | sed "s/^#[ ]*//" > /tmp/start-lvm
		chmod +x /tmp/start-lvm
		echo -en "Starting LVM's..."
		/tmp/start-lvm &
		for i in 1 2 3 4 5 ; do
	    	echo -en "."
	    	sleep 1
		done
		echo "Done."
	fi
# If necessary, cannibalize 'analyze-my-lvm'; copy some of its code here,
# pipe vgscan's output, strip it, run 'vgchange' on its output, etc.etc.
	LogIt "LVM's have been started."
}


StartMpath() {
	if [ "`grep -i nompath /proc/cmdline`" ]; then
		return;
	fi
	echo "Starting Mpath..."
	if [ -x /sbin/multipath ]; then
		mkdir -p /var/lib/multipath
		cat > /etc/multipath.conf << EOF
defaults {
        user_friendly_names yes
}
EOF
		/sbin/multipath -v 0
		if [ -x /sbin/kpartx ]; then
			/sbin/dmsetup ls --target multipath --exec "/sbin/kpartx -a -p p"
		fi
	fi
}

StartRaids() {
	local raid_devices i

	raid_devices=`grep /dev/md /tmp/mountlist.txt | cut -d' ' -f1`
	if which raidstart > /dev/null 2> /dev/null ; then
		for i in $raid_devices ; do
			if grep `basename $i` /proc/mdstat > /dev/null 2> /dev/null ; then
		    	LogIt "$i is started already; no need to run 'raidstart $i'" 1
			else
	    		LogIt "Running 'raidstart $i'" 1
	    		raidstart $i
			fi
		done
	elif which mdrun > /dev/null 2> /dev/null ; then
		LogIt "Running 'mdrun'" 1
		mdrun
	elif which mdadm > /dev/null 2> /dev/null ; then
		LogIt "Running 'mdadm'" 1
		for i in $raid_devices ; do
			if grep `basename $i` /proc/mdstat > /dev/null 2> /dev/null ; then
		    	LogIt "$i is started already; no need to run 'mdadm $i'" 1
			else
				if [ -f /etc/mdadm.conf ] ; then
					LogIt "Running 'mdadm $i' with user supplied /etc/mdadm.conf" 1
					mdadm -A $i -c /etc/mdadm.conf
				elif [ -f /etc/mdadm/mdadm.conf ] ; then
					LogIt "Running 'mdadm $i' with user supplied /etc/mdadm/mdadm.conf" 1
					mdadm -A $i -c /etc/mdadm/mdadm.conf
				else
	    			LogIt "Running 'mdadm $i'" 1
	    			mdadm -Ac partitions -m dev $i
				fi
			fi
		done
	elif which dmraid > /dev/null 2> /dev/null ; then
		if [ "`grep -i nodmraid /proc/cmdline`" ]; then
			return;
		fi
		for x in $(/sbin/dmraid -ay -i -p -t 2>/dev/null | grep -E  -iv "^no " |awk -F ':' '{ print $1 }') ; do
			echo "Please send the result of /sbin/dmraid -ay -i -p -t to the devteam !!!"
			return
			#dmname=$(resolve_dm_name $x)
			#[ -z "$dmname" ] && continue
			#/sbin/dmraid  -ay -i -p "$dmname" >/dev/null 2>&1
			#/sbin/kpartx -a -p p "/dev/mapper/$dmname"
		done
	else
		LogIt "Warning: Neither 'raidstart' nor 'mdrun''found. RAID devices may not have started." 1
	fi

}


TryAgainToFindCD() {
	local res
	mount | grep /mnt/cdrom && return 0
	[ "`grep "backup_media_type" /tmp/mondo-restore.cfg 2> /dev/null | grep "cdstream"`" ] && return
	LogIt "Trying to mount CD-ROM a 2nd time..."
	find-and-mount-cdrom --second-try
	res=$?
	if [ "$res" -eq "0" ] ; then
		CD_MOUNTED_OK=yes
		LogIt "CD-ROM drive mounted successfully." 1
	else
		LogIt "I still cannot find or mount the CD-ROM drive, by the way."
	fi
}



UseTmpfs()
{
	local mount_cmd
	echo -en "Mounting /tmp/tmpfs..."
	mkdir -p /tmp/tmpfs
# For technical reasons, some sets are as large as 16MB.
# I am allowing 32MB because selective restore occupies a lot of space.
	for size in 64m 48m 40m 32m ; do
		mount_cmd="mount /dev/shm -t tmpfs -o size=$size" ; # was 34m until 04/2003
		LogIt "Trying '$mount_cmd'"
		$mount_cmd /tmp/tmpfs 2>> $LOGFILE
		res=$?
		[ "$res" -eq "0" ] && break
	done
	if [ "$res" -ne "0" ] ; then
		LogIt "Failed. I could not run '$mount_cmd /tmp/tmpfs'. Your kernel is BROKEN or you do not have enough RAM." 1
		umount /tmp/tmpfs > /dev/null 2> /dev/null
		rmdir /tmp/tmpfs
		ln -sf /mnt/RESTORING/tmp /tmp/tmpfs; # used by mondo-restore
		LogIt "Failed to mount /tmp/tmpfs; using ugly softlink instead"
	else
		LogIt "Great. Pivot succeeded w/ size=$size" 1
		echo -en "Pivoting /tmp..."
		umount /tmp/tmpfs
		mkdir -p /tmp.old
		mv /tmp/* /tmp.old/
		# Try to Deal with a busybox bug on inexistant links
		cp /tmp/* /tmp.old/
		rm -f /tmp/*
		$mount_cmd /tmp
		mv /tmp.old/* /tmp/
		# Try to Deal with a busybox bug on inexistant links
		cp /tmp.old/* /tmp/
		rm -rf /tmp.old
		mkdir -p /tmp/tmpfs
		mkdir -p $GROOVY
	echo "Done."
	LogIt "Successfully mounted dynamic /tmp ramdisk"
#	mkdir -p /tmp/tmpfs/var
#        mv -f /var/log /tmp/tmpfs/var/log
#        ln -sf /tmp/tmpfs/var/log /var/log
	fi
}


WelcomeMessage()
{
	export ARCH=`uname -m`

	echo "********************************************************************"
	echo "MINDI-LINUX by Mondo Dev Team - web site: http://www.mondorescue.org"
	echo "Boot disk based on AlfaLinux & Trinux. BusyBox by Erik Andersen."
which petris > /dev/null 2> /dev/null && echo "Petris was written by Peter Seidler <p.seidler@mail1.stofanet.dk>."
	echo "Executables and source code are covered by the GNU GPL. No warranty."
	echo "running on $ARCH"
	echo "********************************************************************"
}


EnableCcissIfAppropriate() {
	local i fname
	for i in 0 1 2 3 ; do
		fname="/proc/driver/cciss/cciss$i"
		if [ -e "$fname" ] ; then
	    	LogIt "Engaging $fname"
	    	echo "engage scsi" > $fname
	    	LogIt "...result=$?"
		fi
	done
}

ModprobeAllModules() {

	for m in `find /lib/modules -name '*\.ko*' -o -name '*\.o*'`; do
		k=`basename $m | sed 's/\.ko.*$//'`
		j=`basename $k | sed 's/\.o.*$//'`
		echo "$DENY_MODS" | grep -q "$j "
		if [ $? -eq 0 ]; then
			echo "Denying $j..."
			continue
		fi
		echo "Probing $j..."
		modprobe -q $j
	done
}

ExtractDataDisksAndLoadModules() {
	echo "Installing additional tools ..."
	install-additional-tools
	# Keep the kernel silent again
	PKLVL=`cut -f1 /proc/sys/kernel/printk`
	echo 0 > /proc/sys/kernel/printk
	ModprobeAllModules
	echo $PKLVL > /proc/sys/kernel/printk

	# Retry failed udev events now that local filesystems are mounted read-write
	# (useful for rules creating network ifcfg files)
	if [ -e "/tmp/USE-UDEV" ] ; then
		if [ -x /sbin/udevadm ]; then
			/sbin/udevadm trigger --retry-failed
		fi
	fi
}

# ------------------------ main -----------------------

MINDI_VER=2.0.5
MINDI_REV=2156
trap CaughtSoftReset SIGTERM
LOGFILE=/var/log/mondorestore.log
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/mondo:/usr/games
GROOVY=/tmp/groovy-stuff
USER=root
export PATH GROOVY USER LOGFILE

echo "Welcome to init (from mindi ${MINDI_VER}-r${MINDI_REV})"

mount -o remount rw /
[ ! "$GROOVY" ] && Die "I'm not groovy!"
for i in $GROOVY /mnt/cdrom /mnt/isodir /var/local/petris /tmp/isodir; do
	mkdir -p $i
done
#/bin/update
mount /proc/ /proc -v -t proc 
mkdir /sys 2> /dev/null
mount /sys/ /sys -v -t sysfs 2>> $LOGFILE
# For ESX 3
[ -d /proc/vmware ] && ! grep -q /vmfs /proc/mounts && mount -t vmfs /vmfs /vmfs
rm -f /foozero

if [ "`grep -i denymods /proc/cmdline`" ]; then
	export DENY_MODS="`cat /proc/cmdline | sed 's~.*denymods=\"\(.*\)\".*~\1~'` mondonone"
else
	export DENY_MODS=" "
fi
if [ "`grep -i 'obdr ' /tmp/mondo-restore.cfg 2> /dev/null`" ]; then
	# Do not try to load usb storage when dealing with OBDR it makes the modprobe hang :-(
	export DENY_MODS="usb-storage $DENY_MODS"
fi
if [ "`grep -i forcemods /proc/cmdline`" ]; then
	export FORCE_MODS="`cat /proc/cmdline | sed 's~.*forcemods=\"\(.*\)\".*~\1~'` mondonone"
else
	export FORCE_MODS=" "
fi

echo "Activating a potential USB keyboard/mouse"
StartUSBKbd

if [ -f /proc/sys/kernel/exec-shield ]; then
	echo 0 > /proc/sys/kernel/exec-shield 2>> /dev/null
fi

if [ -f /proc/modules ]; then
	echo "/sbin/modprobe" > /proc/sys/kernel/modprobe
fi

if [ -e "/tmp/USE-UDEV" ] ; then
	RunUdevd
fi
ExtractDevTarballs
LaunchTerminals

# Keeping kernel silent  for module insertion
PKLVL=`cut -f1 /proc/sys/kernel/printk`
echo 0 > /proc/sys/kernel/printk
ModprobeAllModules
echo $PKLVL > /proc/sys/kernel/printk

EnableCcissIfAppropriate
#-------------------------------
#WHOLIVESINAPINEAPPLEUNDERTHESEA#;# --- don't touch this :)
#-------------------------------
UseTmpfs
if [ ! -e "/tmp/mondo-restore.cfg" ] ; then
	LogIt "Warning - /tmp/mondo-restore.cfg not found"
fi

if [ "`grep -i 'obdr ' /tmp/mondo-restore.cfg 2>/dev/null`" ] || [ "`grep -i ' udev' /tmp/mondo-restore.cfg 2>/dev/null`" ]; then
	HandleTape
	ExtractDataDisksAndLoadModules
elif [ "`grep -i pxe /proc/cmdline`" ]; then
	# We need to get here exported variables from start-nfs
	. /sbin/start-nfs

	# Simulate a local CD
	echo "/mnt/cdrom" > /tmp/CDROM-LIVES-HERE
	CD_MOUNTED_OK=yes
	ExtractDataDisksAndLoadModules
elif [ "`grep -i usb= /proc/cmdline`" ] || [ "`grep -i usb /tmp/mondo-restore.cfg 2>/dev/null | grep media-type`" ]; then
	. /sbin/start-usb

	# Simulate a local CD
	echo "/mnt/cdrom" > /tmp/CDROM-LIVES-HERE
	CD_MOUNTED_OK=yes
	ExtractDataDisksAndLoadModules
else
	HandleCDROM
	ExtractDataDisksAndLoadModules
	# We need to get here exported variables from start-nfs
	. /sbin/start-nfs
fi
res=$?
ConfigureLoggingDaemons
if [ -e "/tmp/USE-DEVFS" ] ; then
	umount /mnt/cdrom 2> /dev/null
	mv /dev/cdrom /cdrom.lnk 2> /dev/null
	CD_MOUNTED_OK=""
	RunDevfsd
fi
LoadKeymap
WelcomeMessage
RstHW
echo "Starting potential Raid/LVMs ..."
PauseForRaids
StartRaids
StartMpath
StartLvms
CopyBootDevEntry
mkdir -p /tmp/tmpfs
sleep 2
#clear
if [ -e "/dev/md0" ] && [ ! -e "/dev/md/0" ] && [ "`grep /dev/md/ /tmp/mountlist.txt`" != "" ] ; then
	LogIt "Creating /dev/md/* softlinks just in case." 1
	mkdir -p /dev/md
	cp -af /dev/md0 /dev/md/0 2> /dev/null
	cp -af /dev/md1 /dev/md/1 2> /dev/null
	cp -af /dev/md2 /dev/md/2 2> /dev/null
fi
[ -e "/tmp/mountlist.txt" ] && cp -f /tmp/mountlist.txt /tmp/mountlist.original

if ! [ "`grep -i "pxe" /proc/cmdline`" ] ; then
	res="`cat /mnt/cdrom/archives/THIS-CD-NUMBER 2> /dev/null`"
	[ "$res" != "1" ] && [ "$res" != "" ] && Die "This is CD #$res in the series. Please insert CD #1."
	[ -e "/cdrom.lnk" ] && mv -f /cdrom.lnk /dev/cdrom && mount /dev/cdrom /mnt/cdrom && CD_MOUNTED_OK=yes
	[ "$CD_MOUNTED_OK" != "yes" ] && TryAgainToFindCD
	#mount | grep cdrom > /dev/null 2> /dev/null || mount /dev/cdrom /mnt/cdrom >> $LOGFILE 2>> $LOGFILE
fi
hack-cfg-if-necessary || LogIt "Cannot find hack-cfg-if-necessary"

# Log some useful info
LogIt "init (from mindi v$MINDI_VER-r${MINDI_REV})"
LogIt "/proc/cmdline is:"
cat /proc/cmdline  >> $LOGFILE
LogIt "df result:"
LogIt "----------"
df >> $LOGFILE
LogIt "-------------"
LogIt "mount result:"
LogIt "-------------"
mount >> $LOGFILE
LogIt "-------------"
LogIt "lsmod result:"
LogIt "-------------"
lsmod >> $LOGFILE
LogIt "-------------"
LogIt "dmesg result:"
LogIt "-------------"
dmesg >> $LOGFILE
LogIt "-------------"
LogIt "/proc/swaps:" >> $LOGFILE
LogIt "-------------"
cat /proc/swaps  >> $LOGFILE
LogIt "-------------"
LogIt "/proc/filesystems:" >> $LOGFILE
LogIt "-------------"
cat /proc/filesystems  >> $LOGFILE
LogIt "-------------"
LogIt "/proc/partitions:" >> $LOGFILE
LogIt "-------------"
cat /proc/partitions  >> $LOGFILE


ide-opt

#ctrlaltdel soft
for path in /usr.bin /usr/bin ; do
	fname=$path/nano
	[ -e "$fname" ] && ln -sf $fname /usr/bin/pico
done
res=`which nano 2> /dev/null`
[ "$res" ] && ln -sf /usr/bin/
for i in null stdout stdin stderr ; do
	cp -af /dev/$i /tmp
done
# Not sure it's still needed, but shouldn't hurt
if [ -e "/tmp/USE-UDEV" ] ; then
	if [ -x /sbin/udevadm ]; then
		/sbin/udevadm trigger --retry-failed
	fi
fi
if grep "backup-media-type" /tmp/mondo-restore.cfg > /dev/null 2> /dev/null ; then
	LogIt "backup-media-type is specified in config file - great."
	LogIt "Calling post-init $mountlist"
	post-init
else
	LogIt "backup-media-type is not specified in config file."
	LogIt "I think this media has no archives on it."
fi
if grep "RESTORE" /proc/cmdline > /dev/null 2> /dev/null ; then
	echo "Rebooting in 10 seconds automatically as per reboot order"
	echo -en "Press ^C to interrupt if you have to ..."
	for i in 1 2 3 4 5 6 7 8 9 10 ; do
		sleep 1
		echo -en "."
	done
	echo "Boom."
	sleep 1
else
	echo -en "Type 'exit' to reboot the PC\n"
	umount /mnt/cdrom 2> /dev/null
	mount / -o rw,remount > /dev/null 2> /dev/null
	LogIt "Launching Shell"
	sh
fi
CaughtSoftReset
# reboot
