22# Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved.
33# SPDX-License-Identifier: Apache-2.0
44
5- # We need sudo privilleges to install the kernel
6- if [ " $( id -u) " -ne 0 ]; then
7- echo " This script must be run as root or with sudo privileges"
8- exit 1
9- fi
10-
11- # Currently this script only works on Ubuntu instances
12- if ! grep -qi ' ubuntu' /etc/os-release; then
13- echo " This script currently only works on Ubuntu."
14- exit 1
15- fi
5+ # fail if we encounter an error, uninitialized variable or a pipe breaks
6+ set -eu -o pipefail
7+
8+ check_root () {
9+ # We need sudo privilleges to install the kernel
10+ if [ " $( id -u) " -ne 0 ]; then
11+ echo " To install, this script must be run as root or with sudo privileges"
12+ exit 1
13+ fi
14+ }
15+
16+ check_ubuntu () {
17+ # Currently this script only works on Ubuntu instances
18+ if ! grep -qi ' ubuntu' /etc/os-release; then
19+ echo " This script currently only works on Ubuntu."
20+ exit 1
21+ fi
22+ }
23+
24+ tidy_up () {
25+ # Some cleanup after we are done
26+ popd
27+ rm -rf $TMP_BUILD_DIR
28+ }
1629
1730confirm () {
18- if [[ " $* " == * " -y" * ]]; then
31+ if [[ " $* " == * " --no-install" * ]]; then
32+ echo " Not installing new kernel."
33+
34+ if [[ " $* " == * " --tidy" * ]]; then
35+ echo " Cleaning up.."
36+ tidy_up
37+ fi
38+
39+ exit 0
40+ fi
41+
42+ if [[ " $* " == * " --install" * ]]; then
1943 return 0
2044 fi
2145
2246 while true ; do
23- echo " This script will build and install a new kernel. Run this script at your own risk"
24- read -p " Do you want to continue? (y/n) " yn
47+ read -p " Do you want to install the new kernel? (y/n) " yn
2548 case $yn in
2649 [Yy]* ) return 0 ;;
2750 [Nn]* )
@@ -33,12 +56,51 @@ confirm() {
3356 done
3457}
3558
36- # Make sure a user really wants to run this script
37- confirm " $@ "
59+ apply_patch_file () {
60+ git apply $1
61+ return 0
62+ }
63+
64+ apply_series_mbox () {
65+ git am $1 --empty=drop
66+ return 0
67+ }
68+
69+ apply_series_link () {
70+ patch_url=$( cat $1 )
71+ echo " Fetching mbox from:" $patch_url
72+ wget -O lore.mbox.gz " $patch_url /t.mbox.gz"
73+ gunzip lore.mbox
74+ apply_series_mbox lore.mbox
75+ rm lore.mbox
76+ return 0
77+ }
78+
79+ apply_patch_or_series () {
80+ case " $1 " in
81+ * .patch) apply_patch_file $1 ;;
82+ * .mbox) apply_series_mbox $1 ;;
83+ * .lore) apply_series_link $1 ;;
84+ * )
85+ echo " Uknown patch file: " $1
86+ exit 1
87+ ;;
88+ esac
89+ }
90+
91+ check_override_presence () {
92+ while IFS= read -r line; do
93+ if ! grep -Fq " $line " .config; then
94+ echo " Missing config: $line "
95+ exit 1
96+ fi
97+ done < " $KERNEL_CONFIG_OVERRIDES "
98+
99+ echo " All overrides correctly applied.."
100+ }
38101
39102KERNEL_URL=$( cat kernel_url)
40103KERNEL_COMMIT_HASH=$( cat kernel_commit_hash)
41- KERNEL_VERSION=$( cat kernel_version)
42104KERNEL_PATCHES_DIR=$( pwd) /patches
43105KERNEL_CONFIG_OVERRIDES=$( pwd) /kernel_config_overrides
44106
@@ -57,9 +119,9 @@ git fetch --depth 1 origin $KERNEL_COMMIT_HASH
57119git checkout FETCH_HEAD
58120
59121# Apply our patches on top
60- for PATCH in $KERNEL_PATCHES_DIR /* .patch ; do
122+ for PATCH in $KERNEL_PATCHES_DIR /* .* ; do
61123 echo " Applying patch:" $( basename $PATCH )
62- git apply $PATCH
124+ apply_patch_or_series $PATCH
63125done
64126
65127echo " Making kernel config ready for build"
@@ -72,19 +134,31 @@ make olddefconfig
72134scripts/config --disable SYSTEM_TRUSTED_KEYS
73135scripts/config --disable SYSTEM_REVOCATION_KEYS
74136
137+ # We run this again to default options now changed by
138+ # the disabling of the ubuntu keys
139+ make olddefconfig
140+
75141# Apply our config overrides on top of the config
76- scripts/kconfig/merge_config.sh .config $KERNEL_CONFIG_OVERRIDES
142+ scripts/kconfig/merge_config.sh -m .config $KERNEL_CONFIG_OVERRIDES
77143
78- # Finally run olddefconfig again to make sure any
79- # new options are configured before build
80- make olddefconfig
144+ check_override_presence
81145
82146echo " Building kernel this may take a while"
83- make -j $( nproc)
147+ make -s - j $( nproc)
84148echo " Building kernel modules"
85- make modules -j $( nproc)
149+ make modules -s - j $( nproc)
86150echo " Kernel build complete!"
87151
152+ KERNEL_VERSION=$( KERNELVERSION=$( make -s kernelversion) ./scripts/setlocalversion)
153+
154+ echo " New kernel version:" $KERNEL_VERSION
155+
156+ # Make sure a user really wants to install this kernel
157+ confirm " $@ "
158+
159+ check_root
160+ check_ubuntu
161+
88162echo " Installing kernel modules..."
89163make INSTALL_MOD_STRIP=1 modules_install
90164echo " Installing kernel..."
@@ -96,6 +170,4 @@ update-grub
96170
97171echo " Kernel built and installed successfully!"
98172
99- # Some cleanup after we are done
100- popd
101- rm -rf $TMP_BUILD_DIR
173+ tidy_up
0 commit comments