Automate Linux Kernel Builds
I wrote a small shell script™ to automate building new Linux kernels. I use this to build new kernels on my Debian systems:
#!/usr/bin/env bash
WORKSPACE='/home/gd/workspace'
LINUX="${WORKSPACE}/linux"
find "${WORKSPACE}" -type f \( -name 'linux-*deb' -o -name 'linux-upstream*' \) -mtime -3 -exec rm {} \;
cd "${LINUX}" || exit
git checkout .
git checkout master --force
git branch | while read -r branch; do
[[ "$branch" =~ ^v ]] && git branch -D "${branch}"
done
git pull
version=$(git tag | sort -V | grep -v -E '*-rc*' 2>/dev/null | tail -1)
git checkout tags/"${version}" -b "${version}"
make mrproper
yes "" | make localmodconfig
make -j"$(nproc --all)" bindeb-pkg
-
First I setup two constant values,
WORKSPACEandLINUX, to store paths to my workspace and my Linux source code directory, respectively. -
Next I delete old files. I use the
findcommand to search for files in myWORKSPACEthat match the specific patterns of old builds (linux-*debandlinux-upstream*) and that are older than 3 days, then I delete them. This helps with keeping my filesystem cleared of old builds and package files. -
Then I change the current directory to
LINUX. If the directory does not exist or the command fails, my script exits to prevent further commands from executing in the wrong directory. -
After than I reset and update the Linux kernel git repository:
-
git checkout .discards changes in the working directory. -
git checkout master --forceswitches to the master branch, discarding any changes or differences forcefully. - I then delete all git branches starting with "v". These are from previous builds and are no longer needed.
-
git pullupdates my local repository to the latest commit.
-
-
Next I select the latest stable version:
I find the latest git tag that does not include a release candidate (
-rc) version and set that as the current version. Then I check out this version into a new branch, naming it after the version tag. -
Finally I compile the Linux kernel:
-
make mrpropercleans my local source tree, removing any previous configurations and compiled files. -
make defconfigcreates a default configuration file. I do this so I'm not asked about any new defaults when I run the next command. -
make localmodconfigtailors my configuration for my exact system and also disables any modules that are not needed. -
make -j"$(nproc --all)" bindeb-pkgcompiles my kernel into Debian packages using all available processor cores, speeding up the compilation process.
-
