#!/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 "umount all nfs mounts:"
umount -a -t nfs
umount -a -t nfs4

echo Powercycling machine
echo /sbin/reboot -i -f
/sbin/reboot -i -f
err=$?

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

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

sync
echo "try sysrq commands to reboot:"
echo h > /proc/sysrq-trigger
echo s > /proc/sysrq-trigger
echo u > /proc/sysrq-trigger
echo b > /proc/sysrq-trigger

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
