#!/bin/bash
# /usr/share/bonding/enable-kdump
# Install latest kernel and enable kdump for debugging kernel crashes
# © 2017, Multapplied Networks, Inc.
set -e

TMPFILE=$(mktemp /tmp/abc-script.XXXXXX)
trap 'rm -f "$TMPFILE"' INT TERM HUP EXIT

KDUMP_CFG="/etc/default/kdump-tools"
GRUB_CFG="/etc/default/grub"
KDUMP_ENABLED="/etc/default/bonding-kdump-enabled"

test -f $KDUMP_ENABLED && . $KDUMP_ENABLED 
if [ "$ENABLED" = "1" ]; then
    echo "kdump is already enabled"
    exit 0
fi

echo "This will install kdump and a debug kernel and requires a reboot."
read -p "Would you like to proceed? (y/N) " confirm
case ${confirm} in
    [Yy]* ) ;;
    [Nn]* ) exit ;;
    *     ) exit ;;
esac

# Install kdump requirements
echo "Installing packages"
export DEBIAN_FRONTEND=noninteractive
apt-get update -q > /dev/null
apt-get install -q -y kdump-tools kexec-tools makedumpfile crash file > /dev/null

# Ask about installing debug kernel
package_size=$(echo "$(apt-cache show $(uname -r)-dbg | grep '^Size: ' | head -n1 | cut -d ' ' -f2) / 1000000" | bc)
echo -e "\nWould you like to install the kernel debugging symbols for the current kernel ($(uname -r))? The package is ${package_size} MB and will take up approximately 5-10 times that much space when installed." 
read -p "Install debug symbols? (y/N) " confirm
case ${confirm} in
    [Yy]* ) install_debug=true ;;
    [Nn]* ) install_debug=false ;;
    *     ) install_debug=false ;;
esac
if $install_debug; then
    apt-get install -q -y `uname -r`-dbg > /dev/null
    echo "When upgrading the kernel you will need to keep the dbg package up to date as well."
else
    echo "You can download a local copy of the debug kernel package for later installation by running 'apt-get download $(uname -r)-dbg'"
fi

# KDump is disabled by default on Debian
sed -i 's/USE_KDUMP=0/USE_KDUMP=1/g' $KDUMP_CFG

# Set up memory for crash kernel
if grep -q GRUB_CMDLINE_LINUX_DEFAULT $GRUB_CFG; then
    # append 'crashkernel=128M' to the existing default line
    sed 's#GRUB_CMDLINE_LINUX_DEFAULT="\(.*\)"#GRUB_CMDLINE_LINUX_DEFAULT="\1 crashkernel=128M"#g' $GRUB_CFG > $TMPFILE
else
    # add a sensible default
    cat $GRUB_CFG > $TMPFILE
    echo 'GRUB_CMDLINE_LINUX_DEFAULT="quiet crashkernel=128M"' >> $TMPFILE
fi 

echo -e "\n/etc/default grub will be updated as follows: "
echo "========================================="
diff --old-group-format=$'\e[0;31m%<\e[0m' \
     --new-group-format=$'\e[0;32m%>\e[0m' \
     $GRUB_CFG $TMPFILE ||:
echo "========================================="

read -p "Is this correct? (y/N) " confirm
case ${confirm} in
    [Yy]* ) update_grub=true ;;
    [Nn]* ) update_grub=false ;;
    *     ) update_grub=false ;;
esac
if $update_grub; then
    cp $TMPFILE $GRUB_CFG
    update-grub
else
    echo "Not updating grub, kdump is not enabled."
    exit 1
fi

# Enable cron job to cleanup old kdumps
echo "ENABLED=1" > $KDUMP_ENABLED

echo -e '\nFinished setting up kdump. Please reboot and follow the documentation for testing instructions.' 
