Using GitHub Actions to Build Linux Kernels – 萌え豚's Blog

abdelhousni2 pts0 comments

Using Github Actions to Build Linux Kernels | 萌え豚's Blog<br>Table of Contents

note<br>This article was last updated on September 21, 2023, 2 years ago. The content may be out of date.

Google released BBR v3 nearly two months ago. To try it out, we have to use a kernel which is compiled from a source that has this patch. We can compile the kernel ourselves or be lazy and use a kernel compiled by others. But what fun is there if we take the easy way?<br>About GitHub Actions#<br>GitHub Actions can be used to automate software workflows, from building to deploying. It&rsquo;s free of charge for standard GitHub-hosted runners in public repositories and has 2000 minutes limit per month for private repositories. Their runners have decent hardware resources for building linux kernels.<br>Setting up Build Environment#<br>Although GitHub Actions have Ubuntu installed on their linux runners and Ubuntu can be used to build linux kernels, we choose to use Docker instead because Docker makes it easy to test a configuration and make the build process portable and reproducible.<br>Docker has multi-stage builds that allow us to write a Dockerfile in logical steps. It will help us to debug and maintain the Dockerfile. We use the latest stable version of debian as the build environment.<br>Adding deb-src#<br>Because the sources.list of the debian docker doesn&rsquo;t have deb-src in them, we have to add them back. This is useful later when we need to install build dependencies.<br>FROM debian:12.1 AS deb-src<br>COPY "EOF" /etc/apt/sources.list<br>deb http://deb.debian.org/debian bookworm main<br>deb-src http://deb.debian.org/debian bookworm main

deb http://deb.debian.org/debian-security/ bookworm-security main<br>deb-src http://deb.debian.org/debian-security/ bookworm-security main

deb http://deb.debian.org/debian bookworm-updates main<br>deb-src http://deb.debian.org/debian bookworm-updates main<br>EOF

note<br>This uses heredocs in Dockerfiles, more info can be found here.

Installing Build Dependencies#<br>According to debian&rsquo;s official documentation, we just need to run these three commands to install build dependencies:<br>FROM deb-src AS install-dependency<br>RUN "EOF"<br>apt-get update<br>apt-get install build-essential wget git -y<br>apt-get build-dep linux -y<br>EOF

Downloading the Kernel Config#<br>To compile a kernel, we also need a configuration file that records what parts of the kernel should be compiled. We extract the one from the official kernel:<br>FROM install-dependency AS download-boot<br>RUN "EOF"<br>cd /<br>mkdir debian_config<br>cd debian_config<br>wget http://security.debian.org/debian-security/pool/updates/main/l/linux-signed-amd64/linux-image-6.1.0-12-cloud-amd64_6.1.52-1_amd64.deb -q -O kernel.deb<br>ar -x kernel.deb<br>tar xf data.tar.xz<br>EOF

note<br>We could parse the output of apt download --print-uris to determine the download url for the package.

Cloning the BBR Source#<br>We then need to use git to clone the source of BBR:<br>FROM download-boot as download-bbr<br>RUN "EOF"<br>cd /<br>git clone https://github.com/google/bbr.git -b v3<br>EOF

Building and Packaging the Kernel#<br>Finally, we need to build and package the kernel:<br>FROM download-bbr as builder<br>RUN "EOF"<br>cd /bbr<br>cp /debian_config/boot/config-6.1.0-12-cloud-amd64 .config<br>export BRANCH=`git rev-parse --abbrev-ref HEAD | sed s/-/+/g`<br>export SHA1=`git rev-parse --short HEAD`<br>export LOCALVERSION=+${BRANCH}+${SHA1}+GCE<br>export GCE_PKG_DIR=${PWD}/gce/${LOCALVERSION}/pkg<br>export GCE_INSTALL_DIR=${PWD}/gce/${LOCALVERSION}/install<br>export GCE_BUILD_DIR=${PWD}/gce/${LOCALVERSION}/build<br>export KERNEL_PKG=kernel-${LOCALVERSION}.tar.gz2<br>export MAKE_OPTS="-j`nproc` \<br>LOCALVERSION=${LOCALVERSION} \<br>EXTRAVERSION="" \<br>INSTALL_PATH=${GCE_INSTALL_DIR}/boot \<br>INSTALL_MOD_PATH=${GCE_INSTALL_DIR}"<br>mkdir -p ${GCE_BUILD_DIR}<br>mkdir -p ${GCE_INSTALL_DIR}/boot<br>mkdir -p ${GCE_PKG_DIR}<br>make olddefconfig<br>make ${MAKE_OPTS} prepare<br>make ${MAKE_OPTS}<br>make ${MAKE_OPTS} modules<br>make ${MAKE_OPTS} install<br>make ${MAKE_OPTS} modules_install<br>cd ${GCE_INSTALL_DIR}<br>tar -cvzf /kernel.tar.gz2 boot/* lib/modules/* --owner=0 --group=0<br>EOF

note<br>This part is adapted from gce-install.sh.

Setting up GitHub Actions#<br>To use GitHub Actions, we need to create a repository. Put the Dockerfile in the root and create a yaml file at the path .github/workflows under the root of the repository. We can name the yaml file whatever we want.<br>Here is what our workflow file looks like:<br># This is a basic workflow to help you get started with Actions

name: Compile Linux Kernel with BBRv3

# Controls when the workflow will run<br>on:<br># Allows you to run this workflow manually from the Actions tab<br>workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel<br>jobs:<br># This workflow contains a single job called "build"<br>build:<br># The type of runner that the job will run on<br>runs-on: ubuntu-latest

# Steps represent a sequence of tasks that will be executed as part of the job<br>steps:<br># Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it<br>- uses:...

debian build kernel github from actions

Related Articles