Thursday, February 16, 2012

Recompiling Ubuntu Kernel


I decided to make a post on recompiling your Ubuntu kernel after after taking a look at the instructions on the Ubuntu wiki. Although the instructions were correct it was a big jumbled mess and almost impossible to figure out what was what from the instructions. This post will cover recompiling the kernel that comes with Ubuntu. The reasons for doing this are to keep the current Ubuntu patches and configs and simply add some stuff of your own. This would be useful for adding a patch of some sort or adding support in the kernel .config for a piece of hardware or software which may not be enabled by default.
For this article I will be using Ubuntu 10.10. these instructions should also work for 9.10 and 10.04 but earlier versions had a slightly different process.
Ok so lets get started:
NOTE: I do every thing as root but I will add sudo to the commands for the people that are not comfortable working as root.
1. Install the required packages
sudo apt-get install fakeroot kernel-wedge build-essential makedumpfile kernel-package libncurses5 libncurses5-dev
2. Next issue the following command
1sudo apt-get build-dep --no-install-recommends linux-image-$(uname -r)
3. Next e need to create a working directory and download the Ubuntu image which correspond’s with our running kernel.
1mkdir ~/source
2cd ~/source
3apt-get source linux-image-$(uname -r)
4cd linux-2.6.35
4. Now, since we are just rebuilding the current kernel we can use the current .config file as a starting point.
1cp -vi /boot/config-`uname -r` .config
5. At this point if we have any patch’s we can add those. Generally a patch come in the form of a .patch file and needs to be applied in the top level directory of the source which in this case would be ~/source/linux-2.6.35
1patch -p1 < example.patch
Do this for all patch’s you may need to apply.
6. Next we will open the ncurses editor for the .config file. This is where all the support is defined for the kernel.
1make menuconfig
I’m going to assume you know what kernel options you need. This is also a good chance to remove support for anything you do not need. Smaller kernel with less bloat can greatly improve performance.
7. Save the menu file and exit the interface.
8. At this point you should be in the ~/source/linux-2.6.35 directory again
9. A little trick you can do is to set the CONCURRENCY_LEVEL variable to speed up the compile of the kernel. The number should be the number of processors you have plus one. So in my case I have a dual core processor so I will add one which would be three.
1export CONCURRENCY_LEVEL=3
10. Lets build it! You need to add a custom string to the end to mark your new kernel. In this case I added “-QD” to the end but you can add what ever you want
1make-kpkg clean
2fakeroot make-kpkg --initrd --append-to-version=-QD kernel-image kernel-headers
11. At this point you should go grab some food because the kernel compile can take a while
12. Once the kernel is built it will be one directory up in the ~/source file we were originally working in.
1cd ~/source
2sudo dpkg -i linux-image-2.6.35.(This part will be whatever name you gave it).deb
3sudo dpkg -i linux-headers-2.6.35.(This part will be whatever name you gave it).deb
There will only be these 2 debs so use tab complete to get the correct names. The dpkg commad will do the actual installing of the kernel.
13. Now we need to make a initramfs.
1sudo update-initramfs -c -k 2.6.35+QD
You will need to add your kernel version + the custom string you added on the end.
14. Now lets update grub
1sudo update-grub
15. Reboot and you should be rocking with your new kernel! 






http://www.question-defense.com/2010/09/26/how-to-recompile-your-ubuntu-10-10-kernel-for-patching-or-to-add-support-for-a-specific-device

No comments:

Post a Comment