How to compile a new kernel (2.6.30.5) in Linux CentOS 5.3 / Red Hat 5.3

You purchased a new hardware but the current kernel does not support it, or need to add functionality to the kernel that does not come from the factory.

It is at those times that is necessary to compile a new kernel for the device to be recognized or a new functionality can be used by the software, for example.

I will show you how to compile a new kernel on CentOS / Red Hat Linux.

Pre-requisite

Download latest kernel do Linux

The latest version can be get straight from http://www.kernel.org/pub/linux/kernel/v2.6/

Downloading the kernel source (at this moment is 2.6.30.5)

wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.30.5.tar.bz2

After you have done it, extract to /usr/src

tar jxvf linux-2.6.30.5.tar.bz2 -C /usr/src
...
linux-2.6.30.5/virt/
linux-2.6.30.5/virt/kvm/
linux-2.6.30.5/virt/kvm/coalesced_mmio.c
linux-2.6.30.5/virt/kvm/coalesced_mmio.h
linux-2.6.30.5/virt/kvm/ioapic.c
linux-2.6.30.5/virt/kvm/ioapic.h
linux-2.6.30.5/virt/kvm/iodev.h
linux-2.6.30.5/virt/kvm/iommu.c
linux-2.6.30.5/virt/kvm/irq_comm.c
linux-2.6.30.5/virt/kvm/kvm_main.c
linux-2.6.30.5/virt/kvm/kvm_trace.c

To made things simple, go to /usr/src and create a symlink to source code called linux, do this:

cd /usr/src
ln -sf linux-2.6.30.5 linux

Result:

ls -al
drwxr-xr-x  3 root root 4.0K Sep  2 06:18 .
drwxr-xr-x 13 root root 4.0K Aug 31 14:25 ..
lrwxrwxrwx  1 root root   14 Sep  2 06:18 linux -> linux-2.6.30.5
drwxrwxr-x 22 root root 4.0K Aug 16 18:19 linux-2.6.30.5

Preparing the environment to compile Kernel

To compile the Kernel on Linux, the OS need to be prepared, with gcc, ncurses etc.

If your CentOS or Red Hat installation is the minimal, the devel packages weren’t installed, it is needed to do it now, it’s simple with Yum that will resolve all dependencies.

yum install gcc make bison ncurses-devel rpm-build
...
Installed: bison.i386 0:2.3-2.1 gcc.i386 0:4.1.2-44.el5 ncurses-devel.i386 0:5.5-24.20060715
Dependency Installed: cpp.i386 0:4.1.2-44.el5 glibc-devel.i386 0:2.5-34.el5_3.1 glibc-headers.i386 0:2.5-34.el5_3.1 kernel-headers.i386 0:2.6.18-128.7.1.el5 libgomp.i386 0:4.3.2-7.el5
Updated: glibc.i686 0:2.5-34.el5_3.1 glibc-common.i386 0:2.5-34.el5_3.1 nscd.i386 0:2.5-34.el5_3.1

Compiling the kernel to CentOS or Red Hat

Before starting the compilation, it is interesting to clean the garbage that should left behind.

make clean

A good idea is to take the parameters of configuration that comes with the factory one, this file can be found at /boot dir, let’s copy it to the root of our kernel source and execute make menuconfig.

cd /usr/src/linux
cp /boot/config-`uname -r` .config

Be sure to stay on /usr/src/linux.
Accessing the configuration menu is the easy way to configure it.

make menuconfig

As this is a custom compilation, I suggest you to add a custom tag that will identify the new kernel, to accomplish this, do that:

  • On initial screen, go to General setup  —>
  • Select Local version – append to kernel release

With this done, the resulting kernel.rpm will have the tag identifying it.

It is time to customize the kernel configuration to fit with your hardware.

After this task, it’s time to quit from menuconfig and start the compilation.

While you get out the configuration, you will be asked to save the changes. Answer Yes.

The most awaited time… compiling!

Run make rpm

make rpm
...
  HOSTLD  scripts/kconfig/conf
