Installing my custom kernel on my Ubuntu Desktop

Today I had to customize my Ubuntu Precise kernel. I was required to change the number of maximum pages which can be allocated contiguously so that I could dma_alloc_coherent more memory.
For this I needed to make changes in the linux/mmzone.h file and then build this kernel and boot my Ubuntu with it.

Following is how I did it:

1.    Find your current kernel version: Issue the following command in your Ubuntu command line to find which version of kernel is currently installed in your machine. Mine is:
root@ubuntu:~# uname –r
3.2.0-23-generic
2.    To change anything in your kernel you will need its source. Ubuntu doesn’t generally have your kernel source already, as it isn’t required by most users. To get your Ubuntu kernel’s source just issue this command:
root@ubuntu:~# apt-get install linux-source
         This will install the kernel source for your version of kernel.
3.    You can know where your source has been installed:
root@ubuntu:~# dpkg –L linux-source-3.2.0
/.
/usr
/usr/src
/usr/src/linux-source-3.2.0.tar.bz2
/usr/src/gtest
/usr/src/linux-headers-3.2.0-23-generic
...
4.    The linux-source-3.2.0.tar.bz2 is the package containing your linux kernel source. We untar it.
root@ubuntu:~# tar jxvf linux-source-3.2.0.tar.bz2
/usr/src/linux-source-3.2.0/debian
/usr/src/linux-source-3.2.0/debian/tests
/usr/src/linux-source-3.2.0/debian/tests/README
/usr/src/linux-source-3.2.0/debian/tests/check-aliases
/usr/src/linux-source-3.2.0/debian/changelog
/usr/src/linux-source-3.2.0/debian/scripts
/usr/src/linux-source-3.2.0/debian/scripts/module-check
/usr/src/linux-source-3.2.0/debian/scripts/control-create
/usr/src/linux-source-3.2.0/debian/scripts/config-check
/usr/src/linux-source-3.2.0/debian/scripts/sub-flavour
/usr/src/linux-source-3.2.0/debian/scripts/misc
/usr/src/linux-source-3.2.0/debian/scripts/misc/getabis
/usr/src/linux-source-3.2.0/debian/scripts/misc/insert-changes.pl
/usr/src/linux-source-3.2.0/debian/scripts/misc/retag
/usr/src/linux-source-3.2.0/debian/scripts/misc/insert-mainline-changes
/usr/src/linux-source-3.2.0/debian/scripts/misc/git-ubuntu-log
/usr/src/linux-source-3.2.0/debian/scripts/misc/insert-ubuntu-changes
/usr/src/linux-source-3.2.0/debian/scripts/misc/kernelconfig
5.    To make switching to the linux kernel source directory easy, we create a symbolic link to it:
root@ubuntu:~# ln –s /usr/src/linux-source-3.2.0/linux-source-3.2.0 linux

6.    Now switch to the directory where the source is installed.
root@ubuntu:~# cd /usr/src/linux          (symbolic link makes it easy to switch to the dir)

7.    Open the linux/mmzone.h.
root@ubuntu:~# vi include/linux/mmzone.h

8.    Find the macro MAX_ORDER and change it.
#define MAX_ORDER 14
//#define MAX_ORDER 11

9.    Save the file and exit.
10.    We need a .config file. We copy the one for the current kernel:
root@ubuntu:~# cp /boot/config-`uname –r` /usr/src
NOTE: If you need to change something in the configuration file or using make menuconfig then please perform step 10 before step 9.

11.    Now we build the kernel.
root@ubuntu:~# make-kpkg clean
root@ubuntu:~# fakeroot make-kpkg  --initrd --append-to-version=-custom kernel_image kernel_headers
NOTE: Use fakeroot if you do not have access to root user. “—append-to-version” appends a string after the kernel image which will be made so that you realize it is your custom made kernel. We want to build kernel headers as well as the kernel image.

12.    After the build you will see two such file one directory up:
linux-headers-3.2.59-custom_3.2.59-custom-10.00.Custom_amd64.deb – This is the headers image
linux-image-3.2.59-custom_3.2.59-custom-10.00.Custom_amd64.deb – This is the kernel image

13.    Now we will make our custom kernel as the default kernel:
root@ubuntu:~# dpkg -i linux-image-3.2.59-custom_3.2.59-custom-10.00.Custom_amd64.deb
root@ubuntu:~# dpkg -i linux-headers-3.2.59-custom_3.2.59-custom-10.00.Custom_amd64.deb

14.    Now simply reboot the machine. Once booting is done check the kernel version using uname –r:
root@ubuntu:~# uname –r
3.2.59-custom

Happy Kernelling !

No comments:

Post a Comment