The Linux Kernel

From Colwiki.org

Revision as of 20:28, 4 July 2009 by Pwest (Talk | contribs)
(diff) ←Older revision | Current revision (diff) | Newer revision→ (diff)
Jump to: navigation, search


Outcomes

In this module the learner will learn about:
  1. The Modular Linux Kernel.
  2. Routine Kernel Recompilation.
  3. Manage Kernel Modules at Runtime
  4. Reconfigure, Build and Install a Custom Kernel and Module

There are two types of Kernel; A monolithic and Modular Linux Kernels. These are described below:

A: Monolithic

A monolithic kernel is one which has support for all hardware, network, and filesystem compiled into a single image file.

B: Modular

A modular kernel is one which has some drivers compiled as object files, which the kernel can load and remove on demand. Loadable modules are kept in /lib/modules.

The advantage of a modular kernel is that it doesn’t always need to be recompiled when hardware is added or replaced on the system. Monolithic kernels boot slightly faster than modular kernels, but do not outperform the modular kernel


Contents

The Modular Kernel

Many components of the Linux kernel may be compiled as modules which the kernel can dynamically load and remove as required. The modules for a particular kernel are stored in /lib/modules/<kernel-version>. The best components to modularise are ones not required at boot time, for example peripheral devices and supplementary file systems. Kernel modules are controlled by utilities supplied by the modutils package:

  lsmod
  rmmod
  insmod
  modprobe
  modinfo 

Many modules are dependant on the presence of other modules. A flat file database of module dependencies /lib/modules/<kernel version>/modules.dep is generated by the depmod command. This command is run by the rc.sysinit script when booting the system.

-- modprobe will load any module and dependent modules listed in modules.dep

-- /etc/modules.conf is consulted for module parameters (IRQ and IO ports) but most often contains a list of aliases. These aliases allow applications to refer to a device using a common name. For example the first ethernet device is always referred to as eth0 and not by the name of the particular driver.

Sample /etc/modules.conf file:

  alias eth0 e100
  alias usb-core usb-uhc
  alias sound-slot-0 i810_audio
  alias char-major-108 ppp_generic
  alias ppp-compress-18 ppp_mppe
  100Mbps full duplex
  options eth0 e100_speed_duplex=4

Routine Kernel Recompilation

Source extraction The kernel source is stored in the /usr/src/linux directory tree, which is a symbolic link to the /usr/src/(kernel-version) directory. When extracting a new kernel source archive it is recommended to:

  • remove the symbolic link to the old kernel source directory tree
     rm linux

Kernel sources which have been packaged as an RPM often create a link called linux-2-4

     tar xjf <the name of  source archive>
  • create a symbolic link called linux from the newly created directory
     ln -s linux-2.4.20 linux
  • The kernel is almost ready to be configured now, but first we need to make sure that all old binary files are cleared out of the source tree, and this is done with the make mrproper command.

Note: mrproper is a Scandinavian brand of cleaner that gets things “cleaner than clean”, it is one step beyond “make clean”.

Kernel Configuration

First edit the Makefile and make sure that the “EXTRAVERSION” variable is different from the existing version:

  VERSION = 2
  PATCHLEVEL = 4
  SUBLEVEL = 20
  EXTRAVERSION = -test

The kernel is now ready to be configured. This essentially means creating a configuration file called .config. This is done from the kernel source tree directory /usr/src/linux with any of the following

  make menuconfig 
  make xconfig 
  make config 

All these methods will save the configuration file as /usr/src/linux/.config

It is often easier to configure a new kernel using an older .config file by using the make oldconfig command. This will prompt the user only for new features in the kernel source tree (if the kernel is newer or has been patched).

Notice: Some distributions such as RedHat have a configs subdirectory containing files to be used as .config files with predefined configurations.


To enable kernel features (with make menuconfig) you will enter the top level category by moving with the arrow keys and pressing enter to access the desired category. Once in the particular category, pressing the space bar will change the kernel support for a feature or driver.

Possible support types are

  • supported (statically compiled) [*]
  • modular (dynamically compiled)[M]
  • not supported [ ]

The same choices are available with the other menu editors config and xconfig.

The make xconfig top level menu:

Kernel Compilation

make dep

Once the kernel configuration is complete, it is necessary to reflect these choices in all the subdirectories of the kernel source tree. This is done with the make dep command. Files called .depend containing paths to header files present in the kernel source tree (/usr/src/linux/include) are generated with the dep target..

make clean The make command gets instructions from the Makefile and will build what is needed. If some files are already present make will use them as is. In particular files with *.o extensions. To make sure that all the configuration options in .config are used to rebuild the files needed one has to run make clean (this deletes *.o files)

Notice: you do not need to do “make clean” at this stage if you already prepared the source directory with “make mrproper” The kernel itself is compiled compiled with one of the commands:

    make zImage
    make bzImage  

When the command exits without any errors, there will be a file in the /usr/src/linux/ directory called vmlinux. This is the uncompressed kernel.