scripts/kconfig/conf -s arch/x86/Kconfig
make clean
set -e; cd ..; ln -sf /usr/src/linux-2.6.30.5 kernel-2.6.30.5fogonacaixadagua
/bin/sh /usr/src/linux-2.6.30.5/scripts/setlocalversion > /usr/src/linux-2.6.30.5/.scmversion
set -e; cd ..; tar -cz --exclude SCCS --exclude BitKeeper --exclude .svn --exclude CVS --exclude .pc --exclude .hg --exclude .git -f kernel-2.6.30.5fogonacaixadagua.tar.gz kernel-2.6.30.5fogonacaixadagua/.
...

The compilation will start, it will take a looong time.

At the end of process, without errors:

...
Wrote: /usr/src/redhat/SRPMS/kernel-2.6.30.5fogonacaixadagua-1.src.rpm
Wrote: /usr/src/redhat/RPMS/i386/kernel-2.6.30.5fogonacaixadagua-1.i386.rpm
...

The kernel was compiled and a .rpm was generated to be installed.

To confirm that the new kernel really exists in a rpm format, run:

ls -alh /usr/src/redhat/RPMS/i386/
total 164M
drwxr-xr-x 2 root root 4.0K Sep  2 11:45 .
drwxr-xr-x 9 root root 4.0K Sep  2 10:39 ..
-rw-r--r-- 1 root root 164M Sep  2 11:45 kernel-2.6.30.5fogonacaixadagua-1.i386.rpm

RPM of the new kernel (32 bits)

I’m putting the compiled kernel and packaged here, feel free to download it.

Installing the new kernel

How was generated a RPM of kernel, the installation becomes quite simple

rpm -ivh /usr/src/redhat/RPMS/i386/kernel-2.6.30.5fogonacaixadagua-1.i386.rpm
Preparing...                ########################################### [100%]
   1:kernel                 ########################################### [100%]

Confirming that the new kernel was installed.

ls /boot
config-2.6.18-128.el5                 symvers-2.6.18-128.el5.gz
config-2.6.30.5-fogonacaixadagua      System.map-2.6.18-128.el5
grub                                  System.map-2.6.30.5-fogonacaixadagua
initrd-2.6.18-128.el5.img             vmlinux-2.6.30.5-fogonacaixadagua.bz2
initrd-2.6.30.5-fogonacaixadagua.img  vmlinuz-2.6.18-128.el5
lost+found                            vmlinuz-2.6.30.5-fogonacaixadagua

Creating the initrd for the new kernel

To create the dependencies for the modules:

depmod 2.6.30.5-fogonacaixadagua

You must create a new initrd so the OS can start and pre-load the modules necessary to your hardware.

mkinitrd -v /boot/initrd-2.6.30.5-fogonacaixadagua.img 2.6.30.5-fogonacaixadagua

In case the following error occour:

No module dm-mem-cache found for kernel 2.6.30.5-fogonacaixadagua, aborting.
To work around this, use the parameter--without-dmraid with mkinitrd
mkinitrd --without-dmraid -v /boot/initrd-2.6.30.5-fogonacaixadagua.img 2.6.30.5-fogonacaixadagua

Editing grub

Final phase, it remains only edit the grub.conf to use the new kernel.

With your favorite text editor, add the following lines:

title CentOS (2.6.30.5-fogonacaixadagua)
        root (hd0,0)
        kernel /vmlinuz-2.6.30.5-fogonacaixadagua ro root=/dev/VolGroup00/LogVol00
        initrd /initrd-2.6.30.5-fogonacaixadagua.img

The file /boot/grub/grub.conf will be like this:

# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/VolGroup00/LogVol00
#          initrd /initrd-version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.18-128.el5)
	root (hd0,0)
	kernel /vmlinuz-2.6.18-128.el5 ro root=/dev/VolGroup00/LogVol00
	initrd /initrd-2.6.18-128.el5.img
