Skip to content

Instantly share code, notes, and snippets.

@rgov
Last active February 18, 2021 04:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgov/bdc9948ef8730e8bd993c7d6ff4c5b07 to your computer and use it in GitHub Desktop.
Save rgov/bdc9948ef8730e8bd993c7d6ff4c5b07 to your computer and use it in GitHub Desktop.
Building a custom Ubuntu kernel

I had a need to build a custom Ubuntu 18.04 kernel with a minor config option change.

The build script is based on these instructions from the Ubuntu wiki.

Differences from mainline

  • Enables interrupt support for the PCA9535 GPIO controller
  • Excludes the Nvidia DKMS modules

Building with Docker

It is recommended that the compilation be done in a Docker container:

# Ubuntu 18.04 Bionic
docker run --rm -it \
    -v "$(pwd):/output" \
    -e DISTRIB_CODENAME=bionic \
    -e KERNELVERS=4.15.0-122-generic \
    ubuntu:bionic \
    /output/build.sh

# Ubuntu 20.04 Focal
docker run --rm -it \
    -v "$(pwd):/output" \
    -e DISTRIB_CODENAME=focal \
    -e KERNELVERS=5.4.0-65-generic \
    ubuntu:focal \
    /output/build.sh

Note that it can take several hours to build the kernel and drivers.

The resulting .deb files will appear in the current directory.

Patching

The build script applies patches that are prefixed with the Ubuntu version codename, e.g., bionic-01-blah-blah.patch.

