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
- CentOS / RedHat 5.3 minimal installation (Portuguese only)
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[singlepic id=30 w=320 h=240 float=]
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.
[singlepic id=34 w=320 h=240 float=]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.
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

17 comments
Join the conversationRichard Holloway - 03/03/2010
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 - 03/03/2010
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 - 03/03/2010
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 - 09/03/2010
Thanks for sharing your solution!
vishal - 23/11/2011
Thanks a lot, it took me almost 1 day to compile the custom kernel.
Rocky - 23/07/2010
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
Pingback: [HELP] depmod and mkinitrd command not found
Esonz - 21/08/2010
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 - 21/08/2010
To install mkinitrd use the command:
yum install mkinitrd
To install depmod:
yum install module-init-tools
And you are good to go :)
Esons - 22/08/2010
bro it indeed installed depmod but still it give me the same error :(
http://i913.photobucket.com/albums/ac340/Silver_King62/depmod.jpg
Esons - 22/08/2010
same also to mkinitrd
http://i913.photobucket.com/albums/ac340/Silver_King62/mkinitrd.jpg
:(
Daniel Kühl Lima - 22/08/2010
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 - 22/08/2010
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!
Pingback: Linux Microcore Kernel Compilation For 802.1Q Support « Brezular's Technical Blog
habib - 21/10/2014
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
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.
Pingback: Building the Linux Microcore Kernel for 802.1Q Support | Brezular's Blog
Related Posts
Podman + MariaDB: Resolvendo “Error establishing a database connection” após reiniciar o servidor
Daniel K Lima 30/11/2025Converter espaços/tabs em um único caracter (um espaço ou vírgula etc) com sed
Daniel K Lima 02/06/2016Conveter novas (\n) linhas em vírgula ou espaço usando sed
Daniel K Lima 06/04/2016SARG 2.3.1 erro ao compilar no Fedora 14
Daniel K Lima 11/03/2011rootsh: Keylogger para CLI do Linux
Daniel K Lima 20/04/2010CLI: Converter um PDF para uma imagem
Daniel K Lima 06/04/2010