Recompiling Your Kernel (Linux Tips)

Today’s tip may be for some more advaced Geeks; Recompiling Your Linux Kernel can be extremely daunting at first glance, but once you’ve gone though it once or twice, it’s no big deal, either way, here we go:

Why should I compile my own kernel? What do I need?

If you’ve just installed Linux, there was a kernel with your distribution. Why should you recompile it?

Most often there is a reason for you to compile your own kernel. First, you might have some hardware not supported in your current kernel, forcing you to compile your own.

Second, you might want to compile your own kernel because the one you have has too much hardware support compiled in. As an example, if you have a system with only (E)IDE harddisks, you don’t your kernel to support several SCSI controllers. The same goes for odd hardware like CDROM:s connected to soundblaster sound cards and other similar things.

Recompiling your kernel will make it smaller, taking upp less memory. On todays computers that’s often not a problem, since they have enough memory anyway.

Security is another aspect. Sometimes some security related bug is found in the kernel code, and a fix is released as a patch or in the next kernel release. When you recompile your kernel your computer won’t be vulnerable to that security problem any more.
OK, so now I’ve decided to compile my own kernel, what do I need?
A C compiler

You need a c compiler with friends. To be more specific, you need the GNU C compiler, gcc. Try gcc at your command line. If you get the message gcc: No input files you have gcc. Make sure it’s not historical. If you don’t have any gcc, find out how to install it.
The Linux kernel source

You need (of course) the Linux kernel source, availiable at a very large number of ftp sites around the world. Try pointing your web browser at http://www.kernel.org. Try finding a kernel archive in your country by appending your country code (Ie ’se’ for Sweden, ‘us’ for the USA, ‘fi’ for Finland..) after the www, making the URL something like www.us.kernel.org . That will take less international bandwidth, and that’s always a good thing.

Once you’ve got the source (that will take some time for a modem user, since it’s quite big. At the moment I’m writing this, the 2.2.10 kernel is 13 Megabytes) you should unpack it. Preferably in /usr/src since that’s the place it should be. So.. get the rights you need and go to /usr/src and execute tar -zxvf /foo/bar/linux-2.2.10.tar.gz. That will take some time.

In my humble opinion, when I say “get the rights you need” above, I don’t mean you should get root since that’s a user you shouldn’t use when it’s not absolutely neccessary. Fewer things broke that way.. So, it’s better if you check the rights needed and make yourself a member of the correct group.
Time.

Compiling the kernel will take some time, it’s quite a lot of source code. Depending of your computer, and how much you include in your kernel configuration it may take anything from less than 5 minutes to a day or two. (The first example is my current Intel PII 350, the second if you’re compiling on an i386. Don’t do that :-) )

So, when you’re done with your kernel configuration; set the whole thing off and go get yourself a nice cup of tea…or something.

So, now I have the source, what do I do?

We are now ready to do the real work. Configuring, complining and installing the kernel. I’m not going to describe every single configuration entry availiable, since that would be a novel by itself, and there’s already very good documentation inside the tools you use to configure the kernel.
Configuring the kernel
What tools are there to configure the kernel?

Configuration of the kernel can be done in four different ways.

First, there is the very manual way. This is when you edit the file /usr/src/linux/.config by hand. A very primitive way of doing things, don’t do this since there is better ways.

Second, there is make config. Execute make config in the /usr/src/linux directory, and the kernel configurator will ask you a lot of questions. You have the ability to read some help text about each question by pressing ‘?’. This is a better way than the first one, but still quite boring.

Third, there is make menuconfig. This will present you with a text-based program where you make your choices by moving around in some menus. This is a good way of configuring the kernel if you computer doesn’t have the X window system, or if it’s to slow to run X.

Fourth, and last, is make xconfig. This will give you an X window system based configuration program that is pretty nifty.

Compiling the kernel

So, now our kernel is well configured and we are ready to compile it. This is done with the command make bzImage followed by make modules and make modules_install. If you haven’t enabled modules, you don’t need to do the last two steps. The whole process will, as I’ve mentioned earlier, take some time.

Installing and booting the new kernel

To run the new kernel you have to reboot your machine. This is one of the few things software related you have to reboot to fix.
Installing the new kernel in LILO

Your newly compiled kernel is at /usr/src/linux/arch/i386/boot/bzImage . That’s not a good place for a kernel to be, so let’s move it. If you machine doesn’t have a Intel family processor, the i386 should be replaced by something appropriate, such as ‘alpha’ or ‘mips’.

Personally, I keep my kernels in /boot, but you are free to choose a place to copy the file above to. Keep in mind though that for most PC bioses to boot the kernel it has bo be located in the first 1024 cylinders of the harddisk.

Now install the kernel in the /etc/lilo.conf file. Basically you just put the name of the image file in the top of the file, and a symbolic name too. LILO will boot the first image in the configuration file. Don’t forget to run /sbin/lilo - otherwise the change won’t take effect. I’ve done that mistake, several times :-) Always keep at least one kernel you know work in the lilo.conf, so you may rescueboot if your newly compiled kernel doesn’t work.

Reboot, and see what happends. If you’re unlucky, it won’t boot at all, then restart and choose another kernel at the LILO command line. Watch the boot messages closely, too see that all your hardware is found. With the dmesg you can see what the kernel said, after it’s booted.

Whew! That was a pretty crazy one, alright well, this can be a really fin and rewarding, it can also be very frustrating. Don’t worry, everyone who is now a linux expert was at some point and time a linux no-nothing. It just takes time. So go have a good time and remember to email me or post it in the forums, peace out-

-b

Share and Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • digg
  • Reddit
  • Furl
  • NewsVine
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • connotea
  • De.lirio.us
  • Fark
  • feedmelinks
  • LinkaGoGo
  • Ma.gnolia
  • Netvouz
  • RawSugar
  • scuttle
  • Shadows
  • Simpy
  • Smarking
  • Spurl
  • TailRank
  • Wists
  • YahooMyWeb

No Comments to “Recompiling Your Kernel (Linux Tips)”  

  1. No Comments

Leave a Reply