Skip to main content

Patching the kernel as a custom package in Archlinux

Following the adventure of patching the linux kernel, I tried to automate it a bit. Part of the process is well documented as always in the wiki

The idea is to fetch the official Archlinux sources for the linux kernel so we can modify them and add our patches.

Could I buy a graphics card with a normal HDMI port for a hundred bucks and skip all of this mess? Yes, but where is the fun in that?

Start by creating a new src folder in your homedir.

We will need the asp package that will take care of fetching the sources for us by issuing these commands:

asp update
asp export linux

I will also add my custom kernel patches in a patches/*.diff dir, you can see why I need to patch the kernel here

There is also another issue here: the config for the kernel issued by asp is different than the upstream package, meaning that we will need to modify it, otherwise my system, which is a BTRFS filesystem, will not boot:

cd linux
mv config config.old
sed -r "s/^(|#)CONFIG_BTRFS_FS=.*/CONFIG_BTRFS_FS=y/g" config.old > config

I also built a PKGBUILD patch with e.g diff -Naru PKGBUILD PKGBUILD.old > custom_PKGBUILD.patch to apply it on each kernel release, this gets rid of building the docs and their dependencies, also does a shallow clone and introduces my 0001-amdgpu-clock.patch patchfile. That way the build will take less time:

--- PKGBUILD	2022-10-29 12:28:27.038231602 +0200
+++ PKGBUILD	2022-10-29 12:28:27.038231602 +0200
@@ -1,6 +1,6 @@
 # Maintainer: Jan Alexander Steffens (heftig) <heftig@archlinux.org>
 
-pkgbase=linux
+pkgbase=linux-amdclock
 pkgver=6.0.5.arch1
 pkgrel=1
 pkgdesc='Linux'
@@ -10,14 +10,13 @@
 license=(GPL2)
 makedepends=(
   bc libelf pahole cpio perl tar xz
-  xmlto python-sphinx python-sphinx_rtd_theme graphviz imagemagick texlive-latexextra
   git
 )
 options=('!strip')
 _srcname=archlinux-linux
 source=(
-  "$_srcname::git+https://github.com/archlinux/linux?signed#tag=$_srctag"
   config         # the main kernel config file
+  0001-amdgpu-clock.patch
 )
 validpgpkeys=(
   'ABAF11C65A2970B130ABE3C479BE3E4300411886'  # Linus Torvalds
@@ -26,13 +25,14 @@
   'C7E7849466FE2358343588377258734B41C31549'  # David Runge <dvzrv@archlinux.org>
 )
 sha256sums=('SKIP'
-            '05168cbbeb6378eec6c84fe3300cede4fa5cf6130c39fb8af95040529bd390a6')
+            'SKIP')
 
 export KBUILD_BUILD_HOST=archlinux
 export KBUILD_BUILD_USER=$pkgbase
 export KBUILD_BUILD_TIMESTAMP="$(date -Ru${SOURCE_DATE_EPOCH:+d @$SOURCE_DATE_EPOCH})"
 
 prepare() {
+  git clone --depth=1 --branch=$_srctag "https://github.com/archlinux/linux" $_srcname
   cd $_srcname
 
   echo "Setting version..."
@@ -60,7 +60,7 @@
 
 build() {
   cd $_srcname
-  make htmldocs all
+  make all
 }
 
 _package() {

That patch is then applied like this: patch -p0 < ../custom_PKGBUILD.patch

Now the only thing left is to build and install it:

makepkg -s
sudo pacman -U linux-amdclock-*.zst

You can bundle all of it in a shell script to save time in further upgrades:

#!/bin/bash
rm -rf linux
asp update
asp export linux
cp patches/* linux
cd linux
mv config config.old
# enable support for / as BTRFS filesystem
sed -r "s/^(|#)CONFIG_BTRFS_FS=.*/CONFIG_BTRFS_FS=y/g" config.old > config
patch -p0 < ../custom_PKGBUILD.patch