Wednesday, June 25, 2014

Kernel Programming Step 1

Hallo there, I hope that my last post on Yocto project would have helped you. Taking things forward, this blog post has its focus on the Linux kernel.

This post will emphasise on the First step to kernel programming.
 so what are the first steps?
simple... Build a kernel and install it.

and the i expect some Linux proficiency from the readers, if you have written a simple hello world program and compiled it in Linux terminal you are good to go.

if at this point you are still wondering what a kernel is ? or what does kernel programming means.. we shall do a quick, lightweight insight into these aspects... This is a different kind of task so if any issues , just post in comments, i will be glad to help.

1. Kernel is the core of the operating system that you use, it is NOT the GUI, it is not NOT the application it is a forever running program that calls other programs based on the request that you give.. Putting it in layman terms the Postal office does not write the letters for you, nor does it reads it for you it just takes your mail from you to your destination and it is not just the mail , it parcel, money orders and what not?  lets use this postal service example for rest of the blog tooo... :)

2. kernel programming is telling the kernel how to do something.. keep in mind that the kernel already has many things up in its armoury, we shall add some extra features or improvise the existing ones.. lets say faster delivery of mail or adding support for food delivery.

does the term kernel applies to Linux kernel only?
not necessarily, there is the Unix kernel (Linux is inspired from this) then there is the windows kernel (closed source) and many other operating systems for hardware small and big. however we shall be working on the Linux kernel only, because of the ease of access that is provided for everything.

how will this help me?
well, for starters you can add your own functionality to the OS which you wouldn’t have achieved before. you could also do some pretty nasty things, but that can wait.
Kernel programming do requires lots and lots of reading as things are much more based on policy than implementation. you are not the one deciding how the kernel should work (unlike the user application) instead you work using the existing features that are available in the kernel and do something new. if you still want to dictate things your way, you are always free to download the source and modify it in ways that you want, nobody is stopping you.. Open source baby !!

in one sentence I would say Kernel programming is like a elevated state of programming.

Here you have to follow the rules to get your work done. why so? because there is no backup, its like the last stage of a video game, you have to measure your steps, yes you will fall but you will succeed. just Hold on tight.
There might be some questions on the mind of those who are just getting started, but no issues as they will fade away once you start.

I am using Ubuntu 14.04 and had all the packages that I require before hand. to be sure
On fedora and other similar systems
$ sudo yum install gcc make git ctags ncurses-devel
 
on ubuntu and other similar systems
$ sudo apt-get install libncurses5-dev gcc make git exuberant-ctags
 
 

Step 1: Get the Kernel Source.
there are 2 ways to do this , one is to download a tar-ball from Kernel.org and the other one is to clone a stable git repo of the kernel. I used the latter method as it gives me all the available versions of the kernel source while the first one only gives a particular release.

I used the latest Long  term support version of the kernel, that is v3.12.22 you are free to use any version you want. Here I expect some proficiency with the Linux system like unpacking a tar-ball, copying and renaming files among other things.

if you want to clone the git repository just run the following command
$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git

you will have the source in the directory called "linux-stable"
$  cd linux-stable
 
to look at the available versions
$ git tag -l | less
 
 to checkout your version use this 
$ git checkout -b stable tag
 
you will have a branch called "stable" and replace "tag" with "v3.12.22" or your required version.

for those who downloaded the tar-ball, Once you have downloaded the source, extract the tar-ball and you would have the Linux source, CD into the Linux source .
You should see some directories make sure ARCH, KERNEL and DRIVERS are there among others.

now you need a configuration for the build... these directories will remain same for those who have cloned the git repo too.
if this is complex, please do a google search, there are many resources out there for your help.

Step 2: Get a config file
now, as a begginer it is not advised to build your config, use the existing config that is available. your distribution will have its config in the /boot directory. my config file was called config-3.13.0-24-generic copy this into your linux source directory as .config. This filename is important, you have to name it .config.

any time you think that you have messed up the directory, just do a
$ make mrproper

this command should work, run it from the Linux source directory.
$ cp /boot/config-`uname -r`* .config
 
this should not pose a problem, run this command to make sure everything is fine, if you do not have a configuration file  run this command to build one for you
$ make defconfig
 
$ make oldconfig
to verify that all settings are fine.

now
Step 3: Build the kernel
$ make -jX
where X = number of cores *2 on your CPU. if not sure just
$ make
 
this might take lots of time and CPU resource and a lot of build messages would be printed on the screen during this time. while this is happening I would advice to read the first chapter of "Linux kernel in a nutshell" by greg kroah hartman, he is the maintainer of drivers for the Linux kernel.
after this is done only one command remains
$ sudo make modules_install install
  
you must this run as SUDO.
if you are on UBUNTU, do a
$ sudo update-grub
 
 so that grub knows about the new kernel.
If things are not going well till now, I know they wont do not fret, just post in comments I would be glad to help. Time to reboot now and in the grub menu, select the new kernel that you have created and installed.
Summary:

Step 1:
$ sudo apt-get install libncurses5-dev gcc make git exuberant-ctags
or
$ sudo yum install gcc make git ctags ncurses-devel

Step 2:
$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git
$ cd linux-stable

Step 3:
$ git tag -l | less
$ git checkout -b stable tag

Step 4:
$ cp /boot/config-`uname -r`* .config 
or
$ make defconfig

Step 5:
$ make
$ make -jX

Step 6:
$ sudo make modules_install install

Step 7:
$ sudo update-grub2

Step 8:
$ sudo reboot

feel free to post any questions in the comment section, will be more than happy to help.





 

3 comments:

  1. Nice post and thanks for share any way . I want you to visit the link below to get some useful info like BT helpline Customer Care Number

    ReplyDelete
  2. You’ve written nice post, I am gonna bookmark this page, thanks for info. I actually appreciate your own position and I will be sure to come back here.

    baixar facebook
    baixar whatsapp
    unblocked games

    ReplyDelete
  3. Internet of things is the newest addition to the cathalogue of https://ax-dynamics.com/microsoft-dynamics-ax's services. Be sure to check it out and make your work a better place!

    ReplyDelete