Building a Custom Kernel


Many people new to Linux often ask, “Why should I build my own kernel?” Given the advances that have been made in the use of kernel modules, the most appropriate response to that question is, “Unless you already know why you need to build your own kernel, you probably do not need to.” In the past, you had to recompile the kernel if you added new hardware on your system. In other words, the kernel was static. Improvements in the Linux 2.0.x kernels allowed for many hardware drivers to be modularized into components that are loaded on demand. However, major problems existed when users had multiple kernels that had been compiled for different configuration options on their system — for example, SMP versus UP kernels. Further Linux 2.4.x kernel modularization advancements allow for multiple kernels to coexist more easily, but they cannot share modules. Unless you are recompiling a customized kernel for your system, you will not see many changes in how kernel modules are handled.

Building a Modularized Kernel

The instructions in this section apply to building a modularized kernel. If you are interested in building a monolithic kernel instead, see the section “Building a Monolithic Kernel.” The following steps will guide you through building a custom kernel for the x86 architecture:

Note

This example uses 2.4.18-7.95 as the kernel version. Your kernel version might differ. To determine your kernel version, type the command uname -r. Replace 2.4.18-7.95 with your kernel version.

  1. The most important step is to make sure that you have a working emergency boot diskette in case you make a mistake. If you did not make a boot diskette during the installation, use the mkbootdisk command to make one now. The standard command is similar to mkbootdisk --device /dev/fd0 2.4.x (where 2.4.x is the full version of your kernel, such as 2.4.18-7.95). When done, test the boot disk to make sure that it will boot the system.

  2. You must have the kernel-source package installed. Type rpm -q kernel-source to determine the package version, if it is installed. If it is not installed, install it from one of the Red Hat Linux CD-ROMs or the Red Hat FTP site available at ftp://ftp.redhat.com (a list of mirrors is available at http://www.redhat.com/mirrors.html). Refer to Chapter 6 for information on installing RPM packages.

  3. Open a shell prompt and change to the directory /usr/src/linux-2.4. All commands from this point forward must be executed from this directory.

  4. It is important that you begin a kernel build with the source tree in a known condition. Therefore, it is recommended that you begin with the command make mrproper. This will remove any configuration files along with the remains of any previous builds that may be scattered around the source tree. If you already have an existing configuration file that works (/usr/src/linux-2.4/.config) and you want to use it, back it up to a different directory before running this command and copy it back afterwards.

  5. Now you need a configuration file that will determine which components to include in your new kernel. If you are running the X Window System, the recommended method is to use the command make xconfig. Components are listed in different levels of menus and are selected using a mouse. You can select Y (yes), N (no), or M (module). After choosing your components, click the Save and Exit button to create the configuration file /usr/src/linux-2.4/.config and exit the Linux Kernel Configuration program.

If you want to use the settings of a default Red Hat Linux kernel, copy the the configuration file from the /usr/src/linux-2.4/configs directory to /usr/src/linux-2.4/.config. Then, run the make xconfig command and make only the desired changes. Be sure to save your changes to the configuration file. Other available methods for kernel configuration are listed below:

  • make config — An interactive text program. Components are presented in a linear format and you answer them one at a time. This method does not require the X Window System and does not allow you to change your answers to previous questions.

  • make menuconfig — A text-mode, menu-driven program. Components are presented in a menu of categories; you select the desired components in the same manner used in the text-mode Red Hat Linux installation program. Toggle the tag corresponding to the item you want included: [*] (built-in), [ ] (exclude), M (module), or (module capable). This method does not require the X Window System.

  • make oldconfig — This is a non-interactive script that will set up your configuration file to contain the default settings. If you are using the default Red Hat Linux kernel, the script will create a configuration file for the kernel that shipped with Red Hat Linux for your architecture. This is useful for setting up your kernel with known working defaults and then turning off features that you do not want.

Note

To use kmod and kernel modules, you must answer Yes to kmod support and module version (CONFIG_MODVERSIONS) support during the configuration.

  1. After creating a /usr/src/linux-2.4/.config file, use the command make dep to set up all the dependencies correctly.

  2. Use the command make clean to prepare the source tree for the build.

  3. It is recommended that you give the custom kernel you are building a modified version number so that you do not overwrite your existing kernel. The method described here is the easiest to recover from in the event of a mishap. If you are interested in other possibilities, details can be found at http://www.redhat.com/mirrors/LDP/HOWTO/Kernel-HOWTO.html or in the Makefile in /usr/src/linux-2.4. By default, /usr/src/linux-2.4/Makefile includes the word custom at the end of the line beginning with EXTRAVERSION. Appending the string will allow you to have the old working kernel and the new kernel, version 2.4.18-7.95custom, on your system at the same time. To give the kernel an unique name, you can also append the date to the end of the string.

  4. Build the kernel with make bzImage.

  5. Build any modules you configured with make modules.

  6. Use the command make modules_install to install the kernel modules (even if you did not build any). Make sure that you type the underscore (_). This will install the kernel modules into the directory path /lib/modules/KERNELVERSION/kernel/drivers (where KERNELVERSION is the version specified in the Makefile). In the example it would be /lib/modules/2.4.18-7.95custom/kernel/drivers/.

  7. Use make install to copy your new kernel and its associated files to the proper directories. In addition to installing the kernel files in the /boot directory, this command also executes the /sbin/new-kernel-pkg script, which builds a new initrd image and adds new entries to the boot loader configuration file. If you have a SCSI adapter and you compiled the SCSI driver as a module or if you built your kernel with ext3 support as a module (the default in Red Hat Linux), the initrd image is required.

  8. Even though the initrd image and boot loader modifications are made for you, verify that they were done correctly.

Making an initrd Image

An initrd image is needed to load a SCSI module at boot time or if you compiled the kernel with ext3 support as a module. To verify that a new initrd file was created, view the contents of the /boot directory. You should see the file initrd-2.4.18-7.95custom.img, where 2.4.18-7.95custom is the name of the kernel you just built. If it does not exist, use the /sbin/mkinitrd shell script to create it:

/sbin/mkinitrd /boot/initrd-2.4.18-7.95custom.img 2.4.18-7.95custom

In the above example, /boot/initrd-2.4.18-7.95custom.img is the file name of the new initrd image. 2.4.18-7.95custom is the kernel whose modules (from /lib/modules) should be used in the initrd image. This is not necessarily the same as the version number of the currently running kernel.

Building a Monolithic Kernel

To build a monolithic kernel (that is, a kernel which does not use modules), follow the same steps as building a modularized kernel, with a few exceptions.

  • When configuring the kernel, do not compile anything as a module. In other words, answer only Yes or No to the questions. Also, answer No to kmod support and module version (CONFIG_MODVERSIONS) support.

  • Omit the following steps:

    • make modules

    • make modules_install

  • Edit lilo.conf to include the line append=nomodules or append nomodules to the kernel line in grub.conf.




Official Red Hat Linux Administrator's Guide
Official Red Hat Linux Administrators Guide
ISBN: 0764516957
EAN: 2147483647
Year: 2002
Pages: 278
Authors: Red Hat Inc

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net