Files
Linux-Kernel-Programming_2E/ch3/kbuild.sh

88 lines
2.6 KiB
Bash
Raw Normal View History

2022-10-21 17:03:17 +05:30
#!/bin/bash
# kbuild.sh
# ***************************************************************
# * This program is part of the source code released for the book
# * "Linux Kernel Programming", 2nd Ed
# * (c) Author: Kaiwan N Billimoria
# * Publisher: Packt
# * GitHub repository:
# * https://github.com/PacktPublishing/Linux-Kernel-Programming_2E
# *
# * From: Ch 3 : Building the 6.x Linux Kernel from Source - Part 2
# ****************************************************************
# * Brief Description:
2022-10-21 17:03:17 +05:30
# Simple kernel build script; minimally tested, YMMV!
# ****************************************************************
2023-02-14 10:29:32 +05:30
# Turn on Bash 'strict mode'! V useful to catch potential bugs/issues early.
2023-02-14 10:29:32 +05:30
# ref: http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
2022-10-21 17:03:17 +05:30
name=$(basename $0)
NUMCORES=$(nproc)
JOBS=$((${NUMCORES}*2))
CONFIGURE=1
BUILD_INSTALL_MOD=1
2023-05-05 11:11:05 +05:30
die()
{
echo >&2 "$@"
exit 1
2022-10-21 17:03:17 +05:30
}
2023-05-05 11:11:05 +05:30
#--- 'main'
[[ $# -ne 1 ]] && die "Usage: ${name} path-to-kernel-src-tree-to-build"
2023-02-14 10:29:32 +05:30
set +u
2023-05-05 11:11:05 +05:30
[[ ! -z "${ARCH}" ]] && {
2022-10-21 17:03:17 +05:30
echo "${name}: you seem to want to build the kernel for arch ${ARCH}"
echo "This simple script currently supports only x86_64"
exit 1
}
2023-02-14 10:29:32 +05:30
set -u
2022-10-21 17:03:17 +05:30
cd $1 || exit 1
echo "Version: $(head Makefile)"
2023-05-05 11:11:05 +05:30
if [[ ${CONFIGURE} -eq 1 ]] ; then
2022-10-21 17:03:17 +05:30
lsmod > /tmp/lsmod.now
echo "[+] make LSMOD=/tmp/lsmod.now localmodconfig"
make LSMOD=/tmp/lsmod.now localmodconfig
echo "[+] make oldconfig"
make oldconfig # Update current config utilising a provided .config as base
echo "[+] make menuconfig "
2023-05-05 11:11:05 +05:30
make menuconfig && echo || die "menuconfig failed"
2022-10-21 17:03:17 +05:30
ls -l .config
else
2023-02-14 10:29:32 +05:30
echo "[-] Skipping kernel configure, just running 'make oldconfig'"
2022-10-21 17:03:17 +05:30
echo "[+] make oldconfig"
make oldconfig # Update current config utilising a provided .config as base
fi
2023-02-14 10:29:32 +05:30
2023-05-05 11:11:05 +05:30
# Ensure config is sane; on Ubuntu, SYSTEM_REVOCATION_KEYS being enabled causes
# the build to fail..
2023-02-14 10:29:32 +05:30
echo
2022-10-21 17:03:17 +05:30
echo "[+] scripts/config --disable SYSTEM_REVOCATION_KEYS"
scripts/config --disable SYSTEM_REVOCATION_KEYS
2023-02-14 10:29:32 +05:30
echo
2022-10-21 17:03:17 +05:30
echo "[+] time make -j${JOBS}"
2023-05-05 11:11:05 +05:30
time make -j${JOBS} && echo || die "make <kernel> *failed*"
[[ ! -f arch/x86/boot/bzImage ]] && die "make <kernel> *failed*? arch/x86/boot/bzImage not gen."
2022-10-21 17:03:17 +05:30
2023-05-05 11:11:05 +05:30
if [[ ${BUILD_INSTALL_MOD} -eq 1 ]] ; then
2023-02-14 10:29:32 +05:30
echo
echo "[+] sudo make modules_install "
2023-05-05 11:11:05 +05:30
sudo make modules_install || die "*Failed* modules install"
2022-10-21 17:03:17 +05:30
else
2023-02-14 10:29:32 +05:30
echo "[-] Skipping kernel modules build and install step"
2022-10-21 17:03:17 +05:30
fi
2023-02-14 10:29:32 +05:30
echo
2022-10-21 17:03:17 +05:30
echo "[+] sudo make install"
2023-05-05 11:11:05 +05:30
sudo make install || die "*Failed*"
2022-10-21 17:03:17 +05:30
echo "
2023-05-05 11:11:05 +05:30
Done, reboot, select your new kernel from the bootloader menu & boot it
(If not already done, you first need to configure GRUB to show the menu at boot)"
2022-10-21 17:03:17 +05:30
exit 0