#!/bin/sh
#
#
# Sample script to customize options for Mellonax OFED IB support
# For AIX:
#    TBD
# For Linux:
#    - For full-disk installs:
#        - Copy rpms to node
#        - Install IB rpms
#    - For diskless images:
#        - Copy the packages to the images.
#        - Install IB rpms
#
#
#usage:
#      Copy the xCAT mlnxofed_ib_install script file to postscripts directory: 
#      cp /opt/xcat/share/xcat/ib/scripts/Mellanox/mlnxofed_ib_install /install/postscripts/mlnxofed_ib_install
#   1. install the ofed driver for diskfull node
#      1.1 set the postscript as one postbootscripts for the node
#        chdef xcat01 -p postbootscripts=mlnxofed_ib_install,configiba
#      1.2  copy the pkglist to the custom directory: 
#         cp /opt/xcat/share/xcat/install/<ostype>/compute.<osver>.<arch>.pkglist /install/custom/install/<ostype>/compute.<osver>.<arch>.pkglist
#         Edit your /install/custom/install/<ostype>/compute.<osver>.<arch>.pkglist and add: 
#             #INCLUDE:/opt/xcat/share/xcat/ib/netboot/<ostype>/ib.<osver>.<arch>.pkglist#
#      1.3 Make sure the related osimage use the customized pkglist. 
#         lsdef -t osimage -o  <osver>-<arch>-install-compute
#        if not, change it:
#          chdef  -t osimage -o <osver>-<arch>-install-compute pkglist=/install/custom/install/<ostype>/compute.<osver>.<arch>.pkglist
#      1.4 do the diskfull installation
#   2. install the ofed driver for diskless images:
#      2.1 copy the pkglist to the custom directory: 
#            cp /opt/xcat/share/xcat/netboot/<ostype>/compute.<osver>.<arch>.pkglist /install/custom/netboot/<ostype>/compute.<osver>.<arch>.pkglist
#         Edit your /install/custom/netboot/<ostype>/<profile>.pkglist and add: 
#          #INCLUDE:/opt/xcat/share/xcat/ib/netboot/<ostype>/ib.<osver>.<arch>.pkglist#
#       2.2 Add to postinstall scripts
#       Edit your /install/custom/netboot/<ostype>/<profile>.postinstall and add: 
#          installroot=$1 ofeddir=/install/post/otherpkgs/<osver>/<arch>/ofed/ NODESETSTATE=genimage /install/postscripts/mlnxofed_ib_install
#       2.3 Make sure the related osimage use the customized pkglist and customized compute.postinsall 
#          lsdef -t osimage -o  <osver>-<arch>-netboot-compute
#       if not, change it:
#           chdef  -t osimage -o <osver>-<arch>-netboot-compute pkglist=/install/custom/netboot/<ostype>/compute.<osver>.<arch>.pkglist postinstall=/install/custom/netboot/<ostype>/<profile>.postinstall
#       2.4 run genimage
#       genimage <osver>-<arch>-netboot-compute
#    You can get more info from
#     https://sourceforge.net/apps/mediawiki/xcat/index.php?title=Managing_the_Mellanox_Infiniband_Network#Configure_IB_interfaces_during_Node_installation
#

OS=`uname`
#installroot='/install/netboot/sles11.1/x86_64/compute/rootimg'
INSTALL_DIR='/install'

#echo "------------------------------------------"
#echo "OFED_DIR=$ofeddir"
#echo "OSVER=$OSVER"
#echo "arch=$ARCH"
#echo  "NODESETSTATE=$NODESETSTATE"
#echo "mlnxofed_options=$mlnxofed_options"
#echo  "installroot=$installroot"
#echo "------------------------------------------"

if [ "$(uname -s|tr 'A-Z' 'a-z')" = "linux" ];then
    str_dir_name=`dirname $0`
    . $str_dir_name/xcatlib.sh
fi

#use arg for the OFED_DIR if defined,
ARGNUM=$#
if [ $ARGNUM -gt 1 ]; then
    if [ $1 = "-s" ]; then
        OFED_DIR=$2
    fi
fi

#if arg is not defined, check ofeddir environment variable,
if [ -z "$OFED_DIR" ]; then
   OFED_DIR=$ofeddir
   #if ofeddir is not defined, use default.
   if [ -z "$OFED_DIR" ]; then
       # try to default
       OFED_DIR=$INSTALL_DIR/post/otherpkgs/$OSVER/$ARCH/ofed
   fi
fi

# $mlnxofed_options is an environment variable passed to the mlnxofed_ib_install script. By default mlnxofed_option is '--without-32bit --force'
if [ -z "$mlnxofed_options" ]; then
   mlnxofed_options="--without-32bit --force"
fi 

if [[ $NODESETSTATE != "genimage" ]]; then   
   # running as a postscript in a full-disk install or AIX diskless install
   installroot=""
fi

if [ $OS != "AIX" ]; then
    if [[ $NODESETSTATE == "install" ]] || [[ $NODESETSTATE == "boot" ]]; then
    #  Being run from a stateful install postscript
    #  Copy rpms directly from the xCAT management node and install
        mkdir -p /tmp/ofed
        rm -f -R /tmp/ofed/*
        cd /tmp/ofed
        #download_dir=`echo $OFED_DIR | cut -d '/' -f3-`
        #wget -l inf -N -r --waitretry=10 --random-wait --retry-connrefused -t 10 -T 60 -nH --cut-dirs=5 ftp://$SITEMASTER/$download_dir/ 2> /tmp/wget.log
        download_dir=$OFED_DIR
        num=`echo $download_dir | awk -F"/" '{if($NF==""){print NF-2}else{print NF-1}}'`
        wget -l inf -N -r --waitretry=10 --random-wait --retry-connrefused -t 10 -T 60 -nH --cut-dirs=$num --reject "index.html*"  --no-parent http://$MASTER$download_dir/ 2> /tmp/wget.log
        wget -l inf -N -r --waitretry=10 --random-wait --retry-connrefused -t 10 -T 60 -nH --cut-dirs=$num --reject "index.html*"  --no-parent http://$MASTER$download_dir/.mlnx 2>> /tmp/wget.log
        wget -l inf -N -r --waitretry=10 --random-wait --retry-connrefused -t 10 -T 60 -nH --cut-dirs=$num --reject "index.html*"  --no-parent http://$MASTER$download_dir/.supported_kernels 2>> /tmp/wget.log


        if [ -f /etc/os-release ] && 
            cat /etc/os-release |grep NAME|grep Ubuntu>/dev/null
        then
            nodename=`hostname`
            echo "$nodename 's operating system is Ubuntu."
            echo "If you want to install Mellanox_OFED in $nodename, $nodename must have ability to access public network."
            echo -n "checking $nodename 's ability to access public network..........."
            if ping -c 3 91.189.88.140 > /dev/null;then 
               echo "[OK]"
            else
               echo "[Failed]"
               echo "please make your $nodename have ability to access public network"
               exit 1
            fi
                            
           cp /etc/apt/sources.list  /etc/apt/sources.list.bak
           sed -i "/deb http:\\/\\/91.189.88.140\\/ubuntu-ports\\/ trusty main/d"  /etc/apt/sources.list
           sed -i "/deb http:\\/\\/91.189.88.140\\/ubuntu-ports\\/ trusty-updates main/d"  /etc/apt/sources.list
           sed -i "/deb http:\\/\\/91.189.88.140\\/ubuntu-ports\\/ trusty universe/d" /etc/apt/sources.list
           sed -i "/deb http:\\/\\/91.189.88.140\\/ubuntu-ports\\/ trusty-updates universe/d" /etc/apt/sources.list
                    
           echo "deb http://91.189.88.140/ubuntu-ports/ trusty main
deb http://91.189.88.140/ubuntu-ports/ trusty-updates main
deb http://91.189.88.140/ubuntu-ports/ trusty universe
deb http://91.189.88.140/ubuntu-ports/ trusty-updates universe" >> /etc/apt/sources.list
                
           apt-get clean all
           sleep 1
           apt-get update
        fi
        
        #rpm -Uvh --force libibverbs-devel*.rpm 
        #perl -x mlnxofedinstall --without-32bit --force
        
        if [ -f /etc/os-release ] &&
            cat /etc/os-release|grep NAME|grep Ubuntu>/dev/null &&
            uname -m |grep ppc64 >/dev/null
        then
            echo "ARCH=powerpc perl -x mlnxofedinstall --without-fw-update $mlnxofed_options"
            ARCH=powerpc perl -x mlnxofedinstall --without-fw-update $mlnxofed_options
            sleep 1
            service openibd restart
        else
            echo  "perl -x mlnxofedinstall $mlnxofed_options"
            perl -x mlnxofedinstall $mlnxofed_options
        fi
        rm -Rf /tmp/ofed
        #force openibd load all modules in need, restart again
        restartservice openibd
    fi

    if [[ $NODESETSTATE == "genimage" ]]; then
    # Being called from <image>.postinstall script
    # Assume we are on the same machine
        if [[ $OS == sles* ]] || [[ $OS == suse* ]] || [[ -f /etc/SuSE-release ]]; then
            #mkdir $installroot/tmp/ofed_install
            #cp -r $OFED_DIR/ $installroot/tmp/ofed_install/
            mkdir -p $installroot/tmp/ofed_install/ofed
            ( cd "$OFED_DIR" && tar cf - . ) | ( cd "$installroot/tmp/ofed_install/ofed" && tar xf - )
            #mount -t devtmpfs  /dev $installroot/dev/
            #mount -t sysfs  /sys $installroot/sys
            #mount -t proc /proc $installroot/proc
            mount --bind  /dev $installroot/dev/
            mount --bind  /sys $installroot/sys
            mount --bind /proc $installroot/proc
            chroot $installroot rpm -e --noscripts --allmatches mlnx-ofa_kernel-kmp-default 2>/dev/null 
            chroot $installroot rpm -e --nodeps --allmatches libibverbs 2>/dev/null 
            #chroot $installroot perl -x /tmp/ofed_install/ofed/mlnxofedinstall --without-32bit --force
            chroot $installroot perl -x /tmp/ofed_install/ofed/mlnxofedinstall $mlnxofed_options
            rm -rf $installroot/tmp/ofed_install
            umount $installroot/dev/
            umount $installroot/sys
            umount $installroot/proc
        elif [ -f /etc/os-release ] &&        
             cat /etc/os-release |grep NAME|grep Ubuntu>/dev/null; then

            #mkdir $installroot/tmp/ofed_install
            #cp -r $OFED_DIR/ $installroot/tmp/ofed_install/
            mkdir -p $installroot/tmp/ofed_install/ofed
            ( cd "$OFED_DIR" && tar cf - . ) | ( cd "$installroot/tmp/ofed_install/ofed" && tar xf - )

            nodename=`hostname`
            echo "$nodename 's operating system is Ubuntu."
            echo "If you want to install Mellanox_OFED in $nodename, $nodename must have ability to access public network."
            echo -n "checking $nodename 's ability to access public network..........."
            if ping -c 3 91.189.88.140 > /dev/null;then 
                echo "[OK]"
            else
                echo "[Failed]"
                echo "please make your $nodename have ability to access public network"
                exit 1
            fi

            sourceslist="$installroot/etc/apt/sources.list"
            cp  $sourceslist $sourceslist.bak 

            sed -i "/deb http:\\/\\/91.189.88.140\\/ubuntu-ports\\/ trusty main/d"  $sourceslist 
            sed -i "/deb http:\\/\\/91.189.88.140\\/ubuntu-ports\\/ trusty-updates main/d" $sourceslist 
            sed -i "/deb http:\\/\\/91.189.88.140\\/ubuntu-ports\\/ trusty universe/d" $sourceslist 
            sed -i "/deb http:\\/\\/91.189.88.140\\/ubuntu-ports\\/ trusty-updates universe/d" $sourceslist 
                    
            echo "deb http://91.189.88.140/ubuntu-ports/ trusty main
deb http://91.189.88.140/ubuntu-ports/ trusty-updates main
deb http://91.189.88.140/ubuntu-ports/ trusty universe
deb http://91.189.88.140/ubuntu-ports/ trusty-updates universe" >> $sourceslist 
                
            chroot $installroot apt-get clean all
            sleep 1
            chroot $installroot apt-get update
            mount --bind  /dev $installroot/dev/
            mount --bind  /proc  $installroot/proc/ 
            mount --bind  /sys   $installroot/sys/

            mv "${installroot}/bin/uname" "${installroot}/bin/uname.nouse"
#            cat <<-EOF >"${installroot}/bin/uname"
#		#!/bin/sh 
#		[ "\$1" = "-r" ] && cd /lib/modules && for d in *; do :; done && echo \$d
#		[ "\$1" = "-m" ] && if [ -d "/proc/powerpc" ];then echo ppc64le;else echo x86_64;fi 
#		[ "\$1" = "-s" ] && echo Linux
#		exit 0
#		EOF

            cat <<-EOF >"${installroot}/bin/uname"
		#!/bin/sh 
		case "\$1" in
		"-m")
			ARCH="\$(dpkg --print-architecture || rpm -q kernel-\$("\$0" -r) --qf '%{arch}')"
			case "\$ARCH" in
			"amd64")		
				ARCH="x86_64"
				;;
			"ppc64el")
			        ARCH="ppc64le"		
				;;	
			esac
			echo "\$ARCH"	
			;;
		"-r")	
			cd /lib/modules && for d in * ; do : ; done && echo \$d	
			;;
		"-s"|"")
			echo "Linux"	
			;;
		esac
		exit 0
		EOF

#            head -n 999 "${installroot}/bin/uname"
            chmod 0755 "${installroot}/bin/uname"
             
            chroot $installroot sh -c 'apt-get install -y linux-headers-$(uname -r)'

            echo "perl -x /tmp/ofed_install/ofed/mlnxofedinstall  --without-fw-update $mlnxofed_options"
            chroot $installroot  perl -x /tmp/ofed_install/ofed/mlnxofedinstall  --without-fw-update $mlnxofed_options
            mv "${installroot}/bin/uname.nouse" "${installroot}/bin/uname"

            rm -rf $installroot/tmp/ofed_install 
            umount $installroot/dev/
            umount $installroot/proc/
            umount $installroot/sys/
    
        else
            #mkdir $installroot/tmp/ofed_install
            #cp -r $OFED_DIR/ $installroot/tmp/ofed_install/ 
            mkdir -p $installroot/tmp/ofed_install/ofed
            ( cd "$OFED_DIR" && tar cf - . ) | ( cd "$installroot/tmp/ofed_install/ofed" && tar xf - )
            chroot $installroot rpm -e --nodeps --allmatches libibverbs 2>/dev/null 
            #chroot $installroot perl -x /tmp/ofed_install/ofed/mlnxofedinstall --without-32bit --force
            chroot $installroot perl -x /tmp/ofed_install/ofed/mlnxofedinstall $mlnxofed_options
            rm -rf $installroot/tmp/ofed_install
        fi


    fi
fi

