#!/bin/bash
#
# Running this script will load the latest vmlinuz-generic and
# initrd-generic.img using kexec. Then rebooting will boot from
# the currently running kernel into the kexec-loaded kernel
# bypassing the BIOS/UEFI boot and the subsequent hardware
# initialization, often shaving significant time off of the
# reboot process.
#
# You may load other kernels or initrds by using the kexec
# command directly in a similar fashion to what is done below.
# This is just a shortcut for people using the stock kernel and
# an initrd built for it. It's beyond the scope of this script
# to handle any kernel/initrd.

echo "Loading kernel and initrd with kexec:"
echo 'kexec -l /boot/vmlinuz-generic --append="$(cat /proc/cmdline)" --initrd=/boot/initrd-generic.img'
kexec -l /boot/vmlinuz-generic --append="$(cat /proc/cmdline)" --initrd=/boot/initrd-generic.img
ERRCODE=$?
if [ ! "$ERRCODE" = "0" ]; then
  echo "ERROR: kexec returned error code $ERRCODE."
  echo "kernel/initrd were not loaded."
  exit 1
else
  echo "Kernel and initrd loaded and ready. Use 'reboot' to start them."
fi
