Jump to Navigation

How to compile a custom kernel for Fedora/Redhat/Centos

As a rule I prefer not to compile custom kernels on rpm and apt based distros unless absolutely necessary. If I know I am going to have to customise the kernel before hand, I prefer to use a source based distro such as Gentoo. That way there is nothing that gets in your way of the simple, make menuconfig=>make install=>make modules=>make modules_install=>mkinitrd (if necessary) recipe for success. Most times for debian and redhat based distros you can get by with just installing the kernel headers and then building your special module form there. 

Recompiling Fedora/Redhat/Centos kernels

However occasionally you need to recompile the kernel on a non-source based distro. Debian based systems are pretty straight forward but on a rpm based system the steps are not obvious if you are not in the habit of making your own rpms. Below are the steps I take to re-compile a fedora/redhat systems custom kernel.

Creating your own rpm kernel package

The exact steps vary depending on which version of the distro you are compiling on but in general the steps are as detailed below. The basic target is to create an rpm package and then use that to install the custom kernel (Disclaimer: I am no rpm packaging expert so let me know if you know better).

1) Create the rpm buildroot directory structure

First you need to setup the directory structure that the rpmbuild tools expect when they are invoked to create an rpm package. To do this you need to create the a directory structure like:

mkdir -r ~/rpmbuild/{SPEC,SOURCES,RPMS,SRPMS,BUILD}

This is the same strucutre as under /usr/src/redhat. After that you need to create a .rpmmacros file. This file contains some settings for the rpm build tool, like where to find your buildroot directory.. A standard rmpmacros file looks like:

%_topdir %(echo $HOME)/rpmbuild #path to your custom build dir

Tools exist to make this whole process less painful or so I have read. Fedora has the "fedora-rpmdevtool" package but I haven't used these before.

yum install rmpdevtools

You supposedly can create the default directory structure by running fedora-rpmbuildtree, although I can never find this command.

2) Obtain and install kernel source

Now you need to get the kernel sources for the current kernel you are running. Your first instinct may be to get the latest kernel from kernel.org but this would be a mistake. Unless necessary rather use the kernel sources for your distro.  you will be making your life easier as you will have the custom applied patches and other optimisations that the rest of the system expects to find.

To find the version of the source you need do a 'uname -r'. You will have to download the sources rpm without the use of yum as they are not made available through those repositories. For example:

centos: ftp://centos.mirror.ac.za/5/updates/SRPMS/
fedora: ftp://fedora.mirror.ac.za/linux/releases/9/Fedora/source/SRPMS/

Download the source rpm that matches your current kernel. Once you have it you need to install it with 

rpm -i <kernel-src>.rpm

You will get warning that a user does not exist but you can ignore these.

3) Edited the spec file for custom patches (optional)

The next step depends on what you want to do. If you need to apply a patch download and then apply the patch by editing the kernel-<version>.spec file under the ~/rpmbuild/SPECS directory. The format of the file is pretty straight forward  but I would not recommend hacking it too much. If you don't need to apply a patch but just change some kernel compilation options then you can skip to step 4. The spec file basically tells the rpmbuild tool how to patch the kernel source files. 

4) Build the pathced kernel source

Now that you have the kernel source with the spec file and the patches that need to be applied to make the kernel a fedora/centos/redhat kernel you can run the command.

rpmbuild -bp --target=<i386/i686 etc>  /path/to/rpmbuild/SPECS/kernel-<version>.spec.

-bp means build the patched kernel source files. They will be placed under rpmbuild/BUILD directory.

5) Run make menuconfig

Now you can do your "make menuconfig". Change to the build dir and run the command. To use the default .config file of your system copy the relevant file form /booot/config-kernel-version to BUILD/.config.

6) Compile the kernel and create rpm package

Once you saved your config file you then have to compile the source and create the package. This is done by running 

make rpm or  rpmbuild -bb --target=`uname -m` kernel-<version>.spec

This will compile the source and create an rpm package of your custom kernel under RPMS., notice the use of the "bb" option in the second approach.If you try and compile your kernel with "make install" you will end up with the build failing.

7) Install your custom kernel

Once created install the kernel by running rpm -i <your custom kernel>.rpm. It will be found under the rpmbuild/RPMS direcotry.

8) Create an initrd image

 You will also have to create an initrd image of your kernel.

mkinitrd /boot/initrd-<version>.img <version>

9) Edit grub's menu.lst file

Finally you will need to edit /boot/grub/menu.lst to add your custom kernel to the boot up options and make it the default.

Boot into your new kernel and enjoy. You may have to run "depmod" to link up the new modules and you should copy the patched sources from BUILD to /usr/src/ and created a sim link for /usr/src/linux. Most other apps you build will look there for the source files.

Open Source: 


by Dr. Radut.