1. Maybe you do not need the full kernel source

If you need to compile a kernel driver module, the chances are you do not really need the full kernel source tree. You might just need the kernel-devel package. (If, however, you are certain that the full source tree is required, please follow the instructions in Section 2.)

In CentOS-5, there are three kernel-devel packages available:

  • kernel-devel (both 32- & 64-bit architectures)
  • kernel-xen-devel (both 32- & 64-bit architectures)
  • kernel-PAE-devel (32-bit architecture only)

In CentOS-4, there are four kernel-devel packages available:

  • kernel-devel (both 32- & 64-bit architectures)
  • kernel-smp-devel (both 32- & 64-bit architectures)
  • kernel-xenU-devel (both 32- & 64-bit architectures)
  • kernel-hugemem-devel (32-bit architecture only)
  • kernel-largesmp-devel (64-bit architecture only)

If you are running the standard kernel (for example), you can install the kernel-devel package by:

[user@host]# yum install kernel-devel

You can use this command to determine the version of your running kernel:

[user@host]# uname -r

The result will look similar to this:

2.6.18-92.1.6.el5xen

In this case, the xen kernel is installed and the way to install this specific kernel-devel package is:

[user@host]# yum install kernel-xen-devel

For more specific information about the available kernels please see the Release Notes:

If your kernel is not listed by yum because it is in an older tree, you can download it manually from the CentOS Vault. Pick the version of CentOS you are interested in and then, for the <arch>, look in either the os/<arch>/CentOS/RPMS/ or the updates/<arch>/RPMS/ directories for the kernel[-optional]-devel-<version>.<arch>.rpm Once you have the proper kernel[-optional]-devel-<version>.<arch>.rpm installed, try to compile your module. It should work this way. If it does not, please provide feedback to the module’s developer as this is the way all new kernel modules should be designed to be built.

2. If you really need the full kernel source

Ok, if you really must have the kernel source tree, for whatever reason, it is obtainable.

2.1. CentOS 4 and 5

As an ordinary user, not root, create a directory tree based on ~/rpmbuild:

[user@host]$ cd
[user@host]$ mkdir -p rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
[user@host]$ echo "%_topdir %(echo $HOME)/rpmbuild" > .rpmmacros
[user@host]$ echo -e "%_without_kabichk 1\n%_without_debuginfo 1\n%_without_debug 1" >> .rpmmacros

<!> You are strongly advised not to build packages as root. (See also Building Source RPM as non-root under CentOS.)

Install the rpm-build package:

[user@host]# yum install rpm-build

Find the kernel source rpm in:

(Pick either <version>/updates/SRPMS or <version>/os/SRPMS)

Once you have located the source file, you can install it by running, for example:

# rpm -i http://mirror.centos.org/centos/5/updates/SRPMS/kernel-2.6.18-92.1.6.el5.src.rpm 2> /dev/null
(for CentOS 5)

- or -

# rpm -i http://mirror.centos.org/centos/4/updates/SRPMS/kernel-2.6.9-67.0.15.EL.src.rpm 2> /dev/null
(for CentOS 4)

Now that the source rpm is installed, unpack and prepare the source files:

[user@host]$ cd ~/rpmbuild/SPECS
[user@host SPECS]$ rpmbuild -bp --target=`uname -m` kernel-2.6.spec 2> prep-err.log | tee prep-out.log

The value of unameĀ -m sets –target to the architecture of your current kernel. This is generally accepted and most people will have either i686 or x86_64.

The kernel source tree will now be found in the directory ~/rpmbuild/BUILD/.

2.2. CentOS 3

You can install the kernel source by:

[user@host]# yum install kernel-source

If your kernel is not listed by yum because it is in an older tree, you can download it manually from the CentOS Vault. Pick the version of CentOS you are interested in and then, for the <arch>, look in either the os/<arch>/CentOS/RPMS/ or the updates/<arch>/RPMS/ directories for the kernel-source-<version>.<arch>.rpm

You would then install it by:

[user@host]# rpm -Uvh kernel-source-<version>.<arch>.rpm
Share