Kernel Module HOWTO

On Red Hat Enterprise Linux, it is sometime desireable to compile a kernel module that is not included with the standard distribution. This document describes the basic procedure.

If you subsequently upgrade your kernel, you must rebuild any modules you have created using this method. Modules are stored in a version specific directory, so the module will not be available after an upgrade until you build it again.

Prerequisites

You will need a stock Red Hat kernel installed as well as the appropriate kernel-devel package. For example, on an SMP system, the following two packages should be installed (the version number may be different of course).

  • kernel-smp-2.6.9-34.EL
  • kernel-smp-devel-2.6.9-34.EL

Module Source

In addition, you need the source for the module you wish to compile. With RHEL, the easiest way to get the correct source for your current kernel is with up2date as follows.

[root@coma ~] up2date --get-source kernel-smp

This will put the source RPM in /var/spool/up2date. Install it with RPM.

[root@coma ~] rpm -ivh /var/spool/up2date/kernel-2.6.9-34.EL.src.rpm

Now run rpmbuild to extract and prepare the kernel sources. The prepare phase of rpmbuild will extract the source from the archive, then apply all of the Redhat patches.

[root@coma ~] cd /usr/src/redhat/SPECS/
[root@coma BUILD] rpmbuild -bp kernel-2.6.spec

On an i386 architecture system, you will get an error telling you that the i386 platform is not supported. Just add –target=i686 to the command. This only affect the preparation so it is safe to do so.

You will find the complete kernel source in /usr/src/redhat/BUILD/kernel-2.6.9 (the exact directory might vary if your kernal version is different).

Copy the source for the module that you wish to compile into the kernel development tree.

[root@coma ~] uname -r
2.6.9-34.ELsmp
[root@coma ~] cp -r /usr/src/redhat/BUILD/kernel-2.6.9/linux-2.6.9/fs/reiserfs/. \
    /lib/modules/$(uname -r)/build/fs/reiserfs/

Now you have to run menuconfig and enable the module (reiserfs in this case).

[root@coma ~] cd /lib/modules/$(uname -r)/build
[root@coma build] make menuconfig

Some modules have options that can be set in menuconfig. For example, the NTFS file system allows you to turn on the ability to write to NTFS volume. You will want to carefully review the options for the module you are compiling to assure that they are set appropriately for your needs.

Compile the Module

Now you can compile the module.

[root@coma ~] cd /lib/modules/$(uname -r)/build
[root@coma build] make SUBDIRS=fs/reiserfs/ modules

Now the module should be compiled. You will find the module in the directory where the source is located.

Install the Module

Depending on the module, you may have to create a new directory. In this example, the reiserfs directory doesn’t exists. We create it, copy in the module, change it’s permissions and run depmod to update the module list.

[root@coma ~] cd /lib/modules/$(uname -r)
[root@coma 2.6.9-34.ELsmp] mkdir kernel/fs/reiserfs
[root@coma 2.6.9-34.ELsmp] cp build/fs/reiserfs/reiserfs.ko kernel/fs/reiserfs/
[root@coma 2.6.9-34.ELsmp] chmod 744 kernel/fs/reiserfs/reiserfs.ko
[root@coma 2.6.9-34.ELsmp] depmod

Testing the Module

Now you can load the module with modprobe.

[root@coma ~] modprobe reiserfs

If all goes according to plan, the module should load and be usable.

References

The information above was inspired by the following site.

Installing RHEL4 on Systems with Legacy Megaraid Drivers

This site is geared more at adding a third party driver which is also a very useful thing.

 
howto/modules.txt · Last modified: 2006/06/22 23:11 by dpotter