201.2. Compiling a kernel
201.2 Compiling a kernel
Weight: 3
Description: Candidates should be able to properly configure a kernel to include or disable specific features of the Linux kernel as necessary. This objective includes compiling and recompiling the Linux kernel as needed, updating and noting changes in a new kernel, creating an initrd image and installing new kernels.
Key Knowledge Areas:
/usr/src/linux/
Kernel Makefiles
Kernel 2.6.x/3.x make targets
Customize the current kernel configuration.
Build a new kernel and appropriate kernel modules.
Install a new kernel and any modules.
Ensure that the boot manager can locate the new kernel and associated files.
Module configuration files
Use DKMS to compile kernel modules.
Awareness of dracut
Terms and Utilities:
mkinitrd
mkinitramfs
make
make targets (all, config, xconfig, menuconfig, gconfig, oldconfig, mrproper, zImage, bzImage, modules, modules_install, rpm-pkg, binrpm-pkg, deb-pkg)
gzip
bzip2
module tools
/usr/src/linux/.config
/lib/modules/kernel-version/
depmod
dkms
Kernel
Why we should compile our linux kernel or upgrade it ? to tell the truth as linux kernel is so modular and flexible, most of the time there is no need to manipulate kernel. Specially When we are talking about commercial versions of linux like redhat, manipulating kernel cause support issues. In contrast in embedded linux system, you should try to make kernel as small as possible. So compile or upgrade kernel just if there is a good reason.
Upgrading kernel and compiling it is very distro specific. The way that we traverse to achive that might be different in Ubuntu, Open Suse or cent OS. Upgrading kernel include compiling, so here we start with downloading new version of kernel and compiling it, but as an administrator you might recompile current version of your linux inorder to add / remove some modules and futures.
Upgrading kernel
First go to https://kernel.org inorder to get the desired kernel version:

We chose latest stable kernel version, do not forget we have to be in /usr/src/ and from now on work in this directory only, in this demonstartion we use CentOS7 machine:
Now lets extract .xz file. Also we should make a symbolic link as linux which point to downloaded kernel:
Before continue there is document folder inside downloaded kernel, There are amazing help text file:
Before continue there are some Development tools which is need to compile the kernel lets install them first:
Next step is making kernel with modules that we like, compile it and make other kernel required components. to do do that we use make. make is a command that make a program from the source code. Make can do somany thing and is used for many targets.
Lets classified each target with it usefull commands:
Cleaning Targets (used if you have tried to compile before)
Description
clean
Remove partially Generated Files From Previous compile and refresh the code but keeps config file
mrproper
clean everything + config + backup
Configuration Targets(To configure desired kernel modules)
Description
config
line oriented program, take days (very hard tool)
nconfig
ncurses based menu program (hard tool) [need to nstall ncurses-devel]
menuconfig
menu base program (recommanded tool)
xconfig
QT-Based menu program, like menu config [need to install qt-devel]
oldconfig
use the current setting of current kernel for the next one
The thing we are going to do is making kernel image (zImage / BigzImage if you remember :) )and we need to make modules that should be available in the kernel. Final step is installation process. Because we need to put what we have made in proper place. Let take a look at make xconfig , make menuconfig:

No matter what you choose, a module can have different states:
Condition To choose
Description
<>:Not Available
not even compiled to the kernel
<M>:Loadable as module
compiled with the new kernel, but not loaded as default
<*>:available
Compiled and loaded , Built into the kernel
We are happy with current kernel configuration so lets try:
Wow after answering tons of questions finall:
Make bzImage
ops we encounter a problem, lets resolve it:
And Finally our bzImage is ready:
make modules
Modules need to be compiled too, so do the same thing for them:
make modules_install
Now that every thing is prepared, its time to put all part to the their right places, First let put compiled modules to /lib/modules/
to confirm what has been done:
make install
make install write required files into the /boot and modified grub and set everything for us
and reboot and enjoy your new kernel!
dracut
As we have seen kernel needs RAM to be populated with some drivers and modules.In old days each distro tries to implement its own taste of initrd/initramfs. It made hard for users and developers to work with each distro. dracut is a tool which has tried to standardize the process of making initrd/initramfs.
dracut uses udev deamon events (uevents) and based on them create general purpose initramfs. All we need to know is that dracut is a program wich is used behind kernel installation process to create initrd/initramfs. It considers current kernel and settings and generates desired initramfs/initrd base on them and put initramfs/initrd on the right place and modified grub.
dracut also can be used to just modifying initrd/initramfs:
dracut reads its configurations from /etc/dracut.conf :
in dracut.conf you can commented out module/driver that you like and then run dracut --force to make your falavor of initrd/initramfs.
.
.
.
Last updated