14 Jul 2011

LInux Kernel Configuration and Compilation

Hello Readers,

New to Linux Kernel??
In this post, we will be learning about how to compile Linux kernel, how to get the source and make the kernel and to understand entire ecosystem of Linux kernel and to understand the configuration aspect.


KERNEL COMPILATION
The Linux kernel is the core of the operating system, acting as a layer between the hardware and all other processes. The kernel provides for memory management, multi-tasking, input/output, networking, and many other functions. Since Linux is open source software, access to the source code for the Linux kernel is freely available. This means that anyone is free to customize and recompile the kernel to suit their specific needs. In fact, the kernel is designed in a modular fashion that allows users to remove parts of the kernel that are not needed for the intended purpose of the machine
When thinking of customizing and recompiling the kernel, you may have visions of sorting through thousands of lines of C code, but that is not the case. Unneeded kernel modules can be removed without editing a single line of code. But, recompiling the kernel is not something that needs to be done frequently. It is a rather complex process. A combination of failing to make proper backups of the old kernel and errors in the compilation process can result in a system that does not function correctly or that does not boot up at all!

Kernel Version Numbers
The Linux kernel version numbers consist of three numbers separated by decimals, such as 2.6.14.
·         The first number is the major version number.
·         The second number is the minor revision number.
·         The third number is the patch level version.

At any given time there is a group of kernels that are considered “stable releases” and another group that is considered “development.”

If the second number of a kernel is even, then that kernel is a stable release. For example, the 2.6.14 kernel is a stable release because the second number is even. If the second number is odd, then that kernel is a development release. For example, the 2.5.51 is a development release because the second number is odd. Once the 2.5.x branch is considered finished, and then it will become the 2.6.0 kernel. Patches will then appear for the 2.6.x branch and development work will begin on the 2.7.x branch. If the 2.7.x advancements are significant enough to be considered a major revision, the 2.3.x branch will become 3.0.0 and development work will begin on the 3.1.x branch.

When should I upgrade to new kernel:-
New kernels are released rather frequently and the difference between two consecutive patch levels is usually minimal. Updating your kernel every time a new kernel is released is usually pointless unless the new version addresses an issue that directly affects you.

In the past, the kernel was not as modular as it is today. This means that older kernels used to load many unneeded modules into memory, thus increasing system load and also increasing the chances that bugs in those unneeded modules could adversely affect the system.

Recompiling the kernel to remove the unneeded modules had noticeable benefits. Newer kernels, however, usually load modules into memory only when they are needed. Manually removing these modules has little positive effect since they are not called by the kernel anyway.

Recompiling the kernel may be necessary if new hardware is added to the system that the current kernel does not support. For example, a system with a dual processor motherboard but only one processor installed most likely has a kernel that supports only one processor. If a second processor is installed at a later date, the kernel must be recompiled to support symmetric multi-processing (SMP) in order to utilize the second processor.

The process of compiling the kernel places a heavy load on the system, especially the RAM. On a busy server, there is being a noticeable degradation of system performance. Also, after the kernel has been compiled and installed, the system must be rebooted so that the new kernel can be used. Depending on the role of the machine, the downtime involved with rebooting the server can be costly. Consideration should be given to the items listed above before recompiling the kernel.


If you wish to upgrade to a newer kernel, you can patch your current kernel instead of downloading an entire new kernel. By patching your existing kernel, you retain your settings from previous kernel compilations. Patching the kernel is a good choice if you wish to upgrade from your current patch level to the next consecutive patch level. For example, patching kernel 2.2.5 to 2.2.6 involves applying one patch. However, if you wish to upgrade the 2.2.5 kernel to 2.2.14, then a patch for each patch level must be applied sequentially. In this case, it may be better to download the entire 2.2.14 kernel


Now we know the compilation process in theory, let’s compile the Linux kernel:-
Acquire Source
The best place to get the latest source at is http://kernel.org/. Choose the version you would like to use, either in the 2.6 or 2.4 branches and click on the F link on the right side. Save that file to the /usr/src directory on your machine. If you do not have a src directory, then simply create it.

Extract and Patch
Extract the source by running the following command:
---------------
[root@localhost root]# tar -xvjf linux-2.6.6.tar.bz2
---------------

