#!/bin/ash
#
# /linuxrc:  init script to load initrd kernel modules
#
# Copyright 2004  Slackware Linux, Inc., Concord, CA, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


# With a generic kernel, you need to load the modules needed to mount the
# root partition.  This might mean a SCSI, RAID, or other drive controller
# module, as well as the module to support the root filesystem.  Once the
# root partition is mounted all the other modules will be available so you
# don't need to load them here.
#
# Config files used by this script:
#
# /rootdev   Contains the name of the root device, such as: /dev/hda1 
#
# /rootfs    Contains the root filesystem type, such as: xfs
#
# Optional:
# /modlist   A script that uses insmod to load the desired modules.
#            If this file is not present, all the modules in
#            /lib/modules/`uname -r`/ will be loaded in the usual
#            sorted order.  If you need to load the modules in a
#            certain order, or if the modules need extra options,
#            then use a load_kernel_modules script.
#
#            There's an example in here.  To actually use it, you'll
#            need to make it executable:  
#
#                chmod 755 load_kernel_modules


# Mount /proc:
mount -n proc /proc -t proc

# Load kernel modules:
if [ ! -d /lib/modules/`uname -r` ]; then
  echo "No kernel modules found for Linux `uname -r`."
elif [ -x ./load_kernel_modules ]; then # use load_kernel_modules script:
  echo "/boot/initrd.gz:  Loading kernel modules from initrd image:"
  . ./load_kernel_modules
else # load modules (if any) in order:
  if ls /lib/modules/`uname -r`/*.*o 1> /dev/null 2> /dev/null ; then
    echo "/boot/initrd.gz:  Loading kernel modules from initrd image:"
    for module in /lib/modules/`uname -r`/*.*o ; do
      insmod $module
    done
    unset module
  fi
fi

# Initialize LVM:
if [ -x /sbin/vgscan ]; then
  /sbin/vgscan --mknodes
  sleep 10
  /sbin/vgchange -ay
fi

# If /rootdev isn't set, we'll have to trust exiting to work here.
# It's harder to clean up the initrd without a pivot_root,
# so it's a good idea to set rootdev (and rootfs) properly.
if [ -r /rootdev ]; then
  if [ "`cat /rootdev`" = "" ]; then
    exit 0
  fi
else
  exit 0
fi

# Switch to real root partition:
ROOTDEV=`cat /rootdev`
ROOTFS=`cat /rootfs`
echo 0x0100 > /proc/sys/kernel/real-root-dev
mount -o ro -t $ROOTFS $ROOTDEV /mnt
ERR=$?
if [ ! "$ERR" = "0" ]; then
  echo "ERROR:  mount returned error code $ERR.  Trouble ahead."
fi
unset ERR
umount /proc
# OK, in case there's no initrd directory:
if [ ! -d /mnt/initrd ]; then
  mount -o remount -o rw -t $ROOTFS $ROOTDEV /mnt
  mkdir -p /mnt/initrd
  mount -o remount -o ro -t $ROOTFS $ROOTDEV /mnt
fi
cd /mnt
# bye now
echo "/boot/initrd.gz:  exiting"
pivot_root . initrd
exit 0

