Saturday, February 12, 2011

How To Complie Linux Kernel (For Beginners)


Compiling Linux Kernel, according to a popular myth, is a very tough job. Personally I find it really easy and I'll walk you step by step through this process. I am using Fedora 13 KDE for the compilation.

Before building your kernel I would advice you to backup your data and grub.conf.
  1. Download the source from http://kernel.org. The convention is that that even numbered sources like 2.2, 2.4, 2.6 etc are stable while odd numbered like 2.3 and 2.5 are not suited for production environment. I am using kernel 2.6.37 for this tutorial.
  2. Assuming that you have placed your source on /usr/src/, extract the kernel source form the archive using the following command:
    tar xvjf linux-2.6.37.tar.bz2
     
  3. Now, it is a good practice to clean the build area before any compilation.
    make mrproper
     
  4. Let us start with the configuration now. Kernel source comes with several configuration tools to make your life easier. I will use xconfig but a GNOME user might want to go for gconfig.
    make xconfig
  5. Now select the modules and features you want your kernel to have. I would recommend checking Loadable Module Support. I also got rid of all Mac driver since I use a Dell. Likewise you can strip down your kernel easily. Save the file once you are done.
  6. Once we got the .config file we should go to the Makefile and add a customization marker to differentiate my kernel from the default ones. I'll assign a value to the variable EXTRAVERSION in the Makefile. For me it was EXTRAVERSION = -aditya.
  7. Now just run the make command and be patient. It took my machine around 2 hours for the compilation. I am running an Intel Core 2 Duo and 3 Gig of RAM.
  8. Once the kernel is compiled install the modules.
    make modules_install
  9. Now I would copy my kernel and the system map to the /boot.
    cp /usr/src/linux-2.6.37/arch/i386/boot/bzImage /boot/vmlinuz-2.6.37-aditya
    cp /usr/src/linux-2.6.37/System.map /boot/System.map-2.6.37-aditya
  10. Run the new-kernel-pkg to create the list of module dependencies, update grub.conf etc.
    new-kernel-pkg -v --mkinitrd --depmod --install 2.6.37-aditya
And you are done. You actually built a kernel. Now reboot and enjoy it :)

Update: Please check out the comments for some cool tips.

7 comments:

  1. It is a good write up. I would polish it with the following
    4. "make menuconfig" would work on either kde or gnome or xfe or any other wm. It just requires "ncurses" available with all the distros
    5. U should start with .config files prepared by specialists like from debian or u can use .configs from the liquorix project and also consider patching the vanilla kernel with liquorix patches
    6. Is un-necessary
    7. On a multi core processor u can use make -j 3(for a dual core processor, ie, no of cores + 1). The linux kernel is very modular and this command would definitely speed up things
    8. depmod is always run at the end of "make modules_install"
    9. u have copied the required files manually so one more step is in order "mkinitramfs -k -o /tmp/initramfs- " copy this from tmp to /boot
    10. Edit grub.cfg manually for booting ur flashy new kernel

    ReplyDelete
  2. I would agree with (4) but I find "make xconfig" more intuitive for kde.
    I am no expert but I did the compile a few times and never felt the need of (5). Can you please tell me a few upside of this?
    (6) is not requirement but is handy when you want to differentiate the kernels you built and the original kernels that came with OS unless you remember the version of all of them.
    I would definitely keep (7) in mind.
    Not sure what (9) is doing. Please explain it for me and other readers.
    I would not worry about (10) since new-kernel-pkg would take care of that.

    Your comment has helped me and others. Thanks for that :)

    ReplyDelete
  3. Hey,nice one. Thanks for showing those not in the know how to compile the kernel.

    I think though, that step 3 should be "make mrproper" instead of "make mrporper" though?

    Cheers

    ReplyDelete
  4. Great blog post sharing how easy it can be for a user to build their own Linux kernel!

    As to putting a file in /tmp, normally users do this so that files no longer needed will be deleted when you reboot as normally /tmp is erased on a new boot of the PC.

    This is the best way to control FLASH COOKIES, put them in /tmp and they all get erased between boots. In addition to protecting your privacy, it keeps your PC from getting bogged down with excessive flash tracking cookies.

    All Linux users can do this and I highly recommend it! To Banish all flash cookies (between boots) forever:

    http://www.theinquirer.net/inquirer/news/1725591/banish-flash-cookies-forever-linux

    Of course if all video content was created using open source video (FFMPEG) and audio (Ogg Vorbis) codecs we could all see them no matter what operating system we preferred.

    There are many players like MPlayer that work on every operating system. Isn't that the way it should be?

    ReplyDelete
  5. 5. Patching a vanilla kernel with patches from the liquorix kernel project or from the zen kernel project makes available to the users many of the patches which are not yet part of the vanilla kernel. There are many optimizations to the vanilla kernel which speeds up and adds additional drivers. This would in turn good for people with the latest laptops around. Try it once. U will love it.

    Starting from a .config which is "generic" that is, most of the options are selected by default will work on most laptops/desktops. Once u have a working kernel, U can then tweak around and optimize it to ur machine.
    9. Is the last file required to boot ur kernel ie, the initrd file. This will go to /boot with vmlinuz, system.map."cp /tmp/initrams-** /boot/initrd.img-**"
    10. new-kernel-pkg, I think would lose all the customizations in /boot/grug/grub.cfg. Editing the grub.cfg file manually would complete the "manual" kernel compile.

    Thanks again. U are doing a great job. All the very best.

    ReplyDelete
  6. @Abacus: Thanks for pointing out the typo. Fixed!

    @Srini: I would definitely give vanilla kernel a try.

    ReplyDelete

Note: Only a member of this blog may post a comment.