title CentOS (2.6.30.5-fogonacaixadagua)
        root (hd0,0)
        kernel /vmlinuz-2.6.30.5-fogonacaixadagua ro root=/dev/VolGroup00/LogVol00
        initrd /initrd-2.6.30.5-fogonacaixadagua.img

For the first boot, let the line default=0 to test, if the new kernel boot with no trouble it will be ok to change the line to default=1 to always boot with the new kernel.

Testing a boot with the new kernel

Reboot.

shutdown -r now
  • Press any key on boot screen to enter the selection kernel
  • Select the new kernel configured into Grub
  • At the end of the boot, it will be possible to see that the new one booted properly
Share this post

16 comments

Join the conversation
  • Richard Holloway UNITED KINGDOM - 03/03/2010 reply

    I am getting the message ‘Could not find filesystem /dev/root. ‘

    I have tried many various suggestions found online with no success, are you able to provide me with any advice on trouble shooting this?

    Daniel Kühl Lima BRAZIL - 03/03/2010 reply

    Looks like the kernel haven’t found the root partition.

    Starting looking for the root= entry into your grub.conf and make sure you have compiled the chipset driver to recognize your HD, be sure to know it and if it is SATA or SCSI or SAS to build the properly driver.

    Richard Holloway UNITED KINGDOM - 03/03/2010 reply

    I have found a solution on http://funky-dennis.livejournal.com/3290.html.

    I needed to edit .config and set CONFIG_SYSFS_DEPRECATED_V2=y

    Thank you for this excellent guide.

    Daniel Kühl Lima BRAZIL - 09/03/2010 reply

    Thanks for sharing your solution!

    vishal INDIA - 23/11/2011 reply

    Thanks a lot, it took me almost 1 day to compile the custom kernel.

  • Rocky INDIA - 23/07/2010 reply

    Hi,

    I did same process which u mentioned here.At last when i booted my machine with new kernel, i got Kernel Panic….! error.

    Regards,
    ROcky

  • Esonz PHILIPPINES - 21/08/2010 reply

    bash: depmod command not found
    bash: mkinitrd command not found

    how to solve this one? im stocked in “Creating the initrd for the new kernel”

    Daniel Kühl Lima BRAZIL - 21/08/2010 reply

    To install mkinitrd use the command:

    yum install mkinitrd

    To install depmod:

    yum install module-init-tools

    And you are good to go :)

  • Esons PHILIPPINES - 22/08/2010 reply

    bro it indeed installed depmod but still it give me the same error :(
    http://i913.photobucket.com/albums/ac340/Silver_King62/depmod.jpg

    Daniel Kühl Lima BRAZIL - 22/08/2010 reply

    It’s look like you do not have /sbin in your PATH variable.

    May you are using just ‘su’ to get root user or just haven’t PATH set.

    To use the su command and get the root ENV variables, use:
    su - root

    And even after that, if you got no luck, set the PATH variable:

    export PATH=/sbin:/bin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:$PATH

    It’s good to put it into /etc/profile file so you won’t need to set it every time.

    You can also specify the full path for mkinitrd:

    /sbin/mkinitrd -v /boot/initrd-2.6.32.5-silverking.img 2.6.32.5-silverking

    The same go to ‘depmod':

    /sbin/depmod -a

    Esons PHILIPPINES - 22/08/2010 reply

    ok depmod and mkinitrd [SOLVED]

    and hopefully this is the last and final error.
    after reboot then i chosen my 2.6.32.5-silverking
    http://i913.photobucket.com/albums/ac340/Silver_King62/saigonoerror.jpg

    Please bare with me a little longer daniel, Im really new in configuring kernels.

    thank you very much for the Great Help!

  • habib - 21/10/2014 reply

    i do not have the mkinitrd file.can any one provide the link to that file or simply put it here that i can download from.thanks a lot

    Daniel K Lima - 01/11/2014 reply

    Just issue the command:

    sudo yum provides “*/mkinitrd”

    and you see the package which provides the mkinitrd you need.

    You may find it on dracut rpm package.

Join the conversation