# /usr/lib/mindi/TryToBeCleverAboutInitrd
#
# Debian Plugin script for mindi to decide whether to create the initrd image
# for boot CDs as ext2 or cpio (or to die if neither is possible).
#
# Changelog:
# 21Jul04AL: - intial version
# 22Jul04AL: - even more comments
#            - deal with isolinux and sylinux configuration files
#            - break log messgae into multiple lines
# 28Jul04AL: - added missing syslinux-H.cfg config file
#            - replaced copying syslinux and isolinux config files with linking
#              to make thins clearer and to save a few bytes
#            - deal with FAILSAFE kernel here to keep mindi changes minimal
#            - create link to init explictly (init or linuxrc depending on
#              initrd image type); required for new symlinks.tgz
# 29Jul04AL: - leave mountpoint before unmounting in UseExt2 :-(
#


###############################################################################
# Subroutines
###############################################################################

# Check whether we are running a 2.6 kernel. If so use cpio. Otherwise die (we
# only get called if this the last resort).
UseCpio () {

  # Get major and minor version of current kernel.
  majver=`uname -r | cut -f1 -d"."`
  minver=`uname -r | cut -f2 -d"."`

  # If we are running a 2.6 kernel create cpio initrd image, else die.
  if [ $majver == 2 -a $minver == 6 ] ; then
    # Say what will be used.
    LogIt "You are running a 2.6 kernel, either completely without ext2"
    LogIt "or with ext2 as a module. Good. Using a cpio initrd image."
    # Go into filesystem.
    cd $mountpoint
    # Kernel expects init in cpio filesystem.
    ln -s sbin/init init
    # Create cpio image file and unmount loop filesystem.
    find . -print | cpio -o -H newc > $TMP_ROOT/rootfs.cpio
    cd $old_pwd
    cat $TMP_ROOT/rootfs.cpio | gzip -v9 > $rdz_fname 2> /dev/null
    umount $mountpoint || Die "Cannot unmount $tempfile"
    # Use the cpio isolinux or syslinux config files.
    cd $MINDI_HOME
    ln -sf isolinux.cfg.cpio   isolinux.cfg
    ln -sf isolinux-H.cfg.cpio isolinux-H.cfg
    ln -sf syslinux.cfg.cpio   syslinux.cfg
    ln -sf syslinux-H.cfg.cpio syslinux-H.cfg
    cd $old_pwd
  else
    Die "You are running a `uname -r` kernel with no ext2 support compiled in. The boot media will not work, hence I'm terminating. Please use a 2.6 kernel or compile ext2 into your kernel."
  fi

}


# Follow the standard approach and just do the necessary steps for using the
# already created ext2 filesystem file
UseExt2 () {

  # Say what will be used.
  LogIt "You have ext2 support compiled into your kernel. Good."
  LogIt "Using a ext2 initrd image."

  # Kernel expects linuxrc in ext2 filesystem.
  ( cd $mountpoint && ln -s sbin/init linuxrc )

  # Unmount loop filesystem and create image file using the standard approach.
  umount $mountpoint || Die "Cannot unmount $tempfile"
  dd if=$tempfile bs=1k 2> /dev/null | gzip -v9 > $rdz_fname 2> /dev/null

  # Use the ext2 isolinux or syslinux config files.
  cd $MINDI_HOME
  ln -sf isolinux.cfg.ext2   isolinux.cfg
  ln -sf isolinux-H.cfg.ext2 isolinux-H.cfg
  ln -sf syslinux.cfg.ext2   syslinux.cfg
  ln -sf syslinux-H.cfg.ext2 syslinux.cfg
  cd $old_pwd

}


###############################################################################
#Main Part
###############################################################################


# If we are using the FAILSAFE kernel, only ensure the correct config files for
# isolinux and syslinux are used and continue in mindi. Otherwise try to be
# clever first (and then continue in mindi).
if [ "${kernelname}" == "FAILSAFE" ] ; then
  UseExt2
else
  # What filesystem we are after.
  fileSystem=ext2
  # Check for loaded module.
  lsmod | grep "^$fileSystem" >/dev/null 2>/dev/null
  moduleLoaded=$?
  # If filesystem module is loaded, it's not compiled into the kernel, so we
  # try to go down the cpio path. Otherwise we check whether filesystem support
  # is actually in the kernel. If it is, we take the standard ext2 approach. If
  # not we go down the cpio path again.
  if [ $moduleLoaded == 0 ]; then
    UseCpio
  else
    # Check whether filesystem is compiled into the kernel.
    grep $fileSystem /proc/filesystems >/dev/null 2>/dev/null
    compiledIn=$?
    # If filesystem is compiled into kernel use ext2 otherwise try cpio.
    if [ $compiledIn == 0 ] ; then
      UseExt2
    else
      UseCpio
    fi 
  fi
fi
