improvements
[automated-distro-installer] / fai / config / distro-install-common / install-mainline-kernel-debs
1 #!/bin/bash
2 # This file is part of Ian Kelling's automated-distro-installer
3 # Copyright (C) 2024 Ian Kelling
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 set -eE -o pipefail
20 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
21
22 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
23
24 # default
25 kernel_ver='6\.6'
26 case $1 in
27 stable)
28 # note: update kernel_ver when we are ready to jump to a new stable kernel.
29 # Stable kernels are listed here: https://www.kernel.org/category/releases.html
30 kernel_ver='6\.6'
31 ;;
32 unstable)
33 kernel_ver='[1-9]'
34 ;;
35 esac
36
37
38 prereqs=()
39 for p in wget curl; do
40 if ! type -p $p &>/dev/null; then
41 prereqs+=($p)
42 fi
43 done
44 if (( ${#prereqs[@]} >= 1 )); then
45 apt-get -y install ${prereqs[@]}
46 fi
47
48
49 tmpdir=$($ROOTCMD mktemp -d) || exit
50 # shellcheck disable=SC2154 # defined by fai
51 outertmp=$target/$tmpdir
52 trap 'cd; rm -rf "$outertmp"' EXIT
53 cd $outertmp
54
55 # We get 10 versions cuz maybe the latest directory (or few) get created but not populated.
56 tmps=$(curl -s https://kernel.ubuntu.com/mainline/ | \
57 sed -rn 's,.*alt="\[DIR\]".*href="([^/]+).*,\1,p' | \
58 grep -v -- -rc | sed 's/^v//' | grep "^$kernel_ver" | sort -Vr | head -n10)
59 mapfile -t latest_versions <<<"$tmps"
60
61 for va in "${latest_versions[@]}"; do
62 sleep .2 # be nice
63 # note the wiki page about these says to install linux-headers.*generic.*amd64, but
64 # as of 2024, they have a requirement of a very new glibc, and people report
65 # that installing it is not needed.
66 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' )
67 if [[ $tmpstr ]]; then
68 mapfile -t pkgs <<<"$tmpstr"
69 break
70 fi
71 done
72
73 if (( ${#pkgs[@]} != 3 )); then
74 echo "$0: error. expected to find 3 kernel packages, got: ${pkgs[*]}" >&2
75 exit 1
76 fi
77
78 urls=()
79 for p in ${pkgs[@]}; do
80 if ! $ROOTCMD dpkg -s -- "${p%%_*}" 2>&1 | grep -Fx "Status: install ok installed" &>/dev/null; then
81 urls+=(https://kernel.ubuntu.com/mainline/v$va/amd64/$p)
82 fi
83 done
84 if (( ${#urls[@]} >= 1 )); then
85 wget -nv "${urls[@]}"
86 $ROOTCMD dpkg -i ${pkgs[@]/#/$tmpdir/}
87 fi