CSC 262

CSC 262 Laboratory

Configuring and compiling the kernel

Contents

  1. General advice
  2. The kernel source tree
  3. Configuring the kernel
  4. Compiling the kernel
  5. Installing the kernel
  6. Try it!
  7. Other Build Steps
  8. Resources

General Advice

The kernel is the heart of the operating system and is what makes your computer run. For this reason, modifying the kernel or even changing its configuration is a very risky job. If you are not careful, you may end up with an unusable machine, which could mean having to reinstall the whole system. (Fortunately, this lab has been set up to try to help you avoid this fate.) Recompiling the kernel is also time consuming, so it is worthwhile to double-check every step of the lab before starting to recompile. IMPORTANT: always make backup copies of the files that you are going to modify. If you modify any kernel source files, put in lots of comments in every file that you create or modify, indicating what you did and why. Before you start, you may wish to make a backup of the entire Linux source tree, for extra safety.

One other note: many, if not most, of the commands in this lab will require root privileges to complete. If something does not work in ordinary usermode, try it as root.

The kernel source tree

The Linux kernel source code is mostly written in C and assembly language. It is located in the directory /usr/src/kernel/linux-x.y.z, where x.y.z indicate the kernel release (version). The kernel version in the current release is 2.6.18. The y number of this code is particularly interesting. By convention, if y is even then the release is stable. If odd, then the kernel is unstable, meaning that some features have not been fully tested and may result in a system crash. The latest releases of the linux kernel can be found at the Kernel Archives website (www.kernel.org).

question Q2. In these labs we will be working with the Linux kernel 2.6.18. Is this a stable version of the kernel?

The kernel source tree is organized into directories, some of which are described below:

Downloading the kernel

