Compile a new kernel - CentOS way (make rpm)

Linux specific questions/information are gathered here. The main thrust of topics are applied to Centos/RedHat(RH)/Debian/Ubuntu/Gentoo distributives

Compile a new kernel - CentOS way (make rpm)

Postby lik » Sun Jul 12, 2009 6:18 am

This article is about compiling a kernel on CentOS systems. It describes how to build a custom kernel using the latest unmodified kernel sources from http://www.kernel.org/ (http://en.wikipedia.org/wiki/Vanilla_kernel#Versions) so that you are independent from the kernels supplied by your distribution.

One of the multiple ways of building a kernel for CentOS systems to get you a kernel rpm package that can be installed or shared with others.

Building a Kernel rpm Package

1. Download The Kernel Sources
Code: Select all
cd /usr/src
wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.37.tar.bz2

Then we unpack the kernel sources and create a symlink linux to the kernel sources directory:
Code: Select all
tar xjf linux-2.6.37.tar.bz2
ln -s linux-2.6.37 linux
cd /usr/src/linux


2. Configure The Kernel
Copy configuration of your current working kernel as a basis for your new kernel to /usr/src/linux:
Code: Select all
make clean && make mrproper
cp /boot/config-`uname -r` ./.config

Then run:
Code: Select all
make menuconfig

Go to Load an Alternate Configuration File and choose .config (which contains the configuration of your current working kernel) as the configuration file. Then browse through the kernel configuration menu and make your choices. Save it on exit.

Make sure you specify a kernel version identification string under "General Setup ---> () Local version - append to kernel release" (in order for this setting in force, do not forget to select also "Automatically append version information to the version string"). Nevertheless, you can leave the string empty, but be noticed:

After you have installed kernel-2.6.37.x86_64.rpm and decide to compile another 2.6.37 kernel rpm package, it is important to use a different version string, e.g. -default1, -default2, etc., because otherwise you can't install your new kernel because rpm complains that kernel-2.6.37.x86_64.rpm is already installed!

3. Build The Kernel
Some pre-requirements that should be met. As root, install the packages rpm-build, redhat-rpm-config , ncurses-devel and unifdef:
Code: Select all
yum install rpm-build redhat-rpm-config ncurses-devel unifdef

<!>
The latter package is only required for 64-bit systems.
To build the kernel, simply execute this command:
Code: Select all
make rpm


4. Install The New Kernel
After the successful kernel build, a src.rpm and an rpm package have been created. The src.rpm package can be found in the /usr/src/redhat/SRPMS/ directory, you can find out about its name by running
Code: Select all
ls -la /usr/src/redhat/SRPMS/

The rpm package can be found in /usr/src/redhat/RPMS/`arch`/, depending on your architecture:
Code: Select all
ls -la /usr/src/redhat/RPMS/`arch`/

Now we can install our kernel rpm package like this:
Code: Select all
cd /usr/src/redhat/RPMS/`arch`/
rpm -ivh kernel-2.6.37.x86_64.rpm

Following command will provide you with the real ARCH value on x86 systems:
Code: Select all
rpm -q --queryformat '%{ARCH}' bash

UNDER NO CIRCUMSTANCES use a rpm -Uvh command to install your kernel as this will update (overwrite) the currently installed version. Hence if you have a problem with your custom kernel, you will not be able to revert to the previous, working, version.

Possible issues
ERROR :
error: Failed dependencies:
kernel >= 2.6.10 conflicts with lksctp-tools-1.0.2-6.4E.1.i386

Most time it`s safety to ignore it (use --nodeps key) .

Next we create a ramdisk for our new kernel, because otherwise the system will most likely not boot our new kernel:
Code: Select all
mkinitrd /boot/initrd-2.6.37.x86_64.img 2.6.37


Possible issues.

By default the version of mkinitrd provided in RedHat Linux attempts to include the dm-mem-cache module in the generated initial ramdisk images. Therefore when creating custom initial ramdisk images for kernels that do not provide the dm-mem-cache module, the mkinitrd command will fail with a message similar to:
Code: Select all
No module dm-mem-cache found for kernel 2.6.37, aborting.

Solution:

a) To build an initrd image, for a kernel that does not provide the dm-mem-cache module, it is possible to prevent the inclusion of the module by adding the --without-dmraid argument to the mkinitrd command line.

b) It is also possible to create a permanent exception for the inclusion of the dm-mem-cache module. This can be done by creating the file /etc/sysconfig/mkinitrd/noraid as follows:
Code: Select all
echo "DMRAID=no" > /etc/sysconfig/mkinitrd/noraid
chmod 755 /etc/sysconfig/mkinitrd/noraid


ERROR :
No module {sata_sil,ahci,ehci_hcd,ohci_hcd} found for kernel 2.6.37, aborting.


First, we need to ensure, that this(these) module(s) support is actually enabled in the kernel configuration file:
Code: Select all
grep -i sata /usr/src/linux/.config

Having this done, we can bypass/ignore warning:
Code: Select all
mkinitrd --builtin=sata_sil /boot/initrd-2.6.37.x86_64.img 2.6.37

...till successful result ...
Code: Select all
mkinitrd --builtin=sata_sil --builtin=sata_nv /boot/initrd-2.6.37.x86_64.img 2.6.37


5. Configure The GRUB Boot Loader with fallback support

Code: Select all
# cat /boot/grub/grub.conf
# 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/sda7
#          initrd /initrd-version.img
#boot=/dev/sda
default saved   #needed for fallback support (comment it after successful boot)
##default 0     #typical default boot entry (un-comment it after successful boot)
timeout=10
fallback 1        #fallback sequence (comment it after successful boot)
#splashimage=(hd0,0)/grub/splash.xpm.gz   #need to be commented for fallback support
#hiddenmenu

title CentOS5.5 2.6.37.x86_64
        root (hd0,0)
        kernel /vmlinuz-2.6.37 ro root=LABEL=/
        initrd /initrd-2.6.37.x86_64.img
        savedefault fallback       #needed for fallback support

title CentOS (2.6.18-194.26.1.el5)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-194.26.1.el5 ro root=LABEL=/
        initrd /initrd-2.6.18-194.26.1.el5.img
        savedefault          #needed for fallback support (latest record should end up only with "savedefault")


Reboot the system
Code: Select all
reboot

And check the current running kernel
Code: Select all
uname -a


http://www.howtoforge.com/kernel_compilation_centos
http://wiki.centos.org/HowTos/
http://www.kroah.com/lkn/
lik
Founder
Founder
 
Posts: 497
Joined: Wed Dec 15, 2010 3:21 am

Return to Linux specific

 


  • Related topics
    Replies
    Views
    Last post