Original: http://blog.avirtualhome.com/compile-mainline-kernel-ubuntu/
This article will describe how you can compile the latest kernel from the official kernel.org git repository in a Ubuntu way. As you can tell I’m not referring to any kernel version because this will work for any kernel version you can pull from the official kernel.org repository, but for the better readability I’ll be compiling version 3.3.
git
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.
Architecture
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.
Flavor
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 exit
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/torvalds/linux.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 we want to compile
git tag|sort-V
In our case we will use version v3.3
In order to compile in the Ubuntu way we will add a Ubuntu remote repository to our local repository. For this article we will use the Ubuntu 12.04 (Precise) repository.
git remote add precise git://kernel.ubuntu.com/ubuntu/ubuntu-precise.git git fetch precise git checkout -f precise/master
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 a newer mainline version at a later time.
As determined earlier we will checkout v3.3 of the mainline kernel.
git checkout -b i7 v3.3 --
This will create a branch called i7.
We will pull in the needed Ubuntu files to compile like we already do in the other series I posted on the site here.
git checkout precise/master -- debian git checkout precise/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 = 3 SUBLEVEL = 0 EXTRAVERSION = NAME = Saber-toothed Squirrel
The version numbering we will be using is VERSION.PATCHLEVEL.SUBLEVEL, which makes the version in our case 3.3.0 but for the changelog we need to expand this.
I use the following format: 3.3.0-999.201203231943
The 999 is used so whenever Ubuntu comes out with a 3.3.0 kernel for the Ubuntu version we’re running, our version will seem to be the newer. The 201203231943 is just the date + timestamp. The timestamp is used when we want to recompile the 3.3.0 version after we make some config changes, just update the time stamp 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 protected]"; DEBFULLNAME="Peter van der Does"; dch -v 3.3.0-999.201203231943 --distribution precise --package linux --create -c debian.master/changelog Mainline build: v3.3.0
This will create the changelog in debian.master as follows
linux (3.3.0-999.201203231943) precise; urgency=low * Mainline build: v3.3.0 -- Peter van der Does <[email protected]> Fri, 23 Mar 2012 19:44:07 -0400 </[email protected]>
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 </eom>
The above script is available for download.
At this time we will commit our changes with git so we don’t lose them.
git commit -a -m "Our own kernel"
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.
debian/rules editconfigs
The script will ask us if we want to edit the particular 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 -xdf
Because we are going to be creating a new 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 release we use:
ls debian.master/abi
The previous release in this case is 3.2.0-19.31
cp debian.master/abi/3.2.0-19.31/amd64/genericdebian.master/abi/3.2.0-19.31/amd64/i7 cp debian.master/abi/3.2.0-19.31/amd64/generic.modules debian.master/abi/3.2.0-19.31/amd64/i7.modules
We need to edit some files:
Search for the line:
getall amd64 generic virtual
Change it in:
getall amd64 generic virtual i7
Search for the line:
flavours = generic virtual
Change it in:
flavours = generic virtual 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" supported="I7 Processor" target="Geared toward I7 desktop systems." desc="=HUMAN= SMP" bootloader="grub-pc | grub-efi-amd64 | grub-efi-ia32 | grub | lilo (>= 19.1)" provides="kvm-api-4, redhat-cluster-modules, ivtv-modules, ndiswrapper-modules-1.9"
We need to commit our changes in the git repository.
git add . git commit -a --amend
It’s finally time for compiling, 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/rulesclean
All the packages will be created in the directory /d1/development/kernel/vanilla
Create independent packages:
fakeroot debian/rulesbinary-indep
The above statement will create the following deb files:
linux-doc_3.3.0-999.201203231943_all.deb linux-headers-3.3.0-999_3.3.0-999.201203231943_all.deb linux-source-3.3.0_3.3.0-999.201203231943_all.deb linux-tools-common_3.3.0-999.201203231943_all.deb
Create the tools package:
fakeroot debian/rulesbinary-perarch
The above statement will create the following deb file:
linux-tools-3.3.0-999_3.3.0-999.201203231943_amd64.deb
Create the flavour depended files:
fakeroot debian/rulesbinary-i7
The above statement will create the following deb files:
linux-headers-3.2.0-8-i7_3.2.0-8.15_amd64.deb linux-image-3.2.0-8-i7_3.2.0-8.15_amd64.deb
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.3.0-999-i7_3.3.0-999.201203231943_amd64.deb linux-headers-3.3.0-999_3.3.0-999.201203231943_all.deb linux-image-3.3.0-999-i7_3.3.0-999.201203231943_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.3 kernel.