If you look in /usr/src/kernel, it appears as though the source for several flavors of the kernel are present. This is an illusion. The current Red Hat distribution includes only the header files necessary to build a kernel module. If we want to build an entire kernel image, we will need to get a copy of the full source from some place. (Instructions below come from http://www.mjmwired.net/resources/mjm-fedora-fc5.html#kernelsrc.)

Obtaining Kernel Source through 'yum': There are yum utilities which will download the LATEST kernel source.

# yum install yum-utils
# yumdownloader --source kernel --enablerepo core-source --enablerepo updates-source

Install the SRC.RPM file and Prepare source: Install the kernel.src.rpm that you chose to install in the previous steps.

# rpm -ivh kernel-2.6.20-1.2307.fc5.src.rpm

Set it up:

# rpmbuild -bp --target=$(uname -m)  /usr/src/redhat/SPECS/kernel-2.6.spec

Look in /usr/src/redhat/BUILD/kernel-2.6.20/linux-2.6.20.i686/ for the source.

Configuring the kernel

Before the kernel is compiled, it must be configured properly. Mostly we will be using the default configuration, but we will change a few things. Kernel configuration is possible via a user interface which allows you to select the features that you want include in the new kernel. We will also make some additional changes for documentation purposes.

To start, change the current directory to the root of the linux source tree (given above). The discussion below will assume that your working directory is set to this location, and any relative pathnames will be from this point.

cd /usr/src/redhat/BUILD/kernel-x.y.z/linux-x.y.z.i386

The kernel source configuration choices are stored in a file called .config. Usually we will want to make a copy of the original configuration file before we make any changes, so we can at least get back to where we were if something goes drastically wrong. (If there is no .config file present in the kernel source root directory, then you can copy one from one of the directories under /usr/src/kernels/.)

# cp .config .config-saved

Then type the following:

make xconfig

This graphical configuration program depends upon the QT package, which must be installed properly in order for it to work. If you get an error message, use these steps to install QT:

# yum install qt
# yum install qt-devel

If all goes well, then after a few seconds a window should appear with three panels: an index on the left, a detail view on the upper right, and a documentation panel on the lower right.

Browse through some of the categories on the left-hand side. As long as you don't click in any of the small boxes within the upper right window, nothing will be changed. Notice that as you borwse through different options, helpful text appears in the lower right panel describing the possible choices and making recommendations.

Each configurable option represents an optional piece of the linux kernel that can be included or excluded as you compile the kernel. Of course, some pieces are more optional than others -- you don't need to include support for hardware devices not on your system, for example, but certain options (such as the root file system) are vital. You may specify one of three configurations for each option, represented by the small box to its left: excluded (empty box), included (checked box), or compiled as a separate module (box with dot).

You will notice that most of the options have been compiled as modules. This often makes the most sense, because a module is available to the system but will not be loaded unless it is needed. However, for the purposes of this lab we will not be compiling all the modules -- so if we are going to need a particular feature, it will have to be built in (checked box). This condition drives the two changes below.

Ext3 File System
In order for the system to boot with our recompiled kernel, it must be able to read the hard drive partition where the system code is stored. We formatted our Linux partition using the ext3 journaling file system (an extension to ext2 designed to survive crashes robustly). Thus ext3 support must be built into our kernel. Clcik on File Systems in the left-hand pane, and make sure that all the entries in the upper right concerning the Ext3 file system are checked.
USB Mouse
Our systems have USB mice, so we must also add USB mouse support or we will be unable to use the mouse. Under Device Drivers, find the USB Support subsection. Make sure there are check marks next to EHCI HCD, OHCI HCD, UHCI HCD, and USB Human Interface Device (HID) Support in the upper right pane.
Kernel Hacking
Finally, we want to enable a few other features, which are located in the Kernel Hacking section. Make sure the Kernel Debugging and Magic SysRq key boxes are checked. Leave everything else as is.
Other Changes
If you wish to make other changes (cautiously), then you may. Generally, the only penalty for building something into the kernel instead of as a module is slower compile and load time. On the other hand, excluding a vital resource from the build can result in a kernel that won't boot properly.

Congratulations! You are done configuring the kernel. Save the configuration and quit from the configuration tool. The next step is to compile the kernel (as described in the next section).

Compiling the kernel

For documentation purposes, it is important to keep track of which kernel version we are using. Every Linux kernel is identified by four parts: the major number, minor number, and patch number (referred to above as x.y.z) plus an extra version string that should be unique and descriptive. The Mandrake distribution kernel we have been using has the extra version string 1-12mdk. We will label our altered kernel 1-12mdk-lab to distinguish it. To make this change, edit the first few lines of the Makefile so that they look as follows:

		VERSION = 2
		PATCHLEVEL = 6
		SUBLEVEL = 20
		EXTRAVERSION = csc262-lab

Now we're almost ready to do some compiling. The Makefile includes some preparatory commands that should be run first, to make sure everything is set up. From the root directory of the Linux source, issue the following commands:

# make clean
# make dep
# make bzImage

Each of these commands does something different: make clean will remove old compiled files, make dep will determine dependencies between modules, and, finally, make bzImage will create the kernel image. You will have to wait about 5-10 minutes for the last step to finish. (Note: In some extreme cases, you may also need to issue the command make mrproper before those shown above. This will remove all files except the actual source code, including your .config file -- make sure you have saved a backup of it first!)

After all the compiling is finished, you should have a freshly-baked Linux kernel waiting for you, made to order according to the specifications in your .config file. It will be stored in a subdirectory of the Linux source tree: arch/i386/boot/bzImage.

You're not done yet. In fact, the most time-consuming step still remains: compiling the modules that can extend the kernel.

# make modules
# make modules_install

Installing the kernel

In order to make the new kernel take control of the hardware, the kernel image needs to be copied to the /boot directory and renamed something descriptive: vmlinuz-2.6.20-csc262-lab. (By convention, linux kernel files begin with vmlinuz, followed by the version number.) The copy can be done in one step:

# cp arch/i386/boot/bzImage /boot/vmlinuz-2.6.20-csc262-lab

IMPORTANT: be careful not to overwrite other kernel images located under the /boot directory! Kernel images are named vmlinuz-*.

There are two more files that need to be copied into your boot directory. The first is the system symbol map, located at the root of the kernel build tree. Copy it into /boot, changing the name to match the kernel version we have been developing:

# cp System.map /boot/System.map-2.6.20-csc262-lab

The second file is a ramdisk for use during the boot process. (A ramdisk is a software simulation of an actual physical disk. It is used in the boot process to get the system running before the actual physical disk drives have been initialized.) You may need to install the mkinitrd program for this.

# yum install mkinitrd
# /sbin/mkinitrd /boot/initrd-2.6.20-csc262-lab.img 2.6.20-csc262-lab
# cp System.map /boot/System.map-2.6.20-csc262-lab

The last step is to register the new kernel with the boot loader. (The boot loader is the program responsible for the screen where you pick which OS to start up.) There are two boot loaders in use with Linux these days, called Grub (the newer one) and LILO (older but still in fairly wide use). One or the other was chosen during system install, probably Grub. They can be configured by editing a configuration file. They are located at /boot/grub/menu.lst for Grub, and /etc/lilo.conf for LILO.) In any case, once you have added an entry for the new lab kernel, you are ready to reboot using the new kernel.

To add an entry in Grub, look for lines that look like something this:

title Fedora Core (2.4.9-21)
        root (hd0,0)
        kernel /vmlinuz-2.4.9-21 ro root=/dev/hda6
        initrd /initrd-2.4.9-21.img

You will copy these and create a similar entry. Change the title to something descriptive (it will appear in the boot menu), edit the file names in the kernel and initrd lines to match the kernel version and other files you are creating, and leave everything else the same.

Try it !

Now it's time to try the new kernel. Reboot your computer and select the new kernel when prompted. You may see some error messages during the boot. We can ignore those error messages as long as the system does not crash.

If the system fails to boot, something is probably configured wrong. The messages that appear during the boot sequence can help you to track down what is wrong. Often, a Google search on a particular error message will put you on the right course.

Questions

  1. What is the latest unstable version of the Linux kernel?
  2. Suppose you have a Pentium 4 at home. How do you turn on support for that CPU in the kernel? Hint: use make xconfig.
  3. What is the "symmetric multi-processing support" (Processor Type and Features section)? Hint: remember the Help button...

Resources


Created by Emanuele Altieri and Nick Howe

Valid XHTML 1.0!   Powered by RedHat Linux