Phil Perry | www.pendre.co.uk

Building kernel modules: Part 3

July 24th, 2008

Previously we have seen how we can build kernel modules for CentOS (or RHEL) out of tree (Part 1; May 30th, 2008) or as DKMS modules (Part 2; July 18th, 2008). In this final part we will look at building kmod kernel modules packaged in RPM format and again we will use the coretemp module as an example.

The first thing we need to do is make sure we have all the kernel-devel packages installed for the currect kernel, including any xen and PAE variants. Next create a working directory containing the project source code and Makefile and create the Kbuild file by copying the Makefile. Finally, create a bzip2 compressed tarball of the source directory and copy the compressed tarball to the /SOURCES directory within your build environment.

[buildsys@Quad]$ rpm -qa kernel\* | sort
kernel-2.6.18-92.1.6.el5.x86_64
kernel-devel-2.6.18-92.1.6.el5.x86_64
kernel-headers-2.6.18-92.1.6.el5.x86_64
kernel-xen-devel-2.6.18-92.1.6.el5.x86_64
[buildsys@Quad]$ mkdir /usr/src/coretemp-1.0kmod/
[buildsys@Quad]$ cp coretemp.c Makefile /usr/src/coretemp-1.0kmod/
[buildsys@Quad]$ cd /usr/src/coretemp-1.0kmod
[buildsys@Quad]$ cp Makefile Kbuild
[buildsys@Quad]$ cd ..
[buildsys@Quad]$ tar -cf coretemp-1.0kmod.tar coretemp-1.0kmod/
[buildsys@Quad]$ bzip2 coretemp-1.0kmod.tar
[buildsys@Quad]$ cp coretemp-1.0kmod.tar.bz2 /usr/src/buildsys/SOURCES/
[buildsys@Quad]$

Now we make a copy the kmodtool script (located at /usr/lib/rpm/redhat/kmodtool) called kmodtool-coretemp in the SOURCES directory and edit it to include any dependencies for our kmod RPM package (thanks to Johnny Hughes for the tip). Creating a custom kmodtool script isn't strictly needed for building kmod packages if you don't need to specify any dependencies but I was unable to find a convenient way to pass package dependencies from the SPEC file to the kmodtool build script. Below is an excerpt of my kmodtool-coretemp script showing an additional dependency for lm_sensors >= 2.10.2 which may be obtained from ATrpms.

Requires(post): /sbin/depmod
Requires(postun): /sbin/depmod
Requires: lm_sensors >= 2.10.2
EOF

Finally we make a SPEC file for the project named coretemp-kmod.spec file in the /SPECS directory of our build environment:

# Sources:
Source0: coretemp-1.0kmod.tar.bz2
Source10: kmodtool-coretemp

# If kversion isn't defined on the rpmbuild line, build against the current kernel.
%{!?kversion: %define kversion %(uname -r)}

%define kmod_name coretemp
%define kmodtool sh %{SOURCE10}
%define kverrel %(%{kmodtool} verrel %{?kversion} 2>/dev/null)
%define kmodrel %(echo %{kverrel} | sed 's/2.6.18-//')

%define basevar ""
%ifarch i686
%define paevar PAE
%endif
%ifarch i686 x86_64
%define xenvar xen
%endif

%{!?kvariants: %define kvariants %{?basevar} %{?xenvar} %{?paevar}}

Name: %{kmod_name}-kmod
Version: 1.0
Release: 4.%{kmodrel}
Summary: Coretemp 1.0 CentOS-5 module
License: GPL v2
Group: System Environment/Kernel
URL: http://www.kernel.org/

BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
ExclusiveArch: i686 x86_64

%description
This package provides the coretemp kernel module for monitoring the core temperature
of Intel Core 2 Duo and Quad Core processors with the CentOS 5.2 series kernels.

# Magic hidden here:
%define kmp_version %{version}
%define kmp_release %{release}
%{expand:%(%{kmodtool} rpmtemplate_kmp %{kmod_name} %{kverrel} %{kvariants} 2>/dev/null)}

%prep
%setup -q -c -T -a 0
for kvariant in %{kvariants} ; do
cp -a coretemp-1.0kmod _kmod_build_$kvariant
done

%build
for kvariant in %{kvariants} ; do
ksrc=%{_usrsrc}/kernels/%{kverrel}${kvariant:+-$kvariant}-%{_target_cpu}
pushd _kmod_build_$kvariant
make -C "${ksrc}" modules M=$PWD
popd
done

%install
export INSTALL_MOD_PATH=$RPM_BUILD_ROOT
export INSTALL_MOD_DIR=extra/%{kmod_name}
for kvariant in %{kvariants} ; do
ksrc=%{_usrsrc}/kernels/%{kverrel}${kvariant:+-$kvariant}-%{_target_cpu}
pushd _kmod_build_$kvariant
make -C "${ksrc}" modules_install M=$PWD
popd
done

%clean
rm -rf $RPM_BUILD_ROOT

%changelog
* Tue Jul 15 2008 Alan J Bartlett
- Fixed bugs in spec file. 1.0-4.92.1.6.el5

* Sat Jul 12 2008 Philip J Perry
- Fixed dependencies. 1.0-3.92.1.6.el5

* Tue Jul 08 2008 Philip J Perry
- Fixed bug in spec file. 1.0-2.92.1.6.el5

* Tue Jul 08 2008 Philip J Perry
- Initial RPM build. 1.0-1.92.1.6.el5

Now we're ready to build our kmod packages using rpmbuild:

[buildsys@Quad SPECS]$ rpmbuild -bb --target=`uname -m` coretemp-kmod.spec
[buildsys@Quad SPECS]$

which will produce a set of kmod-coretemp RPMs in your /RPMS directory under the appropriate architecture. Additionally, our SPEC file allows us to specify from the command line at build time which kernel version to build against (e.g, --define 'kversion 2.6.18-92.el5') or which kernel-variant kmod packages to build (e.g, --define 'kvariants ""' would only build the base kernel package).

kmod coretemp packages for CentOS 5 may be downloaded below and installed with rpm:

http://www.pperry.f2s.com/linux/coretemp/

Alan has kindly updated the CentOS Wiki page on building kernel modules and I'd like to thank both Alan Bartlett and Akemi Yagi for helpful discussions, testing and encouragement whilst preparing this series of articles.