If you downloaded any additional patch sets, then you will need to unzip them as follows:
---------------
[root@localhost root]# bunzip2 patch-2.6.6-bk5.bz2
---------------

After uncompressing the patches, then you can apply them to the source. Remember to apply them in order if you have more than one.
--------------------------------------------------------
[root@localhost root]# cd linux-2.6.6
[root@localhost root]# patch -p1 <../patch-2.6.6-bk5
--------------------------------------------------------

Assign a Unique Name
This is completely optional, but if you end up having to make several different revisions of the kernel, then you will find this quite helpful. Inside of the kernel directory is a file called Makefile. You should consider updating the EXTRAVERSION line to include a revision number, such as:
---
VERSION = 2
PATCHLEVEL = 6
SUBLEVEL = 6
EXTRAVERSION = -1
---

Backup .config
If you have configured your kernel previously, then you should consider backing up your previous configuration as follows:
---
[root@localhost root]# cd linux
[root@localhost root]# cp .config config.save
---
Configure the Kernel :-  There are 4 main frontends to configuring the kernel: config, oldconfig, menuconfig, and xconfig. You simply choose one of these and type, for example, make menuconfig.

a. config is the least user-friendly option as it merely presents a series of questions that must be answered sequentially. Alas, if an error is made you must begin the process from the top. Pressing Enter will accept the default entry, which is in upper case.

b. oldconfig will read the defaults from an existing .config and rewrite necessary links and files. Use this option if you've made minor changes to source files or need to script the rebuild process. Note that oldconfig will only work within the same major version of the kernel. You cannot, for example, use a 2.4.x .config with the 2.6.x kernel.

c. menuconfig is probably the best option if you do not have X-Windows available.

d.   xconfig, as the name suggests, is an X-Windows based frontend. It uses the Tcl/TK libraries to present a nice GUI that is similar in structure to the ncurses config used with the menuconfig. Below is an example screen:

Once you have made all of your configurations, you can save your settings. By default, it will save as .config in the topmost directory. You can save this here, but I advise you to copy this file after you exit this utility.
Build Dependencies
The next step is create all of the necessary include files that the kernel needs to run and compile. If you are compiling a 2.4.x kernel, then run the following:

---
[root@localhost root]# make dep
[root@localhost root]# make clean
---

With the 2.6.x kernels, these 2 steps can be skipped.

Build the Kernel
We are now (finally) ready to start the actual kernel build. At the prompt type:
---
[root@localhost root]# make bzImage
---
Build the Modules
Although you have finished building the kernel, without all of the modules that the kernel loads and runs, the system will be pretty useless. You need to run the following 2 commands to build and install the modules:
----
[root@localhost root]# make modules
[root@localhost root]# make modules_install
----

Install
Not sure if this works or not on the 2.4.x kernels, but running the following command on a 2.6.x kernel will make the appropriate boot images and update grub as needed to include this kernel.
---
[root@localhost root]# make install
---

Reboot and Test
Simply reboot the system and then from the grub menu, choose the kernel that you built. If all goes well, then you should be up and running with all of your peripherals responding correctly and without any crashes. If, however, something breaks, you still have your prior kernels you can go back to at any time.
Hope you liked the article; feel free to post your comments/suggestions if any.

4 comments:

  1. You can even build the .deb package for the of the kernel using the following command
    CONCURRENCY_LEVEL=`getconf _NPROCESSORS_ONLN` fakeroot make-kpkg --initrd --append-to-version=-custom kernel_image kernel_headers

    ReplyDelete
  2. When you are building a new kernel, you can copy the running kernel config as .config

    cp /boot/config-`uname -r` .config

    In cases where your kernel source is significantly newer than the existing config file, you'll be presented with all of the new config options for which there is no existing config file setting. You can either sit there and keep hitting Enter to take the default (generally safe), or you can just run
    yes '' | make oldconfig

    ReplyDelete
  3. make distclean or make mrproper, are the other options for cleaning the kernel source, which clean the kernel source completely including certain configuration files, which are not removed by make clean

    ReplyDelete
  4. Thanks Sugnan for the comments. I wasn't aware of yes '' | make oldconfig option, i felt it extremely useful while automating few builds.

    Thanks

    ReplyDelete