Computing and Networks

Kernel patch and compile the Debian/Ubuntu way

Continuing on with my theme of compiling and patching linux kernels a la:

Kernel patch and compile the Fedora way
A more elegant: Quick and dirty kernel patch and build

Here is how I patch and compile new kernels for Ubuntu/Debian distributions. More specifically, create .deb files to be used by the Advanced Packaging Tool (apt)

Prerequisites

make sure your system is updated and you have the required applications and libraries to compile a kernel.

$ sudo apt update
$ sudo apt install git kernel-package libncurses5-dev fakeroot build-essential -y

There are two main ways to get the kernel source.  One is to grab the ubuntu source using apt

apt-get source linux-image-$(uname -r)

or using git cloning the tree that matches your repository name direct from Ubuntu

git clone git://kernel.ubuntu.com/ubuntu/ubuntu-artful.git

Then there is the other way that I like. I go straight to the source, kernel.org. This gives me access to new or release candidate versions.

You can download a tarball every time or keep that kernel and patch it to new versions as they are released (this saves on A LOT of bandwidth and time)

OR

As mentioned in other posts, use git, clone the official kernel tree and create a local branch every time you want to try a patch. This way I find best for me.

I’ll quickly go the first steps, for a more descriptive walk through see: A more elegant: Quick and dirty kernel patch and build .

$ cd
$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
$ cd ~/linux
[~/linux]$ git tag -l | more
[~/linux]$ git checkout -b custom v4.14

Applying patches is the same (applying the acs patch here)

[~/linux]$ git am ~/Downloads/add-acs-overrides.patch

and same with creating a .config, either copy an existing one over from your /boot directory that you are happy with or your latest one with

[~/linux]$ cp /boot/config-`uname -r`* ./.config

If you do this, be sure to run

[~/linux]$ make oldconfig

This will run you through the new options, prompting you for the usual y/n/m responses. You can now edit the .config file to customise/optimise your kernel or go straight to compiling and creating the deb files. While editing the .config file, I highly recommend setting CONFIG_DEBUG_INFO=n to stop the *dbg* package being generated (unless you have a use case for it) and CONFIG_MODULES=y

*an easy and interactive method for editing the .config file. but requires ncurses is to type:

[~/linux]$ make menuconfig

STEP TWO: Compile and make packages.

Compiling and creating the .deb packages is a relatively straight forward one liner. But there are some important options and alternatives as well as caveats and work arounds. I will go through some here that fall within my workflow that I’m explaining here.

from this point on tools really need to be run as root. But you really really should compile as root [note. add reasons/links]. So we will use fakeroot.

Start with a clean source tree (this will preserve your existing .config file

[~/linux]$ fakeroot make-kpkg clean

Now compile and create .deb packages

[~/linux]$ $ fakeroot make-kpkg -j 8 --initrd LOCALVERSION=.acs.custom binary-arch

The -j (–jobs) option acts like it does for the gcc make command and sets the “CONCURRENCY_LEVEL” i.e. the number cpu threads assigned for concurrent use for the compile.  In this case, 8 threads of a 4 core CPU.

LOCALVERSION= creates our custom entry to version suffix of the kernel name. This defines the final name for the .deb files while also helping distinguish this kernel version from others in the GRUB menu. e.g.
Ubuntu 17.10, kernel 4.14.0.acs.custom

The binary-arch target is one of the faster build targets, it creates two .deb files called linux_headers and the other,  linux_image.  For more information on the other targets and the files they generate, read through the make-kpkg man page [here]

Caveat: If your kernel images are being built as “-dirty” on the end of the version string, and you used the git method to clone the source tree, as well as, patching or modified  the local branch.  This is the result of your modifications being committed.   You can ideally commit the changes to the tree HEAD or just simply create an empty .scmversion file in the root of the kernel sources i.e

[~/linux]$ touch .scmversion

STEP THREE: Install the files

The created .deb files can found in your home directory and installed an example of installing them would be

[~/linux]$ sudo dpkg -I linux-*acs.custom*.deb

reboot, select the kernel and test

Beer and profit.

here are the links to “official” methods on the Ubuntu wiki. I’ve never done it this way, your mileage may differ using these.
https://wiki.ubuntu.com/KernelTeam/GitKernelBuild
https://wiki.ubuntu.com/Kernel/BuildYourOwnKernel

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s