Sunday, 11 October 2009

How to Compile the Linux Kernel in GNU/Debian and Ubuntu

NOTE: Tested in GNU/Debian Sid. Ubuntu versions should also work.


OBTAIN KERNEL SOURCE CODE:

Obviously the first step is obtaining the source code.

It is recommended to place and build linux kernel source code in /usr/src directory, although it is not required.

We create our "Linux_Kernel_Source" directory for this task.
$mkdir Linux_Kernel_Source
$cd Linux_Kernel_Source


Linux kernel source code is freely available in Internet:
http://www.kernel.org
specifically, version 2.6:
http://www.kernel.org/pub/linux/kernel/v2.6

We download and uncompress latest source code (2.6.31 when this article was written):
#aptitude install wget bzip2 # packages needed to get the kernel source code and uncompress it.
$wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.31.tar.bz2

Uncompress the source:
$tar xvjf linux-2.6.31.tar.bz2

/usr/src/linux is a symlink to linux sources or at least linux kernel headers. It is required.
#ln -s linux-2.6.31 /usr/src/linux

$cd linux-2.6.31


KERNEL CONFIGURATION:

There are several ways to configure the linux kernel build process. We are gonna use the console way (using make menuconfig).

#aptitude install ncurses-dev # This package is needed to execute make menuconfig.


NOTE: If we would want to execute make gconfig (gtk configuring way) we would need to install:
gtk+-2.0, glib-2.0 and libglade-2.0

We exec:
make menuconfig

A console menu will appear. There always are three options:
select: selects which kernel part to configure. Pressing SPACE key you can select if it is going to be built as a kernel module or built in within the kernel itself.
exit: To exit from the kernel part we are configuring or to exit the configuration process.
help: Provides good info about what choices do you have.


If you do not want to configure everything in the kernel again (It is huge!), you could search in the /boot directory for configuration files:

Every configuration file begins with config followed by its kernel version.

We copy one of them to our linux kernel source directory:
$cp /boot/config-2.6.26-1-686 /usr/src/linux

To select that configuration file you have to select "load alternate file" in the make menuconfig configuration process.
To make permament the changes you have done, and include them in the compilation, select "save alternate file" and save it as ".config"



BUILDING AND COMPILING THE KERNEL:

Once we are finished configuring the kernel compilation process, we are going to compile it.

To get some help about options we have:
$make help # shows all different options we could choose.

We now build the kernel and its modules:
$make

Then we install the kernel in /boot directory, as vmlinuz-"your kernel version"
#make install

To install kernel modules:
#make modules_install # installs kernel modules in /lib/modules/2.6.31


CREATING A INITRD IMAGE FILE

We need also to create a initrd image file to be able to boot in two steps:

#aptitude install initramfs-tools

#mkinitramfs -o /boot/initrd.img-2.6.31 2.6.31
We set the output image file using -o option.
We have also to indicate which kernel version are we using. e.g: 2.6.31


UPDATING GRUB:

Last step is setting correctly the bootloader so it will boot the recently compiled kernel.

We assume the GRUB bootloader.

Next lines show an example grub entry in /boot/grub/menu.lst file.

(hd0,6) and /dev/sda7 should be changed to match your system configuration.

title  Debian GNU/Linux, kernel 2.6.31
root  (hd0,6)
kernel  /boot/vmlinuz-2.6.31 root=/dev/sda7 ro
initrd  /boot/initrd.img-2.6.31



INSTALL GRUB IN YOUR DRIVE

NOTE: Be careful or you will not be able to boot your system.

/dev/sda is the hard disk where boot sector is installed, change it to match your system.
#grub-install /dev/sda


REFERENCE:

http://www.howtoforge.com/howto_linux_kernel_2.6_compile_debian

0 comentarios: