#!/bin/sh
#
# Sample /linuxrc script for an init-ramdisk to be used for activating EVMS
# volumes. See the INSTALL.initrd file in the EVMS package for instructions
# on creating an init-ramdisk image to use with this script.

PATH="/bin:/sbin:/usr/bin:/usr/sbin"
LOG="/linuxrc:"
ERR="/linuxrc: ERROR:"

# Mount /proc.
echo "$LOG Mounting /proc"
mount -t proc none /proc


# Mount /sys if a 2.6 kernel is running.
grep -q sysfs /proc/filesystems
if [ $? -eq 0 ]; then
	echo "$LOG Mounting /sys"
	mount -t sysfs none /sys
fi


# Mount devfs
# You should uncomment these two lines if you are running devfs but do not
# have the kernel automatically mount it on /dev.
# mount -t devfs none /dev
# mounted_devfs=1


# Run the devfs deamon, if devfs is present.
if [ -e /dev/.devfsd ]; then
	echo "$LOG Running devfsd"
	devfsd /dev
fi


# Load modules
#
# By default, the sample init-ramdisk does not contain any kernel modules.
# It assumes that the kernel contains static support necessary to run all
# the tools on the init-ramdisk.
#
# If you compiled kernel features as modules that you will need during the
# init-ramdisk, you need to copy those modules to the init-ramdisk in the
# /lib/modules/ directory. You also need to add appropriate commands below
# to load those modules. The modules should be loaded before running EVMS.
#
# Some examples are provided here. If you didn't install any kernel modules,
# simply leave these commented-out.
#
# NOTE: The version of insmod used by Busybox is not yet compatible with the
#       new 2.6 kernel module format. If you are running a 2.6 kernel and
#       want to load modules from this initrd, you will need to copy your
#       systems version of "insmod" to this initrd.
#
# Device-Mapper modules
# insmod dm-mod
# insmod dm-mirror
# insmod dm-bbr
# insmod dm-sparse
#
# MD/Software-RAID modules
# insmod md
# insmod linear
# insmod raid0
# insmod raid1
# insmod xor
# insmod raid5
# insmod multipath


# Search the kernel command line for parameters needed to mount the root
# filesystem. At a minimum, "root=/dev/evms/Root_Volume_Name" needs to be
# present (where Root_Volume_Name is replaced by your actual root volume's
# name). In addition, rootfstype= and rootflags= may be set in order to
# specify the type of the root filesystem and set any necessary mount options.
# See the section about "Mounting Your Root Filesystem Through EVMS" in the
# INSTALL file in the EVMS source package.

for name in `cat /proc/cmdline`; do
	# Look for "root="
	echo $name | grep -q '^root='
	if [ $? -eq 0 ]; then
		vol=`expr "$name" : '.*=\(.*\)'`
		echo "$LOG Found root = $vol"
		continue;
	fi

	# Look for "rootflags="
	echo $name | grep -q '^rootflags='
	if [ $? -eq 0 ]; then
		rootflags=`expr "$name" : '.*=\(.*\)'`
		echo "$LOG Found rootflags = $rootflags"
		continue
	fi

	# Look for "rootfstype="
	echo $name | grep -q '^rootfstype='
	if [ $? -eq 0 ]; then
		rootfstype=`expr "$name" : '.*=\(.*\)'`
		echo "$LOG Found rootfstype = $rootfstype"
		continue
	fi

	# Look for "suspend2=swap:"
	echo $name | grep -q '^suspend2=swap:'
	if [ $? -eq 0 ]; then
		suspend2_dev=`expr "$name" : '.*:\(.*\)'`
		echo "$LOG Found suspend2 = $suspend2_dev"
		echo $suspend2_dev | grep -q '^/dev/evms_'
		if [ $? -eq 0 ]; then
			suspend2_vol=`expr "$suspend2_dev" : '\/dev\/evms_\(.*\)'`
			suspend2_vol="/dev/evms/${suspend2_vol}"
		fi
		continue;
	fi

	# Look for "evms_initrd="
	echo $name | grep -q '^evms_initrd='
	if [ $? -eq 0 ]; then
		evms_initrd=`expr "$name" : '.*=\(.*\)'`
		echo "$LOG Found evms_initrd = $evms_initrd"
		continue;
	fi
done


if [ -z "$rootfstype" ]; then
	rootfstype="auto"
fi

# Check if the user wants to start a shell before running EVMS.
echo $evms_initrd | grep -q 'pre_shell'
if [ $? -eq 0 ]; then
	echo "$LOG Starting a shell before running evms_activate. When you exit"
	echo "$LOG the shell, I will attempt to continue the boot process."
	sh
