various fixes, improvements, shellcheck
[automated-distro-installer] / fai / config / distro-install-common / install-mainline-kernel-debs
diff --git a/fai/config/distro-install-common/install-mainline-kernel-debs b/fai/config/distro-install-common/install-mainline-kernel-debs
new file mode 100755 (executable)
index 0000000..93f7c57
--- /dev/null
@@ -0,0 +1,87 @@
+#!/bin/bash
+# This file is part of Ian Kelling's automated-distro-installer
+# Copyright (C) 2024 Ian Kelling
+
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+
+set -eE -o pipefail
+trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
+
+[[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
+
+# default
+kernel_ver='6\.6'
+case $1 in
+  stable)
+    # note: update kernel_ver when we are ready to jump to a new stable kernel.
+    # Stable kernels are listed here: https://www.kernel.org/category/releases.html
+    kernel_ver='6\.6'
+    ;;
+  unstable)
+    kernel_ver='[1-9]'
+    ;;
+esac
+
+
+prereqs=()
+for p in wget curl; do
+  if ! type -p $p &>/dev/null; then
+    prereqs+=($p)
+  fi
+done
+if (( ${#prereqs[@]} >= 1 )); then
+  apt-get -y install ${prereqs[@]}
+fi
+
+
+tmpdir=$($ROOTCMD mktemp -d) || exit
+# shellcheck disable=SC2154 # defined by fai
+outertmp=$target/$tmpdir
+trap 'cd; rm -rf "$outertmp"' EXIT
+cd $outertmp
+
+# We get 10 versions cuz maybe the latest directory (or few) get created but not populated.
+tmps=$(curl -s https://kernel.ubuntu.com/mainline/ | \
+         sed -rn 's,.*alt="\[DIR\]".*href="([^/]+).*,\1,p' | \
+         grep -v -- -rc | sed 's/^v//' | grep "^$kernel_ver" | sort -Vr | head -n10)
+mapfile -t latest_versions <<<"$tmps"
+
+for va in "${latest_versions[@]}"; do
+  sleep .2 # be nice
+  # note the wiki page about these says to install linux-headers.*generic.*amd64, but
+  # as of 2024, they have a requirement of a very new glibc, and people report
+  # that installing it is not needed.
+  tmpstr=$(curl -s https://kernel.ubuntu.com/mainline/v$va/amd64/CHECKSUMS | awk '$2 ~ /^linux-/ { print $2 }' | sort -u | sed '/linux-headers.*generic.*amd64/d' )
+  if [[ $tmpstr ]]; then
+    mapfile -t pkgs <<<"$tmpstr"
+    break
+  fi
+done
+
+if (( ${#pkgs[@]} != 3 )); then
+  echo "$0: error. expected to find 3 kernel packages, got: ${pkgs[*]}" >&2
+  exit 1
+fi
+
+urls=()
+for p in ${pkgs[@]}; do
+  if ! $ROOTCMD dpkg -s -- "${p%%_*}"  2>&1 | grep -Fx "Status: install ok installed" &>/dev/null; then
+    urls+=(https://kernel.ubuntu.com/mainline/v$va/amd64/$p)
+  fi
+done
+if (( ${#urls[@]} >= 1 )); then
+  wget -nv "${urls[@]}"
+  $ROOTCMD dpkg -i ${pkgs[@]/#/$tmpdir/}
+fi