Monday, July 18, 2011

How to Compile a Kernel for Debian ( Squeeze)

Every one wonders about how to compile a kernel ? On contrary never checks to compile ones own.. I say its very easy..

Prerequisites: 


ABC of Linux - yes you just need to know the basics of Linux, there is no rocket science

Building the kernel: (Note same steps hold for Ubuntu)


We basically build a .deb package that can be installed into the Debian system

Step 1:


aptitude update && aptitude install dkms kernel-package libncurses5-dev wget bzip2 fake-root build-essential
 This will install all the packages required to compile our kernel

Step 2:


Go to http://kernel.org/ and get the latest stable kernel package. ( During the post latest available stable kernel was 2.6.39.3 )., so here is what I did . Made a directory named kernel for easy understanding
mkdir kernel
cd kernel/
wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.39.3.tar.bz2
Step 3:

Extract the package and move to its directory
tar xvf linux-2.6.39.3.tar.bz2
cd linux-2.6.39.3 
Step 4: ( Only if you wish to patch the kernel with aufs modules, else you can skip this step)


The following commands will patch the kernel source with auf2 modules
git clone http://git.c3sl.ufpr.br/pub/scm/aufs/aufs2-standalone.git aufs
cd aufs
git checkout -q aufs 2.1
cd ..
patch -f -p1 < aufs/aufs2-kbuild.patch
patch -f -p1 < aufs/aufs2-base.patch
patch -f -p1 < aufs/aufs2-standalone.patch
cp -frv aufs/Documentation/* Documentation/
cp -frv aufs/fs/* fs/
cp -fv aufs/include/linux/aufs_type.h include/linux/
Follow these steps as provided above else you may fail to load the aufs configurations


Step 5:


Now lets start the build process of our kernel
make clean and make mrproper
Step 6 i: ( if you want to have the same config of your existing kernel)

Configuring the kernel :

If you wish to have the same config of your previous existing kernel follow the steps

cp /boot/config-`uname -r` ./.config 
this will load the existing kernel config file

now type

make menuconfig
Step 6 ii : ( if you want to have a custom config of your own)

Configuring the kernel :

copy your custom made .config to the current directory

and type
make menuconfig

Step 7:

You will now be displayed with a screen

Go to Load an Alternate Configuration File and choose .config (this is the .config file we copied to our directory in step 6) and press OK.

Step 8:


Now we will make our own custom kernel


make-kpkg clean
fakeroot make-kpkg --initrd --stem extraversion
  --revision revision  --append-to-version=-customversion kernel_image kernel_headers
You need to give these 3 values of your own in the above command:
  extraversion - give your own extraversion for kernel
 revision- the revision you want the kernel to be
customversion   - the custom revision of the kernel "

And sit back for a couple of hours since it takes time

After successful compilation of the kernel you will see two .deb packages in the ../ folder
1. Kernel image file .deb
2. Kernel Header file .deb

Step 9:


Installing the kernel we compiled

dpkg -i linux-image-2.6.39*
dpkg -i linux-headers-2.6.39*

(you may type the whole file name as dpkg -i linux-image-2.6.39.3-custom_2.6.39.3-custom-1.00.custom_i386.deb depending on whatever is generated in your case) .

Restart your system and you should see your kernel in the grub menu or else verify the kernel version with

uname -a

Congrats you have successfully compiled the Linux kernel for Debian and its derivatives :))

Note:If you face any issues comment this post and i will reply you. 




2 comments:

  1. Probably doing something wrong, I'm using this command in step 8 :

    time fakeroot make-kpkg --initrd --stem --revision --append-to-version=-xxx kernel_image kernel_headers

    I got this error :

    The UTS Release version in include/generated/utsrelease.h

    ""

    does not match current version:

    "2.6.39.3-xxx"

    ReplyDelete
  2. Please check the Updated post... the previous text went missing because it had some <> tags

    ReplyDelete