The Linux kernel 3.9 was released on on Monday, April 29, 2013 by Linus and currently I’m running the new kernel on my Ubuntu 13.04 machine but you can do this on any Ubuntu version. This article will describe how you can compile the mainline kernel from the official kernel.org git repository in a Ubuntu way.
I’ll be using git to get the latest kernel version. This is my favorite way to get the sources and it is in my opinion the fastest way to make changes later on when you want to update your own kernel to the latest version.
I suggest adding my Launchpad repository to your system. The repository holds the latest version of git and is usually updated within a day of a new release of git, follow the instructions on the page Git Packages for Ubuntu to add my repository.
I am compiling the amd64 which basically is the version used for all 64 bit versions, if you want to compile for i386 you need replace amd64 for i386 throughout this article.
I choose the name i7 as the flavor name as I’ll be building a kernel geared towards my laptop which has a i7 processor. Besides the change of processor type in the configuration I also have some other changes but that’s beyond this article.
Let’s get started by preparing our machine for compiling the Linux kernel.
Open a terminal.
sudo su - apt-get install fakeroot build-essential devscripts apt-get install crash kexec-tools makedumpfile kernel-wedge apt-get build-dep linux-image-$(uname -r) apt-get install git libncurses5 libncurses5-dev libnewt-dev libunwind8-dev libaudit-dev libpython-dev exit
Create a directory where you would like to build your kernel, this directory will hold the kernel source in a sub directory and all the deb files will end up in this folder. I choose /d1/development/kernel/vanilla/
Getting the source
The mainline repository is big, so cloning the repository will take a while, depending on your Internet speed.
cd /d1/development/kernel/vanilla git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git source cd source
The source code of the kernel is installed in the directory source.
We’ll check the tags set by Linus to determine which version of the kernel we want to compile
git tag|sort -V
I don’t recommend the release candidates kernel at any time, just use the latest stable version, in our case v3.9
In order to compile the kernel in the Ubuntu way we will add a Ubuntu remote repository to our local repository. For this article we will use the Ubuntu 13.04 (Raring) repository.
git remote add raring git://kernel.ubuntu.com/ubuntu/ubuntu-raring.git git fetch raring
Creating a branch
We will create a branch in which we will be doing our modifications. That way the master branch will stay in tact, which will make it a whole lot easier when we want to update our own Ubuntu kernel with an updated mainline kernel at a later time.
As determined earlier we will checkout v3.9 of the kernel.
git checkout -b i7 v3.9 --
This will create a branch called i7.
Ubuntunize the branch
We will pull in the needed Ubuntu files to compile the kernel in the Ubuntu way.
git checkout raring/master -- debian git checkout raring/master -- debian.master
Create our own Changelog
In order to set the right version during the compilation, we need to change the changelog which is located in debian.master/changelog
First of all we need to determine which version of the mainline kernel we are compiling.
head -5 Makefile
This will display something like this
VERSION = 3 PATCHLEVEL = 9 SUBLEVEL = 0 EXTRAVERSION = NAME = Unicycling Gorilla
The version numbering we will be using is VERSION.PATCHLEVEL.SUBLEVEL, which makes the version in our case 3.9.0 but for the changelog we need to expand this.
I use the following format: 3.9.0-999.1
The 999 is used so whenever Ubuntu comes out with a 3.9.0 kernel for the Ubuntu version we’re running, our kernel will seem to be the newer. The .1 is just an extra and is used when we want to recompile the 3.9.0 version after we make some config changes, just increase this number and you have a newer kernel version.
It’s highly recommend to follow the version dash number dot number format.
The changelog needs a name and email address, if you want your own name and email change the DEBEMAIL and DEBFULLNAME.
To recreate the changelog
rm debian.master/changelog DEBEMAIL="email@example.com"; DEBFULLNAME="Peter van der Does"; dch -v 3.9.0-999.1 --distribution raring --package linux --create -c debian.master/changelog Mainline kernel build: v3.9.0
This will create the changelog in debian.master as follows
linux (3.9.0-999.1) raring; urgency=low * Mainline kernel build: v3.9.0 -- Peter van der Does <firstname.lastname@example.org> Mon, 29 Apr 2013 12:43:11 -0400
Modify check files
During modification of configuration files and during the compilation process, Ubuntu does some checks to see if it all looks good but as we are not compiling a kernel with Ubuntu patches these checks might fail. Therefor we will be modifying these files and instead of doing it manually, we’ll use a small script to accomplish the needed changes.
for i in debian/scripts/*-check debian.master/scripts/*-check do if [ -f "$i" ]; then cat - <<EOM >"$i" #!/bin/sh exit 0 EOM chmod 755 "$i" fi done
The above script is available for download.
The Ubuntu kernel maintainers also patch the kernel so it can compile for Hypervisor, if you don’t need this, you have to make a change to the file:
Change the line
do_hyperv = true
do_hyperv = false
If you want Hypervisor support in your kernel you need to change some other files, but I won’t get into this in this article.
At this time we will commit our changes with git so we don’t lose them.
git commit -a -m "Ubuntunized"
Creating a new kernel configuration
I’ll be using the method of creating a new flavor, this adds a bit more work but this allows you to always compile the original kernel, if you wanted to.
We’ll use the flavor you are currently running as the base for our own flavor, being i7. Please note that, as discovered by one of the readers of this blog, the extension needs to be in small caps.
cp /boot/config-`uname -r` debian.master/config/amd64/config.flavour.i7 fakeroot debian/rules clean defaultconfigs
To make changes to the configuration file we need to edit the configuration file. The kernel developers have created a script to edit kernel configurations which has to be called through the debian/rules makefile, unfortunately we will have to go through all the flavors for this script to work properly.
The script will ask us if we want to edit the particular kernel configuration. We should not make changes to any of the configurations until we see the i7 configuration.
Do you want to edit config: amd64/config.flavour.i7? [Y/n]
We make our changes, save the configuration and then keep answering no to any other questions until the script ends.
When we’re done, we will commit the changes into the git repository.
git add debian.master/config/amd64/config.flavour.i7 git commit -a --amend
Now we need to clean up the git tree in order to get ready for compilation.
git reset --hard git clean -df
Getting ready for compilation
Because we are going to be creating a new kernel flavor based on a existing flavor (generic in my case) we need to create some extra files. During compilation the process checks the previous release for some settings, as we’re creating a local flavor it doesn’t exist in the source, so we’re creating it.
To see the previous kernel release we use:
The previous release in this case is 3.8.0-18.28
cp debian.master/abi/3.8.0-18.28/amd64/generic debian.master/abi/3.8.0-18.28/amd64/i7 cp debian.master/abi/3.8.0-18.28/amd64/generic.modules debian.master/abi/3.8.0-18.28/amd64/i7.modules
We need to edit some files:
Search for the line:
getall amd64 generic
Change it in:
getall amd64 generic i7
Search for the line:
flavours = generic
Change it in:
flavours = generic i7
This files does not exist and in order to make the compilation process aware of our own flavor we want to compile we need to create it.
cp debian.master/control.d/vars.generic debian.master/control.d/vars.i7
You can edit the file and make it your own.
arch="i386 amd64 armhf" supported="i7 Processor" target="Geared toward i7 desktop systems." desc="=HUMAN= SMP" bootloader="grub-pc [i386 amd64 x32] | grub-efi-amd64 [i386 amd64 x32] | grub-efi-ia32 [i386 amd64 x32] | grub [i386 amd64 x32] | lilo (>= 19.1) [i386 amd64 x32] | flash-kernel [armhf arm64]" provides="kvm-api-4, redhat-cluster-modules, ivtv-modules"
We need to commit our changes in the git repository.
git add . git commit -a --amend
It’s finally time for compiling our kernel and to keep our newly created branch in pristine condition we will do the compilation in a separate branch. We keep our branch clean as this will help later on when we want to update our new branch to a newer kernel.
git checkout -b work fakeroot debian/rules clean
All the kernel packages will be created in the directory /d1/development/kernel/vanilla
Create independent packages:
PYTHON_CONFIG=x86_64-linux-gnu-python-config fakeroot debian/rules binary-indep
The above statement will create the following deb files:
linux-doc_3.9.0-999.1_all.deb linux-headers-3.9.0-999_3.9.0-999.1_all.deb linux-source-3.9.0_3.9.0-999.1_all.deb linux-tools-common_3.9.0-999.1_all.deb
Create the tools package:
fakeroot debian/rules binary-perarch
The above statement will create the following deb file:
Create the flavour depended files:
fakeroot debian/rules binary-i7
The above statement will create the following deb files:
Install the new Kernel
After the compilation is finished we’ll have the above packages in the parent directory.
To install the files
cd .. sudo dpkg -i linux-headers-3.9.0-999-i7_3.9.0-999.1_amd64.deb linux-headers-3.9.0-999_3.9.0-999.1_all.deb linux-image-3.9.0-999-i7_3.9.0-999.1_amd64.deb
Check the bootloader if the newly installed Ubuntu kernel is the default one, for grub check the file /boot/grub/menu.lst or if we run grub2 check /boot/grub/grub.cfg
Reboot and enjoy our newly installed Linux 3.9 kernel.