...making Linux just a little more fun!
In order to compile a new kernel we have to download the source code of the Linux kernel. We can download the source from www.kernel.org. Here we can find all versions of the Linux kernel source code. Let's take an example. Suppose we want to compile the 2.6.9 version of the linux kernel. We have to download the 2.6.9 source code from:
https://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.9.tar.bz2
It's better to download the bzipped version, as that will be more compressed than its gzipped counterpart; hence will take less time to download. A wget from the command line will look like:
wget https://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.9.tar.bz2
Once we download the required kernel version source, we need to bunzip and untar it. We can do the following:
tar xvjf linux-2.6.9.tar.bz2
The 'x' option is to denote the untarring (e'x'traction), 'v' for verbose, 'j' for specifying that we need to bunzip the file before untarring and 'f' for stating the name of the input file.
The file will untar into the directory linux-2.6.9. Once it's untarred 'cd' to linux-2.6.9.
This will bring up the ncurses interface for configuring the kernel. There are other options such as 'make xconfig' and 'make config'. The former will bring up the configuration menu in graphical mode and the latter in text mode.make menuconfig
Once we select the different components we want for our kernel, we can exit the configuration interface. We should select the option to save the configuration from the configuration menu, before exiting.
After we have configured the kernel as mentioned above, we can find a file named '.config' in the top level directory of the source. This file is the configuration file. It contains various options and their states (whether they are selected or not). For example, if we choose to have the PCI support in our kernel we can find an entry of the form:
in the .config file. Similarly, options which are selected as not required will appear as not set. Suppose we have not selected the XFS filesystem support in our kernel we will find the following in the .configCONFIG_PCI=y
# CONFIG_XFS_FS is not set
A great feature of 2.6 kernels is that if we are running make menuconfig (or xconfig or config) for the first time, then the configuration menu we are presented with is based on our current kernel configuration. In my case, I have a Fedora Core 1 system. The kernel which I run is '2.4.22-1.2115.nptl'. Hence when I run a 'make menuconfig' for the first time on the source then the configuration menu presented will contain the options as given in '/boot/config-2.4.22-1.2115.nptl'.
This will build the dependencies. But for a 2.6 kernel we can skip this step. The dependencies are automatically created when making the final image with a 2.6 kernel.make dep
In 2.6 kernels this step will also resolve the dependencies and proceed to create a bzImage image.make bzImage
After the compilation is over we can find the kernel image at the path arch/i386/boot/bzImage in case of an image for a 386 based processor (Pentium, AMD etc.).
This command will compile the components (which are selected for module compilation) to modules. In a 2.4 kernel the result will be .o files of the corresponding components. But in a 2.6 kernel the output file will be a .ko module. For example if we have given the option for the Network driver of Realtek cards to be built as modules then after giving a 'make modules' we can find in 'driver/net/' a file named 8139too.o in the case of a 2.4 kernel and 8139too.ko in the case of a 2.6 kernel.make modules
After we have compiled the modules, it's time now to install the modules. To install the modules run:
as root. This will install the modules and other necessary files into the /lib/modules/2.6.9 directory.make modules_install
This will update the kernel image on to the /boot area, update the configuration file of the bootloader (lilo.conf or grub.conf) and then do the necessary actions to make the new kernel bootable.make install
After this we need to reboot the machine. When the machine boots next time the boot menu will present us with the option to boot from the new kernel we built. We choose that option and voila!! boot into a kernel we built all by ourselves!
After this we add the following entry to /etc/lilo.confcp -a arch/i386/boot/bzImage /boot/bzImage-2.6.9
We should run lilo after thisimage=/boot/bzImage-2.6.9 label=2.6.9-kernel root=your_root_disk
We will reboot the machine after this. When we are prompted at the lilo prompt enter '2.6.9-kernel' as the boot option and we will be booting to the new custom built kernel.lilo -v
#make bzImage CHK include/linux/version.h UPD include/linux/version.h SPLIT include/linux/autoconf.h -> include/config/* CC scripts/mod/empty.o HOSTCC scripts/mod/mk_elfconfig MKELF scripts/mod/elfconfig.h HOSTCC scripts/mod/file2alias.o HOSTCC scripts/mod/modpost.o HOSTCC scripts/mod/sumversion.o .... ....If we need to know what commands are used for compilation, then we need to give the verbose compilation option while compiling. That is:
This will output the commands which are executed while compiling. Here is a snippet from the compilation output:make bzImage V=1
<..snip..> make -f scripts/Makefile.build obj=init gcc -Wp,-MD,init/.main.o.d -nostdinc -iwithprefix include -D__KERNEL__ -Iinclude -Wall -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -pipe -msoft-float -mpreferred-stack-boundary=2 -march=i686 -Iinclude/asm-i386/mach-default -O2 -fomit-frame-pointer -DKBUILD_BASENAME=main -DKBUILD_MODNAME=main -c -o init/main.o init/main.c CHK include/linux/compile.h UPD include/linux/compile.h gcc -Wp,-MD,init/.version.o.d -nostdinc -iwithprefix include -D__KERNEL__ -Iinclude -Wall -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -pipe -msoft-float -mpreferred-stack-boundary=2 -march=i686 -Iinclude/asm-i386/mach-default -O2 -fomit-frame-pointer -DKBUILD_BASENAME=version -DKBUILD_MODNAME=version -c -o init/version.o init/version.c <..snip..>
This will remove most generated files but will keep the configuration file.make clean
If we need an absolute cleaning, i.e. if we want to return the source to the state in which it was before we started the compilation, then do a
This command will delete all generated files, the configuration file as well as various backup files. This will in effect unwind all the changes we made to the source. The source after this step will be as good as it was just after the download and untar.make mrproper
Krishnakumar loves to hack the Linux kernel. He works
for Hewlett-Packard and is a BTech from Govt. Engg. College
Thrissur.