Installing kernel source on CentOS/RedHat
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:
[root@host]# yum install kernel-devel
You can use this command to determine the version of your running kernel:
The result will look similar to this:
In this case, the xen kernel is installed and the way to install this specific kernel-devel package is:
[root@host]# yum install kernel-xen-devel
For more specific information about the available kernels please see the Release Notes:
*CentOS-5 i386 kernels
* CentOS-5 x86_64 kernels
* CentOS-4 (search for the heading kernel in the section Package-Specific Notes, sub-section Core, for more details.)
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[-type]-devel-version.arch.rpm
Once you have the proper kernel[-type]-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
If you really must have the kernel source tree, for whatever reason, it is obtainable.
As root, install the packages rpm-build, redhat-rpm-config and unifdef:
[root@host]# yum install rpm-build redhat-rpm-config unifdef
* The latter package is only required for 64-bit systems.
As an ordinary user, not root, create a directory tree based on ~/rpmbuild:
[user@host]$ cd
[user@host]$ mkdir -p rpmbuild/
[user@host]$ echo ‘%_topdir %(echo $HOME)/rpmbuild’ > .rpmmacros
* You are strongly advised against package building as root.
Find the kernel source rpm in:
* http://mirror.centos.org/centos/5/updates/SRPMS/(Current Updates/SRPMS)
* http://mirror.centos.org/centos/5/os/SRPMS/(Current OS/SRPMS)
(Replace the “5” with a “4” for CentOS-4 kernels)
* http://vault.centos.org/ (CentOS Vault)
(Pick either (version)/updates/SRPMS or (version)/os/SRPMS)
Once you have located the source file, you can install it by running, for example:
[user@host]$ rpm -ivh http://mirror.centos.org/centos/5/updates/SRPMS/kernel-2.6.18-92.1.18.el5.src.rpm 2> /dev/null (for CentOS 5)
[user@host]$ rpm -ivh http://mirror.centos.org/centos/4/updates/SRPMS/kernel-2.6.9-78.0.8.EL.src.rpm 2> /dev/null
note: Make sure you use -i instead of -U so that you don’t upgrade already installed source three
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` (note: back ticks (grave accents) not single quotation marks (apostrophies)) 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/.
Installing the Kernel Source Code
When the kernel source code is not present on the system, you need to install it.
fedora Before you start, install rpmbuild. You will need this program to unpack and apply patches to the source files. The rpmbuild utility is part of the rpmdevtools package. Give the following command to install this package:
# yum install rpmdevtools rhel Before you start, install some development tools including the C compiler (gcc): Use pirut (page 505) to install Development: Development Tools. You also need to install two packages:
# yum install redhat-rpm-config rpm-build fedora 1. To download the source code for a Fedora kernel, point a browser at mirrors.fedoraproject.org. From the Mirror List Filter in the upper-right corner of the Web page, click the version number of the release of Fedora you want to download (12 in the case of this example). The list is ordered by the country codes that appear in the left column. Find a site near you and click http or ftp adjacent to the words Fedora Linux. Next click releases followed by the version number of Fedora (12) and then fedora, source, and SRPMS.
rhel 1. To download the source code for a Red Hat Enterprise Linux kernel, point a browser or FTP client at ftp://ftp.redhat.com/pub/redhat/linux/enterprise and select the version of RHEL you want, following the links until you get to the SRPMS directory. For the RHEL 5 server kernel, go to
rhel+fedora From the SRPMS page, click and download the rpm file for the kernel source code. It will have a name similar to kernel-2.6.31.5-127.fc12.src.rpm (fedora) or kernel-2.6.18-164.2.1.el5.src.rpm (rhel). The src indicates the package contains source files. From Firefox, select Save file, which downloads the file to -/Downloads (fedora) or -/Desktop (rhel).
Alternatively, you can use yumdownloader to download the kernel for the local system. See page 505 for instructions.
2. Working with root privileges, use rpm to install the package you just downloaded. You need either to cd to the directory that holds the rpm file or to specify the pathname of the rpm file in the following command. You can ignore the warnings about mockbuild.
3. The preceding command installs the kernel specification file. This file holds the instructions that rpmbuild uses to unpack the kernel source files and apply patches to those files. Change directories to the one shown in the following example and run rpmbuild:
# rpmbuild -bp —target $(arch) kernel.spec
# rpmbuild -bp —target $(arch) kerne1-2.6.spec
This command takes a few minutes to run and generates a lot of output. If rpmbuild lists missing dependencies, install those packages and give the preceding command again.
4. Traditionally the source for the kernel that the system is running is kept in /usr/src/linux. The following commands move the source to the /usr/src directory and create a symbolic link to linux there. This example shows the name of the kernel directory as linux-2.6.31.i686. The name on the system you are working on will be different. Under Fedora the BUILD directory is in /root/rpmbuild; under RHEL it is in /usr/src/redhat.
1inux-2.6.31.i686 vanilla-2.6.31 vani11a-2.6.31-rc5-git2
# 1n -s /usr/src/1inux-2.6.31.i686 /usr/src/1inux
After you give these commands, the patched kernel source is located in /usr/src/linux. The rest of this chapter assumes that the kernel source is in this location.
Now the working directory is /usr/src/linux tip All commands in this section on building a kernel are given relative to the top-level directory that holds the kernel source. Traditionally this directory is /usr/src/linux. Make sure that this directory is your working directory before proceeding. If necessary, link the directory holding the kernel source in /usr/src to /usr/src/linux as explained in step 4.
Related Posts
Installing kernel source on CentOS/RedHat
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:
[root@host]# yum install kernel-devel
You can use this command to determine the version of your running kernel:
The result will look similar to this:
In this case, the xen kernel is installed and the way to install this specific kernel-devel package is:
[root@host]# yum install kernel-xen-devel
For more specific information about the available kernels please see the Release Notes:
*CentOS-5 i386 kernels
* CentOS-5 x86_64 kernels
* CentOS-4 (search for the heading kernel in the section Package-Specific Notes, sub-section Core, for more details.)
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[-type]-devel-version.arch.rpm
Once you have the proper kernel[-type]-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
If you really must have the kernel source tree, for whatever reason, it is obtainable.
As root, install the packages rpm-build, redhat-rpm-config and unifdef:
[root@host]# yum install rpm-build redhat-rpm-config unifdef
* The latter package is only required for 64-bit systems.
As an ordinary user, not root, create a directory tree based on ~/rpmbuild:
[user@host]$ cd
[user@host]$ mkdir -p rpmbuild/
[user@host]$ echo ‘%_topdir %(echo $HOME)/rpmbuild’ > .rpmmacros
* You are strongly advised against package building as root.
Find the kernel source rpm in:
* http://mirror.centos.org/centos/5/updates/SRPMS/(Current Updates/SRPMS)
* http://mirror.centos.org/centos/5/os/SRPMS/(Current OS/SRPMS)
(Replace the “5” with a “4” for CentOS-4 kernels)
* http://vault.centos.org/ (CentOS Vault)
(Pick either (version)/updates/SRPMS or (version)/os/SRPMS)
Once you have located the source file, you can install it by running, for example:
[user@host]$ rpm -ivh http://mirror.centos.org/centos/5/updates/SRPMS/kernel-2.6.18-92.1.18.el5.src.rpm 2> /dev/null (for CentOS 5)
[user@host]$ rpm -ivh http://mirror.centos.org/centos/4/updates/SRPMS/kernel-2.6.9-78.0.8.EL.src.rpm 2> /dev/null
note: Make sure you use -i instead of -U so that you don’t upgrade already installed source three
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` (note: back ticks (grave accents) not single quotation marks (apostrophies)) 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/.