Originally Published: Monday, 5 June 2000 Author: Tom Dominico, Jr.
Published to: learn_articles_firststep/General Page: 1/1 - [Std View]

Compiling the Kernel: Part 2

Last week, we discussed the basics of kernel compilation: what the kernel is, why you might want to compile one, and resources for more specific help. By popular request, we are pleased to present a second installment of the series, in which we'll provide our own step-by-step guide to actually performing the necessary configuration and compilation. Our goal will be to get you up and running as painlessly as possible.

Last week, we discussed the basics of kernel compilation: what the kernel is, why you might want to compile one, and resources for more specific help. By popular request, we are pleased to present a second installment of the series, in which we'll provide our own step-by-step guide to actually performing the necessary configuration and compilation. Our goal will be to get you up and running as painlessly as possible.

The first step, naturally, is to download the kernel source. Depending on your distribution, this procedure may vary. For my Debian system, getting the kernel source is as simple as "apt-get install kernel-source-2.2.15" (or whatever version you want to use). The traditional way to get the source is by visiting ftp.(your-country-abbreviation).kernel.org/pub/linux/kernel/, and then choosing the appropriate version. You'll probably want to stick with the stable kernel series, the latest of which is 2.2.15 as of this writing. You'll notice that there are two different types of kernel packages: one that ends with .gz, and one that ends with .bz2. This indicates the type of compression that was used. Bzip2 does a better job of compression, so you'll probably want to download the .bz2 package, but you will need the bzip2 utilities installed to extract it. Or, you can simply get the .gz package and use gzip. When in doubt, use gzip; although bzip2 is increasing in popularity, it may not have been installed with your distribution.

Now that you've got the source, it's time to extract it. If you used your distribution's packaging system, it's probably already been extracted for you. At any rate, you'll want to become root by typing "su" at the command line, then your root password, and head to the /usr/src directory by typing "cd /usr/src". You may already have the current linux source there, in /usr/src/linux. If you've got the disk space, you should probably keep it around, just in case. Rename the directory to reflect its version number (for example, "mv linux linux-2.2.12"). Move the new kernel to the /usr/src directory. Now, unpack your kernel if you downloaded it by hand. For gzipped files, use "tar -zxvf (filename)". For bzipped files, use "tar -Ixvf (filename)". This will create a new "linux" directory, containing the kernel source in all its glory. If you used your distribution's packaging system instead of downloading the kernel by hand, it may have extracted the source into a directory labeled with the kernel version. In that case, you can either create a symlink (using the "ln" command) from that directory to /usr/src/linux, or rename it.

Now comes what is arguably the trickiest part: configuring the many kernel options. To begin with, cd into /usr/src/linux. Now, you've got three options at this point. "make config" takes you through the configuration options, one at a time. I do not recommend this, as you will have to restart the process if you make a mistake. "make menuconfig" is the second option, if you have the ncurses libraries installed. This is my personal favorite, which will give you a set of menus from which to choose your options. Finally, "make xconfig" will give you an X-based configuration tool, provided you have Tk installed and X is running. Some users may be more comfortable with this option. It's really a personal choice.

You will be presented with a number of options, most of which are answered with a "(y)es" or a "(n)o". A third option, usually associated with device drivers, is "(m)odule". This means that the driver will not be compiled directly into the kernel, but instead as a loadable module. This is the preferred choice for most drivers. The specific choices you'll have to make are beyond the scope of this article. You'll need to know about your hardware configuration, because the answers you provide will depend on that information. Many of the options are self-explanatory, but for others, you may need to consult the documentation. Try the official HOWTO, as well as the documentation in /usr/src/linux/Documentation/Configure.help. There is also a '?' option during the configuration that provides a brief description, which may be helpful. If you've exhausted all these sources, try #linuxhelp on irc.linux.com for assistance.

Congratulations! Once you've configured the kernel, the bulk of the work is finished. You'll now issue a series of commands that actually build the kernel. First, type "make dep", which ensures that all dependencies are in place. After that, type "make clean" to perform some housekeeping and clean up the files a bit. This is not always necessary unless you are recompiling the kernel, but I usually do it anyway.

At this point, you're ready to actually compile the kernel. The first command I usually issue is "make bzdisk", after inserting a blank floppy in the drive. This step compiles the kernel, makes a compressed image, and puts it on the disk. You can then boot from the floppy to test out your new kernel, without having to make any changes to LILO. If something goes wrong, just reboot without the floppy, and you're back to your old kernel. Once you are sure that things are working properly, you can cd back to /usr/src/linux and "make bzImage". This will place a file in arch/i386/boot called "bzImage", which is your compressed kernel image.

To install the kernel, you can use 'make bzlilo' if your kernel is in /vmlinuz, lilo is in /sbin, and your lilo config (/etc/lilo.conf) agrees with this. If not, refer to "Installing the Kernel" in the HOWTO. It will provide further instructions on how to back up the old kernel and edit your lilo.conf file so that you can fall back to it, if necessary.

Finally, it's time to make your modules. As root, cd back to /usr/src/linux and type "make modules". Then, "make modules_install". This will install your modules in /lib/modules/(kernel-version). You'll then need to update your configuration to point to the new modules. This will depend on your distribution, and is beyond the scope of this article. On my Debian system, "modconf" will do the trick. Consult the documentation for your system as to how to do this.

Well, you've done it -- you've compiled and installed a new kernel! Congratulations on taking a big step forward in your Linux experience. I would highly recommend that you consult other documentation (such as the official HOWTO) if you have any questions that are not covered in this article, and for further information. Hopefully, the HOWTO will make more sense now that you've read these two articles. Keep checking the FirstStep section of Linux.com, as we'll be bringing you more tutorials to help make the most of your Linux experience.

Tom Dominico (tomd@linux.com) is a programmer, database administrator, and Linux convert. Cursed with insomnia, he spends his sleepless nights chatting on IRC, tweaking his Linux box, and reading everything he can get his hands on.