#!/bin/bash

######## Settings ########
# Specify the kernel. (Modify the path if necessaary.)
KERNEL=~/sharp_zImage.bin

# Set env and do some sanity checks
CMDLINE=$(cat /proc/cmdline)
KEXEC_STATUS=$(dpkg --get-selections kexec-tools | awk '{ print $2 }')
[ "$UID" != "0" ] && echo "This script must be run as root!" && exit 1
[ ! "$KEXEC_STATUS" = "install" ] && echo "kexec not found! Please install the package 'kexec-tools'" && exit 1

# Function to blank the screen... cosmedic
screen_dump() {

# Edit CYCLES if you like.  Less cycles means the script will be faster
# but may leave garbage on the screen from the previous kernel
CYCLES=100
PASS=0
clear
while [[ $PASS -lt $CYCLES ]]
do
        echo "" >/dev/console ; echo "" >/dev/tty0
   echo "" >/dev/tty1 ; echo "" >/dev/tty2
   echo "" >/dev/tty3 ; echo "" >/dev/tty4
   echo "" >/dev/tty5 ; echo "" >/dev/tty6
        PASS=$(($PASS + 1))
done
clear
}

# Deactivate current system and go read-only
## Make sure no other kernel has been loaded
kexec -u
sync
echo -n "Killing all running processes ..."
[ -x /etc/init.d/sendsigs ] && /etc/init.d/sendsigs stop >/dev/null 2>&1 || FATAL_ERROR "/etc/init.d/umountroot"
echo "Done"
echo -n "Mounting root read-only ..."
[ -x /etc/init.d/umountfs ] && /etc/init.d/umountfs stop >/dev/null 2>&1 || FATAL_ERROR "/etc/init.d/umountroot"
[ -x /etc/init.d/umountroot ] && /etc/init.d/umountroot stop >/dev/null 2>&1 || FATAL_ERROR "/etc/init.d/umountroot"
echo "Done"


# Clear the screen once again and boot the new kernel
# Load the system
kexec -l $KERNEL
echo "Booting the system...."
screen_dump
kexec -e 





