HOW-TO compile the Linux kernel

There are common three reasons for a recompile a kernel.

1. You have some new hardware that your current kernel of linux distributuion could not detect it.
(You want to add kernel module of the new hardware)
2. You may have come across some kind of bug which is fixed in a revision of the operating system.
3. You may have some new software which requires a newer version of the operating system.

*****************
1. We can download the new kernel source and new patches from the Linux kernel archive web site. Web : http://www.kernel.org

2. Check the current kernel packages:

# rpm -qa | grep kernel*
Or
# uname -a

3. Preparing for Kernel update
Before build new kernel, you need to install Qt, Qt-devel and Qt-config which are used "make xconfig". If you do not need to use xconfig, do not to do this step.
# rpm -ivh qt*.rpm
or
(If you use Redhat distribution, you can use up2date command.)
# up2date qt
# up2date qt-config
# up2date qt-devel

To check installed QT packages
# rpm -qa | grep qt
qt-devel-xxxx
qt-config-xxxx
qt-xxxx

****************
Compile the Linux kernel (from RPM package)

4. Obtain the kernel source "kernel-.src.rpm" file from one of the following sources:
- The SRPMS directory on the appropriate SRPMS CD iso image.
- The FTP site where you got the kernel package.
- By running the command "up2date" (if you use redhat linux)

# up2date --get-source kernel

5. Install kernel source (kernel-.src.rpm) using the command "rpm".
# rpm -Uvh kernel-.src.rpm

This writes the RPM contents into "/usr/src/redhat/SOURCES" and "/usr/src/redhat/SPECS".

6. Prepare the kernel sources using the command "rpmbuild".
# cd /usr/src/redhat/SPECS
# rpmbuild -bp --target $(arch) kernel-2.6.spec
Here :
--target $(arch) - current system architecture (exm: i386, etc.)

The kernel source tree will be located in the "/usr/src/redhat/BUILD/kernel-" directory.
To move the resulting "linux-" directory to the "/usr/src" tree.
# cd /usr/src/redhat/BUILD/
# mv linux- /usr/src/
# cd /usr/src
# ln -s ./linux- linux
# cd /usr/src/linux

7. The configurations for the specific kernels find from the "configs/" directory.
For example, the i386 SMP configuration file is named "configs/kernel--i386-smp.config".
Issue the following command to place the desired configuration file in the proper place for building:

# cp configs/xxxxx .config
We can also find the ".config" file that matches current kernel configuration in the "/lib/modules//build/.config" file.

8. Every kernel gets a name based on its version number which displayed the output of command "uname ?r".
The kernel name is defined by the first four lines of the kernel Makefile.
You have to edit the kernel Makefile.
Example, If command "uname -r" returns the string "2.6.9-5", we changed the EXTRAVERSION definition from this:
EXTRAVERSION = -pre
to this:
EXTRAVERSION = -5_NEW

9. Change old kernel configuration (.config) to new configuration.
# make oldconfig
"oldconfig" which is read the defaults from an existing .config and rewrite necessary links and files.
Note that oldconfig will only work within the same major version of the kernel.
# make xconfig

"xconfig" is an X Window based frontend. It requires the Tcl/Tk, QT development tools and X libraries to work.

10. To configure and build the kernel
# make all

11. Build modules
# make modules_install
# make install

Note: The command "make install", it will create following files in the "/boot" folder and configure the boot loader configuration file.
"vmlinuz-",
"initrd-.img",
"System.map-"

****************
Compile the Linux kernel (from TAR package)

4. Obtain the kernel source "kernel-.tar.gz" file from Linux Kernel archives or other Kernel mirror site.
# wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.13-5.tar.gz

5. Unpack the kernel source file (kernel-xxx.tar.gz or kernel-xxx.tar.bz2) using the command "tar".
# tar -xvfz kernel-xxx.tar.gz
# tar -xvfj kernel-xxx.tar.bz2

6. To move the result "linux-" directory to the "/usr/src" tree.
# mv linux- /usr/src
# cd /usr/src/
# ln -s ./linux- linux
# cd /usr/src/linux

7. The configurations for the specific kernels find from the "configs/" directory.
For example, the i386 SMP configuration file is named "configs/kernel--i386-smp.config".
Issue the following command to place the desired configuration file in the proper place for building:

# cp configs/xxx .config
We can also find the ".config" file that matches current kernel configuration in the "/lib/modules//build/.config" file.

8. Every kernel gets a name based on its version number which displayed the output of command "uname ?r".
The kernel name is defined by the first four lines of the kernel Makefile.
You have to edit the kernel Makefile.
Example, If command "uname -r" returns the string "2.6.9-5", we changed the EXTRAVERSION definition from this:
EXTRAVERSION = -pre
to this:
EXTRAVERSION = -5_NEW

9. Change old kernel configuration (.config) to new configuration.
# make oldconfig

"oldconfig" which is read the defaults from an existing .config and rewrite necessary links and files.
Note that oldconfig will only work within the same major version of the kernel.
# make xconfig

"xconfig" is an X Window based frontend. It requires the Tcl/Tk, QT development tools and X libraries to work.

10. To configure and build the kernel
# make all

11. Build modules
# make modules_install
# make install

Note: The command "make install", it will create following files in the "/boot" folder and configure the boot loader configuration file.
"vmlinuz-",
"initrd-.img",
"System.map-"

Enjoy youtube video : "How to compile linux kernel"


Note: Using this manual, I compiled following Linux distribution/Kernel :
- Redhat 9 , kernel 2.4.20 -> 2.4.20
- RHEL 4 , kernel 2.6.9-5 -> 2.6.(10,11,12,13,14,15,16,17)
- SUSE Linux 10.0 , kernel 2.6.13 - 2.6.13
- Fedora 4/5/6 , kernel 2.6.X -> 2.6.X

No comments: