#!/bin/bash
# This is a cut down version of 'boot_kernel', just to reboot a SUT.

. run_profile.txt
. $base_dir/plans/commands/common.fns

kernel_info
SUT=`hostname -s`

# set autoreboot on kernel panic
echo 60 > /proc/sys/kernel/panic

# Hack to workaround bug in NFS causing portmap issues if there
# is a mounted filesystem and a cronjob writing to it
echo Powercycling machine

umount -la -t nfs
umount -la -t nfs4

echo /sbin/shutdown now -r
/sbin/shutdown now -r
err=$?

if [ $err -ne 0 ]; then
    echo "Error:  Failed to shutdown."
    echo "EXIT STATUS(shutdown):  $err"
    return $err
fi

## Wait for a while as we reboot
echo sleep 30
sleep 30
#
## If we haven't rebooted by now, something must be wrong.
echo "Warning:  Haven't rebooted after 30 sec."

sync
sleep 30

if [ -f $all_suts_dir/$SUT/bin/power ]; then
    echo "Initiating a remote power cycle event"
    echo "$all_suts_dir/$SUT/bin/power cycle"
    $all_suts_dir/$SUT/bin/power cycle
    err=$?
    if [ $? -ne 0 ]; then
        echo "Error:  Remote power of $SUT failed"
        echo "EXIT STATUS(power):  $err"
        return $err
    fi
fi

# Wait for a while as we reboot
echo sleep 30
sleep 30

# If we've hit this point, something is extremely broken
echo "Error:  Cannot restart $SUT.  Terminating."
echo "EXIT STATUS:  -1"
exit -1
