Easy Way to build a mainline kernel on ubuntu

Compiling a Linux kernel on Ubuntu
———————————-

This should be useful in the event that the user wants to investigate whether a newer kernel allows
certain peripherals to work better (or at all) or some other problems they might encounter while using
the kernel officially provided by Canonical.

As an example, I use the 4.1.5 release from kernel.org.

1) Make sure you have the build environment set up first:

sudo apt-get build-dep linux-image-$(uname -r)

2) Grab the sources from kernel.org.

 mkdir linux_test_builds
    cd linux_test_builds
    wget -c https://www.kernel.org/pub/linux/kernel/v4.x/linux-4.1.5.tar.xz

3) Unpack the kernel:

tar xfv linux-4.1.5.tar.xz

4) Go to https://kernel.ubuntu.com/~kernel-ppa/mainline/v4.1.5-unstable/
(make sure to match the kernel version)

5) Now go into the “linux-4.1.5” directory and download the following patches:

cd linux-4.1.5
    wget -c https://kernel.ubuntu.com/~kernel-ppa/mainline/v4.1.5-unstable/0001-base-packaging.patch
    wget -c https://kernel.ubuntu.com/~kernel-ppa/mainline/v4.1.5-unstable/0002-debian-changelog.patch
    wget -c https://kernel.ubuntu.com/~kernel-ppa/mainline/v4.1.5-unstable/0003-configs-based-on-Ubuntu-4.1.0-3.3.patch

6) Apply the patches:

 patch -p1 <0001-base-packaging.patch
    patch -p1 <0002-debian-changelog.patch
    patch -p1 <0003-configs-based-on-Ubuntu-4.1.0-3.3.patch

(patch file names may change in the future)

7) We need to set the execute bit on some scripts

chmod a+x debian/rules

8) Time to build the kernel

fakeroot debian/rules clean
    fakeroot debian/rules do_extras_package=false do_tools=false binary-headers binary-generic

Note: On more recent version kernels, as Canonical experiments with ZFS, building 64bit
packages will fail. To prevent this from happening, use this command line:

fakeroot debian/rules do_mainline_build=true binary-headers binary-generic

(According to apw on Freenode / #ubuntu-kernel the contents of the -extra package
are merged into the main .deb package. Reason:
“we never build the mainline kernels with the extras package turned on because
we cannot guarentee the interdependancies are the same”)

9) Installing the new kernel packages:
(package names will differ, of course, but you know which ones to select)

 cd ..
    sudo dpkg --install linux-headers-4.1.5-040105-generic_4.1.5-040105.201508101730_amd64.deb linux-headers-4.1.5-040105_4.1.5-040105.201508101730_all.deb linux-image-4.1.5-040105-generic_4.1.5-040105.201508101730_amd64.deb

* the packages are:

linux-headers-4.1.5-040105-generic_4.1.5-040105.201508101730_amd64.deb
linux-headers-4.1.5-040105_4.1.5-040105.201508101730_all.deb
linux-image-4.1.5-040105-generic_4.1.5-040105.201508101730_amd64.deb

10) Notes for docker users

If you’re using docker, the default kernel on Trusty offers aufs. Docker will try to use it and fail
because aufs is not in the mainline kernel. The options are to either get the pacthes for aufs or tell
docker to use overlayfs which is included in the mainline tree since 3.18.

To make docker use overlayfs open /etc/default/docker and add this line

DOCKER_OPTS="-s overlay"

11) Documentation used to create this guide:

[1] https://askubuntu.com/questions/89542/how-to-build-the-mainline-kernel-source-package
[2] https://wiki.ubuntu.com/Kernel/BuildYourOwnKernel

12) Other resources:

[1] https://kernel.ubuntu.com/~kernel-ppa/mainline/
[2] https://www.kernel.org/

Leave a Reply