HOW TO REBUILD A LINUX KERNEL MODULES FROM SRPM (c) 2006 Hiro Sugawara It often happens that I need to create a new driver kernel module for my Linux servers running versions of Fedora Core. The kernel source package installed during the binary distribution installation under /usr/src/kernels does not help any more than referencing include files becasue there are no *.c file to compile. Neither does the tarball from kernel.org because it is just a generic plain kernel and does not have the spec file (or whatever necessary) for the particular builds running on my servers. Here's how to make it possible to build a new kernel module using build-specific source tree from an SRPM. The following uses the latest Fedora Core 5 as of writing to show examples. This document was originally derived from http://www.bonsai.com/ken/ace_tao_rpm/srpm_howto.txt. Create personal RPM work area ============================= The default RPM configuration assumes host-wide operations performed by root. To avoid building things as root... $ mkdir -p ~/rpm/{BUILD,RPMS,SOURCES,SPECS,SRPMS,tmp} $ cat > ~/.rpmmacros %_topdir %(echo ${HOME}/rpm) %_tmppath %{_topdir}/tmp %packager Firstname Lastname # Have built RPMs land in RPMS/ instead of RPMS// %_rpmfilename %%{NAME}-%%{VERSION}-%%{RELEASE}.%%{ARCH}.rpm ^D Install SRPM ============ To download the SRPM from one of mirros and project in ~/rpm/SOURCES... $ rpm -U ftp://mirror.linux.duke.edu/pub/fedora/linux/core/updates/5/SRPMS/kernel-2.6.17-1.2187_FC5.src.rpm The ~/rpm/SOURCE directory will get populated with a bunch of files--do not worry about them. The SRPM spec file will take care of them... Rename the spec file if necessary. This may be useful when one has to deal with multiple kernel builds. $ cd ~/rpm/SPEC $ mv kernel-2.6.spec kernel-2.6.17-1.2187_FC5.spec Set up build tree ================= To create a compilable build tree under ~/rpm/BUILD... $ rpmbuild -bp --target i686 ~/rpm/SPECS/kernel-2.6.17-1.2187_FC5.spec Rename the directory if necessary for the same reason as the spec file. $ cd ~/rpm/BUILD $ mv kernel-2.6.17 kernel-2.6.17-1.2187_FC5 Pick up the right config file ============================= $ cd ~/rpm/BUILD/kernel-2.6.17-1.2187_FC5/linux-2.6.17.i686 $ cp configs/kernel-2.6.17-i686.config .config Build target modules ==================== $ cat Makefile ifneq ($(KERNELRELEASE),) # kbuilt portion obj-m := mymod.o mymod-y := mymod-1.o mymod-2.o else # normal portion KDIR := $(HOME)/rpm/BUILD/kernel-2.6.17-1.2187_FC5/linux-2.6.17.i686 all: myprog mymod.ko myprog: myprog-1.o myprog-2.o mymod.ko: $(MAKE) -C $(KDIR) M=`pwd` clean: $(MAKE) -C $(KDIR) M=`pwd` clean endif