How-to build a new kernel – linux

Building akernel is not as hard as many may think it is actually pretty easy and stright forward of course if you know what you are doing, but after doing this a few times(even if you don’t get it right from the first time) you will see that everything looks easy.

In order to be able to build a new kernel you may need some extra packages like make, kernel-headers(if rebuilding) or C++ so if you get any errors that a package is needed or not installed just install it even by using apt-get(debian) or yum(redhat based) or by simply downloading and installing the package(harder way) .

If you want to build a new kernel(like build the latest kernel) you will need the new kernel source and this can be downloaded from kernel.org as the latest patches(if any or needed).

Lets say that we want to have the latest stable kernel(at this time 2.6.27.4) installed on the server:

1. Download the kernel to the server:

cd /usr/src
wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.27.4.tar.gz

2. Unpack the archive and create a symlink:

tar -zxvf linux-2.6.27.4.tar.gz
cd linux-2.6.27.4
ln -s /usr/src/linux-2.6.27.4 /usr/src/linux

3. You may need to apply some patches:

Download the patch(es) from http://www.kernel.org/pub/linux/kernel/v2.6/ :

cd /usr/src
wget http://www.kernel.org/pub/linux/kernel/v2.6/patch-2.6.27.4.bz2

Apply the patch:

cd /usr/src/linux
bzip2 -dc /usr/src/patch-2.6.27.4.bz2 | patch -p1 --dry-run
bzip2 -dc /usr/src/patch-2.6.27.4.bz2 | patch -p1

First command will test the patch against the kernel files, don’t worry if you get a message saying that the patch is already existing as that just means that this is already applied and you did not need to do this step so just jump over the patching step.

4. Configure the new kernel and make modifications:

To make everything more simple we will use the already existing and working configuration of the kernel we are currently running and make modifications on it if needed, like adding extra hardware support or modules(for example adding Tarpit support).

cd /usr/src/linux
make clean && make mrproper
cp /boot/config-`uname -r` ./.config
make menuconfig

Running make menuconfig will open a new window where you will be able to see all available modules and make modifications if needed.
A good idea is to add another name to the kernel like “custom” and also remember to save the config at the end.

5. Build the new kernel:

5.1 If you want to build the kernel and also install it on the server follow this path:

make bzImage
make modules && make modules_install
make install
vi /boot/grub/grub.conf
default=1 -> default=0

5.2 If you just want to create an rpm and to install it later:

make rpm

5.2.1 You now have to install the kernel:

cd /usr/src/redhat/RPMS/i386/
rpm -ivh --nodeps kernel-2.6.27.4custom.i386.rpm
mkinitrd /boot/initrd-2.6.27.4-custom.img 2.6.27.4-custom
vi /boot/grub/grub.conf
default=1 -> default=0

6. Reboot

If you want to make sure that your server will boot up even if the kernel crashes try and do the following instead of editing grub.conf (last 2 commands on 5.1 and 5.2.1):

grub
savedefault --default=0 --once
quit

Now reboot the server shutdown -r now and make sure everything is fine.
To check which kernel was booted run uname -r on the server.

If everything was done right you should have the latest available kernel installed on the server.

Share/Bookmark