#!/bin/sh
#
# Sample postscript that can be executed to help update the firmware level on
# Mellanox hardware adapters on the target nodes
#
# This script requires passing in an optional force (-f) flag and
# the path of the Mellanox OFED ISO
# located on the xCAT Management node under the /install directory.
#
# If OFED file is located here:
# /install/mlnxofed/MLNX_OFED_LINUX-4.1-4.0.7.1-rhel7.4alternate-ppc64le.iso
#
# Call the script as follows:
#    updatenode <noderange> -P "update_mlnx_adapter_firmware /install/mlnxofed/MLNX_OFED_LINUX-4.1-4.0.7.1-rhel7.4alternate-ppc64le.iso"
#         or
#    updatenode <noderange> -P "update_mlnx_adapter_firmware -f /install/mlnxofed/MLNX_OFED_LINUX-4.1-4.0.7.1-rhel7.4alternate-ppc64le.iso"
#

FORCE=""
while getopts "f" option
do
   case "${option}" in
       f) FORCE="--force-fw-update"
          shift;;
       *) echo "Usage: ${0} [-f] <path to MLNX OFED ISO file>"
          exit 1;;
   esac
done

MLNX_OFED_PATH=${1}

if [[ -z ${MLNX_OFED_PATH} ]]; then
   echo "Error: you must provide the path of the MLNX OFED ISO file"
   exit 1
fi

WORKING_DIR="/"
MLNX_ISO_FILE=`basename ${MLNX_OFED_PATH}`
TARGET_ISO_FILE="${WORKING_DIR}/${MLNX_ISO_FILE}"

echo "==> Mellanox OFED PATH: ${MLNX_OFED_PATH}"
echo "==> Mellanox ODEF ISO: ${MLNX_ISO_FILE}"

if [[ -e ${TARGET_ISO_FILE} ]]; then
   rm -f ${TARGET_ISO_FILE}
fi

echo "==> Retrieving file from http://${MASTER}/${MLNX_OFED_PATH}"
wget -q http://${MASTER}/${MLNX_OFED_PATH} -O ${TARGET_ISO_FILE}

ls -ltr ${TARGET_ISO_FILE}

MOUNT_DIR="${WORKING_DIR}/mlnx_tmp_iso_dir"
mkdir -p ${MOUNT_DIR}

mount -o ro,loop ${TARGET_ISO_FILE} ${MOUNT_DIR}
ls -ltr ${MOUNT_DIR}

${MOUNT_DIR}/mlnxofedinstall --fw-update-only ${FORCE}

#
# Clean up
#
sleep 1
umount ${MOUNT_DIR}
rmdir ${MOUNT_DIR}

rm -f ${TARGET_ISO_FILE}


echo "==> "
echo "==> "
echo "==> REBOOT THIS MACHINE FOR THE NEW FIRMWARE LEVEL TO TAKE EFFECT."
echo "==> "
echo "==> "

exit 0
