2022-04-07 08:48:52 +00:00
|
|
|
#!/bin/bash
|
|
|
|
# Automated AWS generic kernel setup for jibri.
|
2023-11-08 06:34:51 +00:00
|
|
|
# SwITNet Ltd © - 2023, https://switnet.net/
|
2022-04-07 08:48:52 +00:00
|
|
|
# GPLv3 or later.
|
|
|
|
|
2022-05-21 01:07:02 +00:00
|
|
|
while getopts m: option
|
|
|
|
do
|
|
|
|
case "${option}"
|
|
|
|
in
|
|
|
|
m) MODE=${OPTARG};;
|
|
|
|
\?) echo "Usage: sudo bash ./$0 [-m debug]" && exit;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
#DEBUG
|
|
|
|
if [ "$MODE" = "debug" ]; then
|
|
|
|
set -x
|
|
|
|
fi
|
|
|
|
|
2022-04-07 08:48:52 +00:00
|
|
|
wait_seconds() {
|
|
|
|
secs=$(($1))
|
|
|
|
while [ $secs -gt 0 ]; do
|
|
|
|
echo -ne "$secs\033[0K\r"
|
|
|
|
sleep 1
|
|
|
|
: $((secs--))
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2022-05-21 01:07:02 +00:00
|
|
|
echo "####
|
|
|
|
# WARNING: Only use this script if you know what you are doing.
|
|
|
|
# Under your own risk.
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY.
|
|
|
|
####"
|
|
|
|
|
2022-04-07 08:48:52 +00:00
|
|
|
# Check if user is root
|
2022-05-21 01:07:02 +00:00
|
|
|
if [ "$UID" != 0 ]; then
|
2022-04-07 08:48:52 +00:00
|
|
|
echo You need to run this script as root or sudo rights!
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
TMP_DIR="$(mktemp -d)"
|
|
|
|
KERNEL_LOG="$TMP_DIR/kernel_log"
|
|
|
|
GRUB_FILE="/etc/default/grub"
|
|
|
|
|
|
|
|
echo -e "# Check and update HWE kernel if possible...\n"
|
|
|
|
apt-get -q2 update
|
2022-05-21 01:07:02 +00:00
|
|
|
HWE_VIR_MOD="$(apt-cache madison linux-image-generic-hwe-"$(lsb_release -sr)" 2>/dev/null|head -n1|grep -c hwe-"$(lsb_release -sr)")"
|
2022-04-07 08:48:52 +00:00
|
|
|
if [ "$HWE_VIR_MOD" = "1" ]; then
|
|
|
|
apt-get -y install \
|
2022-05-21 01:07:02 +00:00
|
|
|
linux-image-generic-hwe-"$(lsb_release -sr)" \
|
|
|
|
linux-tools-generic-hwe-"$(lsb_release -sr)"
|
2022-04-07 08:48:52 +00:00
|
|
|
else
|
|
|
|
apt-get -y install \
|
|
|
|
linux-image-generic \
|
2022-05-21 01:07:02 +00:00
|
|
|
linux-modules-extra-"$(uname -r)"
|
2022-04-07 08:48:52 +00:00
|
|
|
fi
|
|
|
|
apt-get -y autoremove
|
|
|
|
apt-get autoclean
|
|
|
|
|
|
|
|
#Write update-grub output
|
2022-05-21 01:07:02 +00:00
|
|
|
update-grub > "$KERNEL_LOG" 2>&1
|
2022-04-07 08:48:52 +00:00
|
|
|
|
|
|
|
#Get clean output
|
2022-05-21 01:07:02 +00:00
|
|
|
awk -F'boot/' '{print$2}' < "$KERNEL_LOG"|sed '/^[[:space:]]*$/d' | \
|
|
|
|
tee "$KERNEL_LOG".tmp
|
|
|
|
mv "$KERNEL_LOG".tmp "$KERNEL_LOG"
|
2022-04-07 08:48:52 +00:00
|
|
|
|
|
|
|
echo -e "Check if AWS kernel is installed.\n"
|
2022-05-21 01:07:02 +00:00
|
|
|
[ "$(grep -wc aws "$KERNEL_LOG")" = 0 ] && echo "No AWS kernel found, exiting..." && exit
|
2022-04-07 08:48:52 +00:00
|
|
|
|
|
|
|
#Get kernel number
|
2022-05-21 01:07:02 +00:00
|
|
|
RAW_KERNEL_NUM="$(grep -Fn generic "$KERNEL_LOG"|head -n1|cut -d ':' -f1)"
|
2022-04-07 08:48:52 +00:00
|
|
|
FIXED_KERNEL_NUM="$(awk "BEGIN{ print $RAW_KERNEL_NUM - 1 }")"
|
|
|
|
|
|
|
|
echo -e "Set up GRUB for custom kernel.\n"
|
2022-05-21 01:07:02 +00:00
|
|
|
sed -i "s|GRUB_DEFAULT=.*|GRUB_DEFAULT=\"1\>$FIXED_KERNEL_NUM\"|" "$GRUB_FILE"
|
2022-04-07 08:48:52 +00:00
|
|
|
|
|
|
|
echo -e "Saving changes...\n"
|
|
|
|
update-grub
|
|
|
|
|
|
|
|
echo "Time to reboot..."
|
|
|
|
echo "Rebooting in..."
|
|
|
|
wait_seconds 15
|
|
|
|
reboot
|