Sunday 22 January 2012


Compile A Custom Linux Kernel – An Essential Guide



Reference And Courtesy :-   upcomingtechnology



You might question – why compile a custom linux kernel when the distro vendor already provides you with one? The answer is simple, the kernel which ships with your distro is a generic kernel, it means it’s designed to run on any machine on which it’s installed, it supports almost all types of processors, graphic cards, storage devices, wired/wireless networking, etc. As the generic kernel has support for plethora of PC hardware, you might argue that it is better to use a generic kernel instead of custom built kernel, but that’s not the case. A generic kernel has all the features you want, more specifically, your hardware wants, but it also has many features that you may never need. A simple example would be of the processor support, suppose you have an Intel processor the generic kernel will support it but it also has support for AMD processors which is redundant as you will never run the same kernel on an Intel processor and an AMD processor simultaneously. Thus a custom compiled kernel is more suited to your PC requirements and thus is smaller in size. Smaller size means your kernel occupies less space than the generic one but supports all the hardware you have installed in your machine.
To sum it up you might want to compile your custom Linux kernel to/for:
  1. Build a kernel customized for your hardware setup
  2. Improved performance
  3. Learn how kernel works
  4. Fun :D
Requirements :- 
Kernel sources: These are the source files of the kernel. You will need to compile these source files later. The latest kernel sources can be downloaded from HYPERLINK “http://www.kernel.org/”www.kernel.org.
Build Tools: These are the tools which you will require for compiling the source files. The method to install these tools depend on your distribution.
The packages required on Ubuntu/Linux Mint can be installed be executing the following command in the terminal,
sudo apt-get install -y build-essential kernel-package libncurses5-dev bzip2
On Fedora, execute the following,
sudo yum -y install gcc ncurses-devel
The command begins with a “sudo” because root access is required for installing packages, if you do not have access to the root account then you cannot install these.
Once you have installed the build tools and downloaded the kernel sources you are ready to compile your custom kernel.
The kernel source you have downloaded is in an archive, first you will need to extract the sources. Execute the following command without the ‘quotes’:
tar -xjvf  ‘kernel archive file name’ ’folder in which you want to extract the sources
Example, the command with the appropriate paths may look like:
tar -xjvf linux-2.6.25.tar.bz2 /home/casper/kernel/
After the kernel sources are extracted navigate into that directory and create a soft link named “linux”. Some Kernel sources require a path from the linux directory in order to compile, so the soft link needed. Run the following command to make a soft link,
ln -s ‘kernel source folder name’ linux
Example,
ls -s zen-stable linux
link
Now navigate to the linux directory and execute the following commands:
make clean && make mrproper
These commands clean the directory structure deleting any files from the previous builds.
Configure
Now we are ready to configure our kernel, but we have to choose whether we want to configure the kernel from scratch or use current kernel configuration as a base and develop on it. As we are beginners in the realm of kernel compilation we will stick with the easy method i.e. use the current configuration as a base. The current configuration can be found in “/boot/”.
Run the following command to copy the current configuration into your source folder,
cp /boot/’your config file name’ ‘your kernel source path/linux/.config
Example,
cp config-3.1.5-casper.kernel-v.1.1+ /home/nishikant/kernel/linux/.config
config
Now we need to compare the old configuration to the new one, so run the following command,
make oldconfig
oldconfig
In the screenshot the command doesn’t give any output because I am current running a custom kernel, but on your PC you might be prompted to select new features that are available.
Now run the following command,
make menuconfig
After executing “make menuconfig” a GUI interface will appear, here you can select the features you want. Say, processor type, cpu schedulers, IO schedulers, filesystem drivers, networking drivers, etc. Once you are done with the customization, select exit and then save the configuration.
save
Now you have finished customizing the kernel and it’s time to compile. The command to compile the kernel is,
make -j’number of cpu cores+1′
Example,
make -j3 (without spaces in between. Enter 3, if your processor is Intel core2duo)
If your processor supports hyper-threading then the number is twice the number of cpu cores,
Example,
make -j9 on Intel corei7 2600/2600k
The compilation process make take 15 minutes to an hour depending on your PC.
Once the compilation is over (without any errors), you are now ready to install your kernel, to install the kernel execute the following command,
sudo make modules_install install
After the kernel is installed you have to update your bootloader so that you can boot using your kernel.
On Ubuntu/Linux Mint the command is,
sudo update-grub
updategrub
While on Fedora, run,
su -c ‘grub2-mkconfig -o /boot/grub2/grub.cfg’
If you are using a different bootloader instead of grub then the above commands won’t work.
That’s it, you’re done compiling your custom kernel, after you restart your PC you’ll be able to boot into your kernel from the bootloader’s menu.
If your PC won’t boot into your kernel or some drivers don’t work then you can always boot into the generic kernel(that’s why having more than one kernel on your PC is always useful).
If you want to delete the kernel from your PC just delete the following file/directories(root access required):
/boot/’config-kernel name’
Example,
/boot/config-3.1.5-casper.kernel-v.1.1+
Next,
/boot/initrd.img-’kernel name’
Example,
/boot/initrd.img-3.1.5-casper.kernel-v.1.1+
Next,
/boot/vmlinuz-’kernel name’
Example,
 /boot/vmlinuz-3.1.5-casper.kernel-v.1.1
Next,
‘kernel name’ folder in /lib/modules
Example,
3.1.5-casper.kernel-v.1.1+
Finally update your bootloader,
On Ubuntu/Linux Mint the command is,
sudo update-grub
On Fedora run,
su -c ‘grub2-mkconfig -o /boot/grub2/grub.cfg’
So, this is it. 

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Great sharing your knowledge...
    https://www.caretit.com/services/odoo-erp-services/odoo-training

    ReplyDelete