CSCI 2150 -- Laboratory Experiment
Linux Kernel Compile


If your installation of Linux included kernel development, you should have all of the resources necessary resources to recompile your operating system with options specific to your needs. This lab will take you step-by-step through that process. The commands in this lab assume that your are running Linux from the graphical user interface (GUI). If your system does not automatically come up in the GUI environment, simply enter the command startx after you've logged onto the system.

Begin by logging onto your system as root. Since most of the commands that we will be using are entered from a command line, we will need to open a terminal window. After your system opens into the X-Windows environment, select System Tools->Terminal from the menu. This will bring up a text window where you will enter most of the commands for this exercise.

Next, we need to move into the directory where the source code for the operating system is contained. In the terminal window, type the command:

cd /usr/src/linux

This will move you into the directory containing the resources for your kernel compile. Note that if you are given an error claiming that this directory does not exist, then take a listing of the directory using the ls command, and look for a directory labeled something like "linux-2.4.22". Replace the "linux" in the above command with the directory you found on your machine.

The kernel compile process is started by cleaning up any of the garbage left from previous kernel compiles. Type the command:

make clean

This will remove any old object files from previous builds. If necessary, you can do an even more extensive cleaning resetting all of the settings back to factory defaults using the command:

make mrproper

The next step is to configure the characteristics of the kernel that you are building. Remember that the reason we are doing this is to make a Linux kernel that is targeted to our specific needs. To do this, we need to modify the configuration stored in a large, complex text file named ".config". Don't worry, we won't be editing this file directly. Instead, we will be using tools that come with the kernel development files.

There are three methods for configuring the compilation of the kernel: make xconfig, make menuconfig, or make config. The make  xconfig configuration tool uses X-Windows and is probably the easiest to use. If you are running your terminal from X-Windows, type:

make xconfig

After a moment, a window with buttons defining each configuration category will appear. This window is controlled just like any window of a GUI interface.

-- Non-GUI Users Only --

If you are using a text-only terminal to do your compile, you will be using the command make  menuconfig. This option uses text-based menus to configure the kernel compilation and is the most reliable in terms of execution, but second in ease to make xconfig. make  config is the most difficult to use especially for newcomers. If you are using make  menuconfig because you do not have access to the GUI interface, type the following command:

make menuconfig

This should bring up a text window with a number of options to set for the compilation. Use the following keystrokes to manage this interface:

  • Left and right arrows: If you look at the bottom of the screen, you should see three menu items: <Select>, <Exit>, and <Help>. These menu items help you navigate through the options of the configuration menu. For example, if you decide that you want to select a configuration category to edit, use the left or right arrow keys to highlight <Select>. Pressing enter will select that category for editing. If you don't quite understand what a configuration item represents, use the left or right arrow keys to highlight <Help> and press Enter. This will bring up a description of the item.
  • Enter key: The enter key executes the menu item that has been highlighted.
  • Up and down arrows: The up and down arrow keys allow you to scroll through the configuration options. At the bottom of the
  • Esc key: The escape key exits the current screen and moves you up one level.

 

After running make  xconfig, you should see a window pop up with a matrix of buttons. Clicking on these buttons will allow you to configure certain characteristics of the kernel. For example, the third button from the top left corner is titled "Processor type and features." If you click on this, a second window opens with check boxes and some more buttons allowing you to create an operating system specifically for you processor's characteristics. On about the third row of this new window, you should see a button that displays a specific processor. Click on it and a drop down menu should appear allowing you to specify exactly which processor the kernel is intended for. Clicking on the button "Main Menu" will return you to the top window of the configuration program.

If you are interested, click on some of the buttons to see what kind of options there are to configure. Many of the options will be baffling to the novice, and some will even appear baffling to the expert. If, however, you find that your installation of Linux doesn't play sound properly, you might want to select the "Sound" button and configure the kernel specifically for your sound card.

Once you are finished with this screen, go back to the main menu and click on the button labeled "Save and Exit". The file we are saving is the .config file mentioned earlier. (We are not actually going to install the kernel that you build with this exercise, so the options you select will not be critical.) After you exit the configuration program, you can see the configuration file that you created by typing the command:

cat .config

Aren't you glad we didn't edit this file by hand?

Upon exiting the configuration, you should be told to do a "make dep". This verifies that the dependencies of the selected items in the configuration such as include files are where they need to be. Therefore, type:

make dep

Now we should be ready to compile the kernel. Typing the command

make bzImage

will compile the kernel to a file named bzImage the directory /usr/src/linux-2.4/arch/i386/boot. Note that Linux is case sensitive, so you need to capitalize the I in bzImage. After the long compile process is completed, type the following command to verify that the bzImage file (which should be very large) is placed in the proper directory:

ls -l /usr/src/linux-2.4/arch/i386/boot

At this point, we are going to stop actually entering commands to complete the kernel compile. The following instructions are listed here simply as a reference. Be careful when compiling and installing a kernel. It is very easy to mess up a working installation of Linux by changing the kernel. You should always have a startup disk when you do this.

Not all of the code for the operating system is contained in the bzImage file. Some of it is stored in dynamically loaded modules. The kernel uses these modules so that it doesn't have to load all of the operating system's code at one time. These include pieces code that are needed only occasionally or even rarely.

We need to create these modules for our custom kernel. To do this, type the command:

make modules

This process may take as long as the kernel compile did. Depending on how you set up your kernel compile, there may be more code in the modules than there is in the kernel itself.

Before we transfer these compiled modules to a location where the kernel will be able to access them, it would be a good idea to put our old modules out of harms way. The modules are contained in a directory called /lib/modules/kernelversion where kernelversion is the version of the kernel you are running. (For example, if your kernel's version is 2.4.18-14, then the directory your modules are contained in is /lib/modules/2.4.18-14) Use the mv command to rename your old modules directory so that they will be safe.

mv /lib/modules/kernelversion /lib/modules/kernelversion-old

where kernelversion is replaced with the version number for your existing kernel.

Once the old modules are safe, we can install the newly compiled modules. Do this with the command:

make modules_install

We should also put the old kernel itself in a safe place. The kernel is stored in the directory "/boot". If you move to this directory using the command cd  /boot, and take a listing with ls, you should see a large file with a name like vmlinuz-2.4.18-14. This is the file that we need to make safe. From the /boot directory, enter the command:

mv vmlinuz-kernelversion vmlinuz-kernelversion-old

where kernelversion is replaced with the version number of the file you found in your /boot directory.

Now we need to get the newly compiled kernel and put it where the boot-sequence can find it, specifically, we need to put it in your /boot directory. The following command should do this. (Note that for the purpose of making this lab easier, we are going to name the new kernel the same as the old kernel so that we don't have to change the boot loader's configuration. Typically, you will need to change the configuration of your boot loader, be it GRUB or LILO, to look for the new kernel's file name.)

cp /usr/src/linux/arch/i386/boot/bzImage /boot/vmlinuz-kernelversion

where the linux directory needs to be replaced with the directory you found your kernel development software in and kernelversion needs to be replaced with the version number of the old kernel.

All there is left to do now is cross your fingers and reboot. Good luck!