The two other commands will write an additional file in /usr/src/linux/arch/i386/boot/ called zImage and bzImage respectively. These are compressed kernels using gzip and bzip2. See the next section Installing the New Kernel to find out how to proceed with these files.

make modules The modules are compiled with make modules.

make modules_install Once the modules are compiled they need to be copied to the corresponding subdirectory in /lib/modules. The make modules_install command will do that. The sequence of commands are depicted in Fig below.


Kernel compilation commands: make dep make clean make bzImage make modules make modules_install

Installing a New Kernel The new kernel can be found in /usr/src/linux/arch/i386/boot/bzImage, depending on your architecture of your system. This file must be copied to the /boot directory, and named vmlinuz-<full-kernel-version>

   /usr/src/linux/arch/i386/boot/bzImage /boot/vmlinuz-<full-kernel-version>

Next the /etc/lilo.conf or /boot/grub/grub.conf file needs to be edited to add our newly compiled kernel to the boot menu. Copy the “image” section from your existing kernel and add a new image section at the bottom of the file, as shown below:

   Editing the /etc/lilo.conf file
   Prompt
   timeout=50
   message=/boot/message
   image=/boot/vmlinuz
          label=linux
                root=/dev/hda6                      Existing section
                          read-only
   image=/boot/vmlinuz-<full-kernel-version>
           label=linux-new                   Added section
           root=/dev/hda6
           read-only
 ----------snip-------------------------------

The symbol table for the various kernel procedures can be copied to the /boot directory:

   cp /usr/src/linux/System.map /boot/System.map-<full-kernel-version>

The full kernel version

On a system, the version of the running kernel can be printed out with

   uname -r

This kernel version is also displayed on the virtual terminals if the \k option is present in /etc/issue.

Initial Ramdisks If any dynamically compiled kernel modules are required at boot time (e.g a scsi driver, or the filesystem module for the root partition) they will be loaded using an initial ramdisk.

The initial ramdisk is created with the mkinitrd command which only takes two parameters: the filename, and the kernel version number.

If you use an initial ramdisk then you will need to add an initrd= line in your /etc/lilo.conf

Example:

   mkinitrd /boot/initrd-$(uname -r).img $(uname -r)

Optional It is recommended to copy the /usr/src/linux/.config file to /boot/config-<fiull-kernel-version>, just to keep track of the capabilities for the different kernels that have been compiled. Rerunning LILO Finally lilo needs to be run in order to update the boot loader . First lilo can be run in test mode to see if there are any errors in the configuration file:

NOTICE The LILO bootloader needs to be updated using lilo everytime a changed is made in /etc/lilo.conf

   mkinitrd /boot/initrd-$(uname -r).img $(uname -r)


Summary

With the modern distributions you may not require to recompile your kernel but in case you would like to add new functionality or kernel modules, then this becomes and important topic for you.




Assignment

Before starting with the exercises make sure you don’t have an existing kernel tree in /usr/src/. If you do, pay attention to the /usr/src/linux symbolic link.


Manually recompile the kernel following the compilation steps.

  • Get the kernel-version.src.rpm package from rpmfind or a CD. Installing this package will also give you a list of dependencies, such as the gcc compiler or binutils package if they haven't yet been met.
  • Install the package with –i (this will put all the code in /usr/src/ )
  • Go into the /usr/src/linux-version directory and list the configs directory
  • Copy the kernel config file that matches your architecture into the current directory and call it .config
  • Run

make oldconfig at the command line to take into account this new .config file.

  • Edit the Makefile and make sure the version is not the same as your existing kernel. You can get information on your current kernel by running uname –a at the command line or list the /lib/modules directory.
  • Run

make menuconfig (or menu or xconfig) and remove ISDN support from the kernel.

  • When you exit the above program the .config file is altered but the changes have not yet taken place in the rest of the source tree. You next need to run

make dep - Finally to force new object files (.o) to be compiled with these changes you delete all previously compiled code with make clean - You can now build the kernel the modules and install the modules with: make bzImage modules modules_install - The modules are now installed in the /lib/modules/version directory. The kernel is called bzImage and is in the following directory:

     /usr/src/linux/arch/i386/boot/ 

We need to manually install this kernel (2 steps):

  • cp /usr/src/linux/arch/i386/boot/bzImage /boot/vmlinuz-<full-kernel-version>
  • That was easy! now edit /etc/lilo.conf and add an ‘image’ paragraph that will tell LILO where to find this kernel and the root filesystem.
  • Run /sbin/lilo and reboot

Since we downloaded the kernel-version.src.rpm package we can now use this package to recompile a ‘RedHat preconfigured’ kernel. Notice that although no intervention is needed you won’t be able to change the .config menu.

  • First rebuild the compiled binary package with
     rpm --rebuild kernel-version.src.rpm     (...wait!)
  • This will eventually generate the kernel-version.i368.rpm in /usr/src/redhat/RPMS/i386/.
  • Next, upgrade you kernel with the RPM manager using the –U option.


Image:somerights20.png This work is licenced under a Creative Commons - By Attribution Licence - Share Alike License.

Personal tools
News & Events