This patch file was produced by running
LANG=C fakeroot debian/rules editconfigs
and enabling the GPIO_PCA953X and GPIO_PCA953X_IRQ settings for the amd64 arch.
The Debian scripts propagate the old setting to all of the other architectures.
Because we are no longer building gpio_pca953x as a module, we also need to
disable a sanity check for the presence of the .ko file. This is done
dynamically in the build.sh script.
--------------------------------------------------------------------------------
diff -ru linux-4.15.0-orig/debian.master/config/amd64/config.flavour.generic linux-4.15.0/debian.master/config/amd64/config.flavour.generic
--- linux-4.15.0-orig/debian.master/config/amd64/config.flavour.generic 2021-02-12 17:36:12.590636000 +0000
+++ linux-4.15.0/debian.master/config/amd64/config.flavour.generic 2021-02-12 17:38:14.323530000 +0000
@@ -1,6 +1,7 @@
#
# Config options for config.flavour.generic automatically generated by splitconfig.pl
#
+CONFIG_GPIO_PCA953X=y
CONFIG_HZ=250
# CONFIG_HZ_1000 is not set
CONFIG_HZ_250=y
diff -ru linux-4.15.0-orig/debian.master/config/amd64/config.flavour.lowlatency linux-4.15.0/debian.master/config/amd64/config.flavour.lowlatency
--- linux-4.15.0-orig/debian.master/config/amd64/config.flavour.lowlatency 2021-02-12 17:36:12.590636000 +0000
+++ linux-4.15.0/debian.master/config/amd64/config.flavour.lowlatency 2021-02-12 17:38:14.314530000 +0000
@@ -1,6 +1,7 @@
#
# Config options for config.flavour.lowlatency automatically generated by splitconfig.pl
#
+CONFIG_GPIO_PCA953X=m
CONFIG_HZ=1000
CONFIG_HZ_1000=y
# CONFIG_HZ_250 is not set
diff -ru linux-4.15.0-orig/debian.master/config/arm64/config.common.arm64 linux-4.15.0/debian.master/config/arm64/config.common.arm64
--- linux-4.15.0-orig/debian.master/config/arm64/config.common.arm64 2021-02-12 17:36:12.590636000 +0000
+++ linux-4.15.0/debian.master/config/arm64/config.common.arm64 2021-02-12 17:38:40.079516000 +0000
@@ -134,6 +134,7 @@
CONFIG_GPIO_GENERIC=y
CONFIG_GPIO_GENERIC_PLATFORM=y
CONFIG_GPIO_MB86S7X=m
+CONFIG_GPIO_PCA953X=m
CONFIG_GPIO_TWL4030=m
CONFIG_GPIO_TWL6040=m
# CONFIG_HAVE_AOUT is not set
diff -ru linux-4.15.0-orig/debian.master/config/armhf/config.common.armhf linux-4.15.0/debian.master/config/armhf/config.common.armhf
--- linux-4.15.0-orig/debian.master/config/armhf/config.common.armhf 2021-02-12 17:36:12.590636000 +0000
+++ linux-4.15.0/debian.master/config/armhf/config.common.armhf 2021-02-12 17:38:40.078516000 +0000
@@ -117,6 +117,7 @@
CONFIG_GPIO_GENERIC=y
CONFIG_GPIO_GENERIC_PLATFORM=y
CONFIG_GPIO_MB86S7X=m
+CONFIG_GPIO_PCA953X=m
CONFIG_GPIO_TWL4030=y
CONFIG_GPIO_TWL6040=y
# CONFIG_HAVE_AOUT is not set
diff -ru linux-4.15.0-orig/debian.master/config/config.common.ubuntu linux-4.15.0/debian.master/config/config.common.ubuntu
--- linux-4.15.0-orig/debian.master/config/config.common.ubuntu 2021-02-12 17:36:12.591636000 +0000
+++ linux-4.15.0/debian.master/config/config.common.ubuntu 2021-02-12 17:38:40.073516000 +0000
@@ -3248,7 +3248,7 @@
CONFIG_GPIO_MXC=y
CONFIG_GPIO_OMAP=y
CONFIG_GPIO_PALMAS=y
-CONFIG_GPIO_PCA953X=m
+CONFIG_GPIO_PCA953X_IRQ=y
CONFIG_GPIO_PCF857X=m
CONFIG_GPIO_PCH=m
CONFIG_GPIO_PCI_IDIO_16=m
diff -ru linux-4.15.0-orig/debian.master/config/i386/config.common.i386 linux-4.15.0/debian.master/config/i386/config.common.i386
--- linux-4.15.0-orig/debian.master/config/i386/config.common.i386 2021-02-12 17:36:12.590636000 +0000
+++ linux-4.15.0/debian.master/config/i386/config.common.i386 2021-02-12 17:38:40.076516000 +0000
@@ -118,6 +118,7 @@
CONFIG_GPIO_GENERIC=m
CONFIG_GPIO_GENERIC_PLATFORM=m
CONFIG_GPIO_MB86S7X=m
+CONFIG_GPIO_PCA953X=m
CONFIG_GPIO_TWL4030=m
CONFIG_GPIO_TWL6040=m
CONFIG_HAVE_AOUT=y
diff -ru linux-4.15.0-orig/debian.master/config/ppc64el/config.common.ppc64el linux-4.15.0/debian.master/config/ppc64el/config.common.ppc64el
--- linux-4.15.0-orig/debian.master/config/ppc64el/config.common.ppc64el 2021-02-12 17:36:12.590636000 +0000
+++ linux-4.15.0/debian.master/config/ppc64el/config.common.ppc64el 2021-02-12 17:38:40.082516000 +0000
@@ -119,6 +119,7 @@
CONFIG_GPIO_GENERIC=y
CONFIG_GPIO_GENERIC_PLATFORM=m
CONFIG_GPIO_MB86S7X=m
+CONFIG_GPIO_PCA953X=m
CONFIG_GPIO_TWL4030=m
CONFIG_GPIO_TWL6040=m
# CONFIG_HAVE_AOUT is not set
#!/bin/bash
set -eu -o pipefail
shopt -s nullglob
# For example: 4.15.0-122-generic
KERNELVERS="${KERNELVERS:-$(uname -r)}"
# For example: bionic
[ -z "$DISTRIB_CODENAME" ] && source /etc/lsb-release
# Drop into an interactive shell on error
function on_error {
echo
echo
echo "An error occurred, you are being dropped into an interactive shell:"
exec /bin/bash
}
trap on_error ERR
# Add source repositories
echo "deb-src http://archive.ubuntu.com/ubuntu ${DISTRIB_CODENAME} main" >> /etc/apt/sources.list
echo "deb-src http://archive.ubuntu.com/ubuntu ${DISTRIB_CODENAME}-updates main" >> /etc/apt/sources.list
apt update
# Keep tzdata from asking us questions during install
ln -snf /usr/share/zoneinfo/UTC /etc/localtime
echo UTC > /etc/timezone
# Install dependencies
apt build-dep -y linux-image-$KERNELVERS
apt install -y \
bc \
bison \
cpio \
curl \
flex \
dkms \
gawk \
kernel-wedge \
kmod \
libelf-dev \
libssl-dev \
lz4 \
openssl
# Download the Linux sources
apt source linux-image-unsigned-$KERNELVERS
cd linux-*/
# Make scripts executable
chmod a+x debian/rules
chmod a+x debian/scripts/*
chmod a+x debian/scripts/misc/*
# Update the package version
sed -i -E '1 s/\((.*)\)/(\1+lrauv)/' debian.master/changelog
# Disable the module check
# https://wiki.ubuntu.com/KernelTeam/KernelMaintenance#Overriding_module_check_failures
PREV_ABIDIR=$(echo debian.master/abi/*/amd64)
sed -i "/gpio-pca953x/d" "$PREV_ABIDIR"/generic.modules
# Apply patches
for PATCH in "$(dirname "$0")/${DISTRIB_CODENAME}-"*.patch; do
patch -p1 < "$PATCH"
done
# On Ubuntu Bionic, the build process tries to download an Nvidia driver package
# that has been pruned from the package repository. Since our system does not
# use any Nvidia components, we just disable it.
sed -i '/do_dkms_nvidia/ s/true/false/' debian.master/rules.d/amd64.mk
# Build
LANG=C fakeroot debian/rules clean
LANG=C fakeroot debian/rules binary-headers binary-generic
# Move built packages
mv ../*.deb "$(dirname "$0")"/
See bionic-01-pca9535-interrupt-support.patch for details.
On Focal, the debian.master/config/annotations file must also be updated.
--------------------------------------------------------------------------------
diff -ru linux-5.4.0-orig/debian.master/config/amd64/config.common.amd64 linux-5.4.0/debian.master/config/amd64/config.common.amd64
--- linux-5.4.0-orig/debian.master/config/amd64/config.common.amd64 2021-02-15 20:19:18.486654797 -0500
+++ linux-5.4.0/debian.master/config/amd64/config.common.amd64 2021-02-15 20:21:55.861268695 -0500
@@ -168,7 +168,6 @@
CONFIG_GPIO_MAX7300=m
CONFIG_GPIO_MAX732X=m
CONFIG_GPIO_MB86S7X=m
-CONFIG_GPIO_PCA953X=m
CONFIG_GPIO_PCF857X=m
CONFIG_GPIO_SIOX=m
CONFIG_GPIO_TPIC2810=m
diff -ru linux-5.4.0-orig/debian.master/config/config.common.ubuntu linux-5.4.0/debian.master/config/config.common.ubuntu
--- linux-5.4.0-orig/debian.master/config/config.common.ubuntu 2021-02-15 20:19:18.485654797 -0500
+++ linux-5.4.0/debian.master/config/config.common.ubuntu 2021-02-15 20:21:55.860268695 -0500
@@ -3607,6 +3607,7 @@
CONFIG_GPIO_MXC=y
CONFIG_GPIO_OMAP=y
CONFIG_GPIO_PALMAS=y
+CONFIG_GPIO_PCA953X_IRQ=y
CONFIG_GPIO_PCH=m
CONFIG_GPIO_PCIE_IDIO_24=m
CONFIG_GPIO_PCI_IDIO_16=m
diff -ru linux-5.4.0-orig/debian.master/config/annotations linux-5.4.0/debian.master/config/annotations
--- linux-5.4.0-orig/debian.master/config/annotations 2021-02-15 20:19:18.484654797 -0500
+++ linux-5.4.0/debian.master/config/annotations 2021-02-15 20:35:44.112182860 -0500
@@ -1456,7 +1456,7 @@
CONFIG_GPIO_GW_PLD policy<{'arm64': 'm', 'armhf': 'm', 'ppc64el': 'm', }>
CONFIG_GPIO_MAX7300 policy<{'amd64': 'm', 'arm64': 'm', 'armhf': 'm', 'i386': 'm', 'ppc64el': 'm', 's390x': 'n'}>
CONFIG_GPIO_MAX732X policy<{'amd64': 'm', 'arm64': 'm', 'armhf': 'm', 'i386': 'm', 'ppc64el': 'm', 's390x': 'n'}>
-CONFIG_GPIO_PCA953X policy<{'amd64': 'm', 'arm64': 'm', 'armhf': 'm', 'i386': 'm', 'ppc64el': 'm', 's390x': 'n'}>
+CONFIG_GPIO_PCA953X policy<{'amd64': 'y', 'arm64': 'm', 'armhf': 'm', 'i386': 'm', 'ppc64el': 'm', 's390x': 'n'}>
CONFIG_GPIO_PCF857X policy<{'amd64': 'm', 'arm64': 'm', 'armhf': 'm', 'i386': 'm', 'ppc64el': 'm', 's390x': 'n'}>
CONFIG_GPIO_TPIC2810 policy<{'amd64': 'm', 'arm64': 'm', 'armhf': 'm', 'i386': 'm', 'ppc64el': 'm', 's390x': 'n'}>
CONFIG_GPIO_TS4900 policy<{'armhf-generic': 'm'}>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment