#!/bin/sh
#
#
# Sample script to customize options for PE
# For AIX:
#    TBD
# For Linux:
#    - Create a smaller PNSD log file in /tmp
#    - Configure poe.limits file
#    - Generate and store checkpoint key
#    - BSR support configuration

OS=`uname`

# On AIX, need to correctly configure LAPI kernel extensions on a
# diskless node boot
# (added for PE 1.2 and newer releases)
if [ "$OS" == "AIX" ]; then
  if [ "$NODESETSTATE" = "netboot" -o \
       "$NODESETSTATE" = "statelite" -o \
       "$NODESETSTATE" = "diskless" -o \
       "$NODESETSTATE" = "dataless" ]; then
       # Unconfigure any existing LAPI kernel extension
       if [ -x /etc/methods/ucfgzcmem ] ; then
           /etc/methods/ucfgzcmem
       fi
       # Configure the LAPI kernel extension at the latest level
       if [ -x /etc/methods/cfgzcmem ] ; then
           /etc/methods/cfgzcmem
       fi
  fi
fi

# Configure the PNSD.cfg to use a smaller log so it doesn't use so much memory
# for a stateless image.  Normally, pnsd creates this file if it does not
# exist, but it will not fill it in if it does exist.
if [ ! -f $installroot/etc/PNSD.cfg ]; then
    echo "log_file = /tmp/serverlog" > $installroot/etc/PNSD.cfg
    echo "log_file_size = 2097152" >> $installroot/etc/PNSD.cfg
    echo "socket_file = /tmp/PNSD" >> $installroot/etc/PNSD.cfg
else
  if [ "$OS" == "AIX" ]; then
    /usr/bin/sed -e 's/log_file_size = .*/log_file_size = 2097152/g'  $installroot/etc/PNSD.cfg > $installroot/etc/PNSD.cfg.new
    mv  $installroot/etc/PNSD.cfg.new  $installroot/etc/PNSD.cfg
  else
    /bin/sed -i 's/log_file_size = .*/log_file_size = 2097152/g'  $installroot/etc/PNSD.cfg
  fi
  if [ $NODESETSTATE == "install" ] || [ $NODESETSTATE == "boot" ]; then
    stopsrc -s pnsd
    startsrc -s pnsd
  fi
fi

# Configure the poe.limits file
if [ ! -f $installroot/etc/poe.limits ]; then
    echo "MP_POE_LAUNCH=all" > $installroot/etc/poe.limits
else
  if [ "$OS" == "AIX" ]; then
    /usr/bin/sed -e 's/MP_POE_LAUNCH=.*/MP_POE_LAUNCH=all/g'  $installroot/etc/poe.limits > $installroot/etc/poe.limits.new
   mv $installroot/etc/poe.limits.new $installroot/etc/poe.limits
  else
    /bin/sed -i 's/MP_POE_LAUNCH=.*/MP_POE_LAUNCH=all/g'  $installroot/etc/poe.limits
  fi
fi


# POE requires rsh to be running
if [ "$OS" == "AIX" ]; then
  /usr/bin/sed -e '/\/rshd/s/^#//g' $installroot/etc/inetd.conf > $installroot/etc/inetd.conf.new
  mv /etc/inetd.conf.new /etc/inetd.conf
  refresh -s inetd
else
  /bin/sed -i 's/disable.*/disable = no/g'  $installroot/etc/xinetd.d/rsh
  if [ $NODESETSTATE == "install" ] || [ $NODESETSTATE == "boot" ]; then
     service xinetd restart
  fi
fi


# The Parallel Debugger on AIX requires a /proc filesystem
if [ "$OS" == "AIX" ]; then
  if ! grep "/proc:" /etc/filesystems > /dev/null 2>&1 ; then
     echo "/proc:                     " >> /etc/filesystems
     echo "        dev       = /proc  " >> /etc/filesystems
     echo "        vol       = /proc  " >> /etc/filesystems
     echo "        mount     = true   " >> /etc/filesystems
     echo "        check     = false  " >> /etc/filesystems
     echo "        free      = false  " >> /etc/filesystems
     echo "        vfs       = procfs " >> /etc/filesystems
  fi
  mount /proc
fi

# Generate mpi modules
if [ "$OS" != "AIX" ]; then
    gen_xlf=`grep PE_LATEST_LEVEL $installroot/etc/ppe.cfg | cut -f2 -d ' ' `/mpich2/sbin/xlf_gen_mpimod
    if [ -x $installroot/$gen_xlf ] ; then
          if [ $NODESETSTATE == "install" ] || [ $NODESETSTATE == "boot" ]; then
              $gen_xlf
          else
              chroot $installroot $gen_xlf
          fi
    fi
fi

#Generate checkpoint key and store it into the image, ckpt script could ust it after boot.
if [ "$OS" != "AIX" ]; then
    #KEY=$(date | sha1sum | awk '{ print $1 }')
    KEY=$(dd if=/dev/random count=1 2>/dev/null | sha1sum | awk '{print $1}')
    echo $KEY > $installroot/root/.ckpt.key
    chmod 400  $installroot/root/.ckpt.key
fi

# Ensure that the /etc/security/privcmds file has a complte set of entries.
if [ "$OS" == "AIX" ]; then
    /usr/sbin/trustchk -ry ALL >/dev/null 2>&1
fi

# BSR configuration, uncomment the following lines to enable BSR configuration on Power Linux cluter.
#if [ "$OS" != "AIX" ]; then
#    if [ $NODESETSTATE == "install" ] || [ $NODESETSTATE == "boot" ]; then
#        groupadd bsr
#        mkdir -p /var/lib/bsr
#        chown root:bsr /var/lib/bsr
#        chmod g+sw /var/lib/bsr
#    else
#        chroot $installroot groupadd bsr
#        chroot $installroot mkdir -p /var/lib/bsr
#        chroot $installroot chown root:bsr /var/lib/bsr
#        chroot $installroot chmod g+sw /var/lib/bsr
#    fi
#fi


# pelinks script support, uncomment the following lines and change to the correct pe version that you intend to use.
#PE_VERSION=1202
#if [ "$OS" != "AIX" ]; then
#    if [ $NODESETSTATE == "install" ] || [ $NODESETSTATE == "boot" ]; then
#        MP_CONFIG=$PE_VERSION /opt/ibmhpc/pe$PE_VERSION/ppe.poe/bin/pelinks
#    else
#        export MP_CONFIG=$PE_VERSION;chroot $installroot /opt/ibmhpc/pe$PE_VERSION/ppe.poe/bin/pelinks
#    fi
#fi
