-->
Previous Table of Contents Next


Compiling the Kernel from Source Code

Upgrading, replacing, or adding new code to the kernel is usually a simple process. You obtain the code for the kernel, make any configuration changes, compile it, then place the code in the proper location on the file system to run the system properly. The process is often automated for you by a shell script or installation program, and some upgrades are completely automated with no need to do anything more than start the upgrade utility.

Kernel sources for new releases of Linux are available from CD-ROM distributions, FTP sites, user groups, and many other locations. Most kernel versions are numbered with a version and a patch level, so you will see kernel names like 1.12.123 where “1” is the major release, “12” is the minor version release, and “123” is the patch number. Most sites of kernel source maintain several versions simultaneously, so check through the source directories for the latest version of the kernel.

Patch releases are sometimes numbered differently and do not require the entire source of the kernel to install. In most cases, the patch will overlay a section of existing source code, and a simple recompilation is all that’s necessary to install the patch. Patches are released quite frequently.


Note:  
When you are installing a patch it you might want to reduce the number of open files, applications, and running processes to a minimum. This prevents problems with files being left open and corrupting the I-node table. Because patches are installed when you are logged in as root, you have the ability to kill all unnecessary applications and processes.

Most kernel source programs are maintained as a gzipped tar file. Unpack the files into a subdirectory of /usr/src, which is where most of the source code is kept for Linux. Some versions of Linux keep other directories for the kernel source, so you may want to check any documentation supplied with the system or look for a README file in the /usr/src directory for more instructions.

Often, unpacking the gzipped tar file in /usr/src creates a subdirectory called /usr/src/linux, which can overwrite your last version of the kernel source. Before starting the unpacking process, rename or copy any existing /usr/src/linux (or whatever name is used with the new kernel) so that you have a backup version in case of problems.

After the kernel source has been unpacked, you need to create two symbolic links to the /usr/include directory, if they are not created already or set by the installation procedure. Usually, the link commands required are


ln -sf /usr/src/linux/include/linux /usr/include/linux

ln -sf /usr/src/linux/include/asm /usr/include/asm

If the directory names are different on your version of Linux, substitute them for /usr/src/linux. Without these links, the upgrade or installation of a new kernel cannot proceed.

After the source code has been ungzipped and untarred and the links have been established, the compilation process can begin. You must have a version of gcc or g++ (the GNU C and C++ compilers) or some other compatible compiler available for the compilation. You may have to check with the source code documentation to make sure you have the correct versions of the compilers because occasionally new kernel features are added that are not supported by older versions of gcc or g++.

Check the file /usr/src/linux/Makefile (or whatever path the Makefile is in with your source distribution). There will be a line in the file that defines the ROOT_DEV, the device that is used as the root file system when Linux boots. Usually the line looks like this:


ROOT_DEV = CURRENT

If you have any other value, make sure it is correct for your file system configuration. If the Makefile has no value, set it as shown above.

The compilation process begins by going to the /usr/src/linux directory and issuing the command


make config

which invokes the make utility for the C compiler. The process may be slightly different for some versions of Linux, so check with the release or installation notes supplied with the source code.

Next, the config program issues a series of questions and prompts for you to answer to indicate any configuration issues that need to be completed before the actual compilation is started. These may be about the type of disk drive you are using, the CPU, any partitions, or other devices such as CD-ROMs. Answer the questions as well as you can. If you are unsure, choose the default values or the one that makes the most sense. The worst case scenario is that you have to redo the process if the system doesn’t run properly. (You do have an emergency boot disk ready, don’t you?)

Now you have to set all the source dependencies. This is a commonly skipped step and can cause a lot of problems if not performed for each software release. Issue thecommand


make dep

If the software you are installing does not have a dep file, check with the release or installation notes to ensure the dependencies are correctly handled by the other steps.

After that, you can finally compile the new kernel. The command to start the process is


make Image

which compiles the source code and leaves the new kernel image file in the current directory (usually /usr/src/linux). If you want to create a compressed kernel image, use the command


make zImage

Not all release or upgrades to the kernel support compressed image compilation.

The last step in the process is to copy the new kernel image file to the boot device or a boot floppy disk. Use the command:


cp Image /dev/fd0

to place it on a floppy. Use a different device driver as necessary to place it elsewhere on the hard drive file system. Alternatively, if you plan to use LILO to boot the operating system, you can install the new kernel by running a setup program or the utility /usr/lilo/lilo. See Chapter 4, “Using LILO.”

Now all that remains is to reboot the system and see if the new kernel loads properly. If there are any problems, boot from a floppy, restore the old kernel, and start the process again. Check documentation supplied with the release source code for any information about problems you may encounter or steps that may have been added to the process.


Previous Table of Contents Next