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+ echo " Cleaning up.."
27+ popd
28+ rm -rf $TMP_BUILD_DIR
29+ }
1630
1731confirm () {
18- if [[ " $* " == * " -y" * ]]; then
32+ if [[ " $* " == * " --no-install" * ]]; then
33+ echo " Not installing new kernel."
34+
35+ if [[ " $* " == * " --tidy" * ]]; then
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,48 @@ 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+ }
62+
63+ apply_series_mbox () {
64+ git am $1 --empty=drop
65+ }
66+
67+ apply_series_link () {
68+ patch_url=$( cat $1 )
69+ echo " Fetching mbox from:" $patch_url
70+ wget -O lore.mbox.gz " $patch_url /t.mbox.gz"
71+ gunzip lore.mbox
72+ apply_series_mbox lore.mbox
73+ rm lore.mbox
74+ }
75+
76+ apply_patch_or_series () {
77+ case " $1 " in
78+ * .patch) apply_patch_file $1 ;;
79+ * .mbox) apply_series_mbox $1 ;;
80+ * .lore) apply_series_link $1 ;;
81+ * )
82+ echo " Uknown patch file: " $1
83+ exit 1
84+ ;;
85+ esac
86+ }
87+
88+ check_override_presence () {
89+ while IFS= read -r line; do
90+ if ! grep -Fq " $line " .config; then
91+ echo " Missing config: $line "
92+ exit 1
93+ fi
94+ done < " $KERNEL_CONFIG_OVERRIDES "
95+
96+ echo " All overrides correctly applied.."
97+ }
3898
3999KERNEL_URL=$( cat kernel_url)
40100KERNEL_COMMIT_HASH=$( cat kernel_commit_hash)
41- KERNEL_VERSION=$( cat kernel_version)
42101KERNEL_PATCHES_DIR=$( pwd) /patches
43102KERNEL_CONFIG_OVERRIDES=$( pwd) /kernel_config_overrides
44103
@@ -57,9 +116,9 @@ git fetch --depth 1 origin $KERNEL_COMMIT_HASH
57116git checkout FETCH_HEAD
58117
59118# Apply our patches on top
60- for PATCH in $KERNEL_PATCHES_DIR /* .patch ; do
119+ for PATCH in $KERNEL_PATCHES_DIR /* .* ; do
61120 echo " Applying patch:" $( basename $PATCH )
62- git apply $PATCH
121+ apply_patch_or_series $PATCH
63122done
64123
65124echo " Making kernel config ready for build"
@@ -72,19 +131,31 @@ make olddefconfig
72131scripts/config --disable SYSTEM_TRUSTED_KEYS
73132scripts/config --disable SYSTEM_REVOCATION_KEYS
74133
134+ # We run this again to default options now changed by
135+ # the disabling of the ubuntu keys
136+ make olddefconfig
137+
75138# Apply our config overrides on top of the config
76- scripts/kconfig/merge_config.sh .config $KERNEL_CONFIG_OVERRIDES
139+ scripts/kconfig/merge_config.sh -m .config $KERNEL_CONFIG_OVERRIDES
77140
78- # Finally run olddefconfig again to make sure any
79- # new options are configured before build
80- make olddefconfig
141+ check_override_presence
81142
82143echo " Building kernel this may take a while"
83- make -j $( nproc)
144+ make -s - j $( nproc)
84145echo " Building kernel modules"
85- make modules -j $( nproc)
146+ make modules -s - j $( nproc)
86147echo " Kernel build complete!"
87148
149+ KERNEL_VERSION=$( KERNELVERSION=$( make -s kernelversion) ./scripts/setlocalversion)
150+
151+ echo " New kernel version:" $KERNEL_VERSION
152+
153+ # Make sure a user really wants to install this kernel
154+ confirm " $@ "
155+
156+ check_root
157+ check_ubuntu
158+
88159echo " Installing kernel modules..."
89160make INSTALL_MOD_STRIP=1 modules_install
90161echo " Installing kernel..."
@@ -96,6 +167,4 @@ update-grub
96167
97168echo " Kernel built and installed successfully!"
98169
99- # Some cleanup after we are done
100- popd
101- rm -rf $TMP_BUILD_DIR
170+ tidy_up
0 commit comments