Compile i3/i5/i7/xeon optimized linux kernel on Ubuntu 12.04 x32

Why you would need to compile kernel? Compiling kernel is not recommended so do not do it except you need a very good reason. There is a few reasons i can think of:

  1.  You have unusual hardware and you need to build that driver into the kernel (keep in mind that you can compile driver as module so it will be loaded upon boot, but in some cases you really need it into kernel – one example is hard disk controller that is not supported in current kernel release)
  2.  You need to optimize the kernel for you computer. Linux kernel works on P4 also, and you’re probalby using Core2Duo or similar architecture and you want the kernel to be compiled for your CPU to squeeze last bit of MHz. Also, you can set preempt for box purpose.
  3. You just want to experiment and educate yourself and want to see how’s new kernel is working.

Whatever your motivation is, be careful what you do as you can expect to stuck with kernel loading at start of booting process. You can compile the kernel in two ways: – Using debian helpers to produce DEB files which you can install/uninstall later on – Using manual compiling for which you must manually compile and install kernel files I recommend first method becouse it’s quite easier to compile/install/uninstall packages. This script assumes you want to compile kernel in /root/kernel location, and if not, you need to change /root/kernel to your location. It will download plain 3.4.1 kernel (last one for which you can find ubuntu precise patches), patch it, fix compiler parameters (for core2duo/compatible cpus) and compile it to DEB package files. It uses PAE x32 extension (i found somewhere it’s more secure than just standard x32) so you can still see >4GB memory (with some limitations). So if you want to compile x64 kernel you must change this script.

This script do the compile automatically but it’s good for you to understand what it does, line by line.
Let’s start on first line, requirements

sudo apt-get install fakeroot build-essential crash kexec-tools makedumpfile kernel-wedge libncurses5 binutils-dev libelf-dev libdw-dev libnewt-dev libncurses5-dev python-dev bison flex
sudo apt-get –no-install-recommends install asciidoc xmlto

This 2 lines install necessary pre-requisites to build kernel, next you download kernel source from http://www.kernel.org. It seems that ubuntu team latest patch for ubuntu 12.04 is 3.4.1, so we chose the right one. In our example, we go to root folder and unpack sources there. It’s easier that way to follow procedure and not needing to change script/commands to specific home path. You can, offcourse, do it in your home folder also, if you wish, but keep in mind you need to change alot of paths.

sudo -s
mkdir -p /root/kernel
cd /root/kernel
wget -c http://www.kernel.org/pub/linux/kernel/v3.0/linux-3.4.1.tar.bz2

Now, if you’re pre-compiling or experimenting it’s a good idea to delete all previous kernel patches released by Ubuntu team, just in case…

rm 000*

There is a branch for ubuntu precise (12.04) in kernel.ubuntu.com so we download our patches from there:

wget -c http://kernel.ubuntu.com/~kernel-ppa/mainline/v3.4-precise/0001-base-packaging.patch
wget -c http://kernel.ubuntu.com/~kernel-ppa/mainline/v3.4-precise/0002-debian-changelog.patch
wget -c http://kernel.ubuntu.com/~kernel-ppa/mainline/v3.4-precise/0003-default-configs.patch

If there’s previous builds/versions of linux kernel unpacked (for testing, perhaps?) delete them:

rm -r -f linux
rm -r -f linux-3.4.1

We now need to extract the kernel source so we can patch it with ubuntu patches:

tar xjvf linux-3.4.1.tar.bz2

And create softlink of current unpacked version to linux:

ln -s linux-3.4.1 linux

Change path to linux source

cd /root/kernel/linux

Apply ubuntu kernel patches:

patch -p1 < ../0001-base-packaging.patch
patch -p1 < ../0002-debian-changelog.patch
patch -p1 < ../0003-default-configs.patch

Make debian scripts executable:

chmod -Rv +x debian/scripts/

Create specific flavor. I called it Xeon becouse i compiled kernel for xeon, but in fact, this optimization is done for all new cpus. Kernel have option: Core2/Newer Xeon, which means from Core2 onward, including i3, i5, i7. Ubuntu official kernel is build to support every CPU from P4, so by choosing new CPU batch you break the compatibility with older CPUs. Keep that in mind if you want to install deb packages on another computer.

cp debian.master/config/i386/config.flavour.generic-pae debian.master/config/i386/config.flavour.xeon

Clean compile tree and update compile configs where you edit only config.flavour.xeon with your new compile parameters. If you are building server i recommend to set Preempt to none. Choose Processor type to be Core2/Newer Xeon. Some people report desktop to be more responsive with 1000Hz kernel, so you can try it if you want. You can also disable stuff you don’t use. If you’re unsure, leave it, it may lead to unusable kernel.

fakeroot debian/rules clean
fakeroot debian/rules updateconfigs
fakeroot debian/rules editconfigs

Copy abi entries for xeon flavor. Also change the entries to be optimized for i7/xeon using sed.

cp debian.master/abi/3.0.0-12.20/i386/generic-pae debian.master/abi/3.0.0-12.20/i386/xeon
cp debian.master/abi/3.0.0-12.20/i386/generic-pae.modules debian.master/abi/3.0.0-12.20/i386/xeon.modules
sed -i s/getall\ i386\ generic\ server\ virtual/getall\ amd64\ generic\ server\ virtual\ xeon/g debian.master/etc/getabis
sed -i s/\=\ generic\ server\ virtual/\=\ generic\ server\ virtual\ xeon/g debian.master/rules.d/i386.mk
cp debian.master/control.d/vars.generic debian.master/control.d/vars.xeon
sed -i s/\”Generic\”/\”core\ i7\”/g debian.master/control.d/vars.xeon

Change Makefiles for gcc/g++ to produce i7 optimized binaries:

sed -i s/\=\ gcc/\=\ gcc\ -march\=corei7\ -mtune\=corei7/g Makefile
sed -i s/\=\ g++/\=\ g++\ -march\=corei7\ -mtune\=corei7/g Makefile
sed -i s/core2/corei7/g arch/x86/Makefile
sed -i s/core2/corei7/g arch/x86/Makefile_32.cpu

Do the sync for pre-building:

sync

Clean the fakeroot and start building:

fakeroot debian/rules clean
skipabi=true skipmodule=true fakeroot debian/rules binary-indep
skipabi=true skipmodule=true fakeroot debian/rules binary-perarch
time skipabi=true skipmodule=true no_dumpfile=yes fakeroot debian/rules binary-xeon

Compiler will detect how many cores you have in system and start parallel build. Depending on your computer speed, mainly CPU, you will have to wait more or less. Build speed also depends on how many entries in kernel config you turned off, especially whole sections. For example, you probalby don’t need to compile 10Gbit ethernet drivers support. Server don’t need to build whole framebuffer (which is alot of sources).

Below are needed files to compile this kernel version. It seems that some parts of kernel sources are not done properly and miss some things, like hv_kvp_daemon.8. This script fix all that.

buildkernel.sh
3-binary-indep.mk
config.flavour.xeon (optional)

This entry was posted in Uncategorized. Bookmark the permalink.

5 Responses to Compile i3/i5/i7/xeon optimized linux kernel on Ubuntu 12.04 x32

  1. aceleron says:

    You can say what this commands do:

    skipabi=true skipmodule=true

    thanks.

  2. aceleron says:

    I appreciate your goodwill, but I left that idea a little aside now, because the performance gain that I will be very little and also because I’m out of time. Anyway, thank you for your time also.

Leave a comment