fi

# Activate EVMS volumes.
echo "$LOG Running evms_activate"
evms_activate

# Setup Software-Suspend-2.
if [ -n "$suspend2_dev" ]; then
	if [ -n "$suspend2_vol" ]; then
		cp -af $suspend2_vol $suspend2_dev
	fi

	echo "$LOG Remounting initrd read-only."
	mount -n -o remount,ro /

	echo "$LOG Software-suspend resume..."
	echo > /proc/software_suspend/do_resume

	echo "$LOG Remounting initrd read-write."
	mount -n -o remount,rw /
fi

# Mount the root volume, or tell the user to mount it manually.
if [ -n "$vol" ]; then
	if [ -n "$rootflags" ]; then
		echo "$LOG Mounting $vol on /sysroot with options: $rootflags"
		mount -t $rootfstype -o ro,$rootflags $vol /sysroot
	else
		echo "$LOG Mounting $vol on /sysroot with no extra options."
		mount -t $rootfstype -o ro $vol /sysroot
	fi
else
	echo "$ERR The root volume name was not found in the kernel"
	echo "$ERR command-line. I don't know which volume to mount"
	echo "$ERR as root. Starting a shell, where you can mount the"
	echo "$ERR root volume manually on the /sysroot directory. You"
	echo "$ERR can do this with the command:"
	echo "$ERR     mount -o ro /dev/evms/Your_Root_Volume /sysroot"
	echo "$ERR When you exit the shell, I will attempt to continue"
	echo "$ERR the boot process."
	sh
fi


# Check that the root volume was actually mounted.
grep -q "/sysroot" /proc/mounts
if [ $? -ne 0 ]; then
	echo "$ERR The root volume has not been properly mounted. Starting"
	echo "$ERR a shell. Please mount the root volume manually on the"
	echo "$ERR /sysroot directory. You can do this with the command:"
	echo "$ERR     mount -o ro /dev/evms/Your_Root_Volume /sysroot"
	echo "$ERR When you exit the shell, I will attempt to continue"
	echo "$ERR the boot process."
	sh
fi


# Check that there is an /initrd directory on the root-fs.
if [ ! -d /sysroot/initrd ]; then
	echo "$ERR The root volume must have an '/initrd' subdirectory"
	echo "$ERR for pivot_root to work, but this directory does not"
	echo "$ERR exist. Starting a shell, where you can attempt to"
	echo "$ERR create this directory (in /sysroot). The $vol root"
	echo "$ERR volume is currently mounted read-only, so you will"
	echo "$ERR need to remount read-write, create the directory, and"
	echo "$ERR then remount read-only. You can use the commands:"
	echo "$ERR     umount /sysroot"
	echo "$ERR     mount -o rw /dev/evms/Your_Root_Volume /sysroot"
	echo "$ERR     mkdir /sysroot/initrd"
	echo "$ERR     umount /sysroot"
	echo "$ERR     mount -o ro /dev/evms/Your_Root_Volume /sysroot"
	echo "$ERR When you exit the shell, I will attempt to continue"
	echo "$ERR the boot process."
	sh
fi


# Set the real-root-dev to /dev/ram0 (1:0).
echo 0x0100 > /proc/sys/kernel/real-root-dev


# Stop the devfs daemon
if [ -e /dev/.devfsd ]; then
	echo "$LOG Stopping devfsd"
	killall devfsd
fi


# Unmount devfs (if we manually mounted above).
if [ -n "$mounted_devfs" ]; then
	umount /dev
fi


# Unmount /sys
grep -q sysfs /proc/mounts
if [ $? -eq 0 ]; then
	echo "$LOG Unmounting /sys"
	umount /sys
fi


# Unmount /proc
echo "$LOG Unmounting /proc"
umount /proc


# Pivot-root to /sysroot
echo "$LOG Pivoting root to /sysroot"
cd /sysroot
if ! pivot_root . initrd; then
	echo "$ERR Failed to pivot root to /sysroot. Starting a shell"
	echo "$ERR where you can attempt to fix the problem manually."
	echo "$ERR When you exit the shell, I will attempt to continue"
	echo "$ERR the boot process."
	sh
fi

# Check if the user wants to start a shell before exiting the initrd.
if echo $evms_initrd | grep -q 'post_shell'; then
	echo "$LOG Starting a shell before exiting the initrd. When you exit"
	echo "$LOG the shell, I will attempt to continue the boot process."
	sh
fi

echo "$LOG Done"

