Skip to content

Instantly share code, notes, and snippets.

@rawiriblundell
Last active September 30, 2019 07:56
Show Gist options
  • Select an option

  • Save rawiriblundell/a3c440a9021877da5093eaec3070b776 to your computer and use it in GitHub Desktop.

Select an option

Save rawiriblundell/a3c440a9021877da5093eaec3070b776 to your computer and use it in GitHub Desktop.
Scripted version of a reddit post detailing installing a more recent kernel
#!/bin/bash
# Scripted version of this reddit post:
# https://www.reddit.com/r/linuxmint/comments/dawx7s/how_i_fixed_my_ubuntu_base_to_get_the_best/
# Beerware Licence and standard no warranty, no liability disclaimers
# TO-DO: Perhaps insert a reboot after the kernel installation
# Define any extra packages that we want
extra_packages=(
amd64-microcode
fancontrol
fwupd
fwupdate
i2c-tools
intel-microcode
lm-sensors
libsensors4-dev
mrtgutils-sensors
read-edid
thermald
)
# Function to list available kernels
get_available_kernels() {
apt-cache search linux-image-[0-9].*generic | sort -nr | awk '{print $1}'
}
# This function formats our output
# Options:
# -e prints a non-fatal error to stderr and returns 1 from the function
# -l prints a long line with the provided text e.g. --[ provided text here----(dashes continue)
# -x prints a fatal error to stderr and exits
# * Any text provided without an arg is treated as an informational output
send_user() {
case "${1}" in
(-a) printf -- '\e[32m--[ ATTENTION ]-- %s\e[0m\n' "${*:2}" ;;
(-e) printf -- '\e[31m--[ ERROR ]-- %s\e[0m\n' "${*:2}" >&2; return 1 ;;
(-l)
shift
local lnlen="${COLUMNS:-$(tput cols)}"
local msg="${*}"
local msglen="${#msg}"
lnlen=$(( lnlen - (msglen + 6) ))
printf -- '\e[32m--[ %s ]%s\e[0m\n' "${msg}" "$(printf '%*s' "${lnlen}" '' | tr ' ' -)"
;;
(-x) printf -- '\e[31m--[ ERROR ]-- %s\e[0m\n' "${*:2}" >&2; exit 1 ;;
(*) printf -- '\e[32m--[ INFO ]-- %s\e[0m\n' "${*}" ;;
esac
}
# Ensure that we're running with root privileges
(( UID != 0 )) && send_user -x "This script must be run as root"
# Ensure that we're on an Ubuntu or a derivative like Mint
uname -a | grep -q Ubuntu || \
send_user -x "This script is for Ubuntu, Mint and other Ubuntu derivatives"
# Send a greeting message
send_user -l Welcome
printf -- '%s\n' "This script reconfigures Ubuntu, Mint etc for a more recent kernel" \
"This adds hardware support, various fixes and improvements"
# Install this PPA and update.
# This PPA is canonical STABLE dont be scared by the wording.
# Its post Ubuntu Mainline.
# In 6 years Ive never had an issue. ever.
# Its how FOSS works. gems hiding in plain site.
send_user -l "Ensuring kernel PPA is configured"
add-apt-repository --yes ppa:canonical-kernel-team/proposed
send_user -l "Ensuring oibaf graphics drivers PPA is configured"
add-apt-repository --yes ppa:oibaf/graphics-drivers
send_user -l "Grabbing latest list of updates"
apt-get update
send_user -l "Performing full-upgrade"
apt-get --yes dist-upgrade
send_user -l "Cleaning up and fixing packages where necessary"
apt-get install --yes --fix-broken
apt-get install --yes --fix-missing
apt-get --yes autoclean
apt-get --yes autoremove
# Get a list of kernels and build an array
readarray -t avail_kernels < <(get_available_kernels)
# Offer the user a choice of available kernels
send_user -a "Please select a kernel version from the following list"
select target_kernel in "${avail_kernels[@]}"; do
send_user "${target_kernel} selected"
break
done
# Extract our version information from the selection
target_version=$(cut -d '-' -f3- <<< "${target_kernel}")
# We should only need the following packages to get all our required deps
send_user -l "Installing target kernel: ${target_kernel}"
apt-get install --yes \
linux-headers-"${target_version}" \
linux-modules-extra-"${target_version}" \
linux-headers-"${target_version/generic/lowlatency}" \
linux-modules-"${target_version/generic/lowlatency}"
send_user -l "Installing extra packages"
apt-get install --yes --reinstall --install-recommends "${extra_packages[@]}"
send_user -l "Running firmware check and update"
systemctl start fwupd
fwupdmgr refresh
fwupdmgr update
send_user -l "Performing cursory grub update"
update-grub
update-initramfs -c -k all
send_user -l "Script complete!"
printf '%s\n' "Everything should now be ready. Reboot and enjoy!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment