Skip to content

Instantly share code, notes, and snippets.

@davidlohr
Last active March 13, 2016 21:16
Show Gist options
  • Save davidlohr/83df4897e5c405a46a5f to your computer and use it in GitHub Desktop.
Save davidlohr/83df4897e5c405a46a5f to your computer and use it in GitHub Desktop.
Automating tedious kernel hacking related tasks.
#!/bin/bash
#
# Copyright (C) 2016 Davidlohr Bueso.
#
# Download and build/install one or more flavors of the Linux kernel.
# Do what you please with this, but you better be root if you expect
# it to do anything useful. Also, probably very SUSE specific, this
# is my only working env.
#
# Usage:
# kbuild-auto.sh --config <config-file>
# [--kernels <master,v4.5,v4.5-rc1,...>]
# [--repo <linux|tip|rcu|next|...>]
# [--force] (removes any exising repository directory
# before cloning)
# [--cleanup] (removes cloned working directory once done)
#
# notes:
# o defaults will always be master branch on Linus' tree.
# o the script does everything inside working directory where called.
# examples:
# ./kbuid-auto.sh --config /boot/config-4.5.0-rc5
# ./kbuid-auto.sh --repo rcu --config /boot/config-4.5.0-rc5
# ./kbuid-auto.sh --config /boot/config-4.5.0-rc5 --kernels v4.4,v4.5 --force
#
# TODO (wishlist):
# o Change (grub) bootloader default.
# o When available, use bkp protocol if default git:// fails.
# o --repo-url <url> option, for non-tagged repositories.
#
KERNEL_DOTCONFIG="" # non-optional arg
GIT_REPO_ALIAS="linux"
KERNEL_STR="master"
GIT_REPO_FORCE=0 # do not overwrite an existing repo
GIT_REPO_CLEANUP=0 # cleanup after ourselves
NUMCPUS=$(grep -c '^processor' /proc/cpuinfo)
MAKE="make -j$((NUMCPUS*2))"
# Linus' and other common used trees for kernel development.
declare -A GIT_REPO_ARR
GIT_REPO_ARR["linux"]=git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
GIT_REPO_ARR["tip"]=git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git
GIT_REPO_ARR["next"]=git://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git
GIT_REPO_ARR["rcu"]=git://git.kernel.org/pub/scm/linux/kernel/git/paulmck/linux-rcu.git
GIT_REPO_ARR["net"]=git://git.kernel.org/pub/scm/linux/kernel/git/davem/net.git
GIT_REPO_ARR["block"]=git://git.kernel.org/pub/scm/linux/kernel/git/axboe/linux-block.git
# -- akpm
GIT_REPO_ARR["mmotm"]=git://git.cmpxchg.org/linux-mmotm.git
GIT_REPO_ARR["mmots"]=git://git.cmpxchg.org/linux-mmots.git
while [ "$1" != "" ]; do
case "$1" in
--config)
KERNEL_DOTCONFIG=$2
shift 2
;;
--kernels)
KERNEL_STR=$2
shift 2
;;
--repo)
GIT_REPO_ALIAS=$2
shift 2
;;
--cleanup)
GIT_REPO_CLEANUP="yes"
shift 1
;;
--force)
GIT_REPO_FORCE="yes"
shift 1
;;
*)
echo Unrecognised option: $1
shift
esac
done
#
# Input sanitation
#
if [ ! -f $KERNEL_DOTCONFIG ] || [ "$KERNEL_DOTCONFIG" = "" ]; then
echo "$0: Must pass a valid base configuration file (--config)"
exit 1
fi
# zypper in -y git-core gcc openssl-devel
GIT_CLONE_REPO=${GIT_REPO_ARR[$GIT_REPO_ALIAS]}
if [ "$GIT_CLONE_REPO" = "" ]; then
echo "$0: '$GIT_REPO_ALIAS' is not a valid repository alias (--repo)"
echo "$0: Available repositories are: ${!GIT_REPO_ARR[@]}"
exit 1
fi
if [ "$GIT_REPO_FORCE" = "yes" ]; then
echo "$0: Deleting any previous '$GIT_REPO_ALIAS' directory"
rm -rf $GIT_REPO_ALIAS # do not bother checking if it exists
fi
git clone $GIT_CLONE_REPO $GIT_REPO_ALIAS
cp $KERNEL_DOTCONFIG $GIT_REPO_ALIAS/.config && cd $GIT_REPO_ALIAS
#
# Lets do it!
#
IFS=',' read -a KERNEL_ARR <<< "$KERNEL_STR"
for KERNEL in ${KERNEL_ARR[@]}
do
# echo "Checking out and building '${KERNEL}'"
git checkout $KERNEL && make olddefconfig
# ^^ can return (ignorable) symbol warnings.
$MAKE && $MAKE modules && $MAKE modules_install && $MAKE install
done
if [ "$GIT_REPO_CLEANUP" = "yes" ]; then
cd .. && rm -rf $GIT_REPO_ALIAS
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment