Sunday, October 20, 2013

Fedora: Compile a single module directory for current running Kernel

Sometimes one may want to just compile a single module and use it without rebooting the machine. In my case I wanted to play with 8139cp/8139too modules inside a KVM virtual machine running Fedora.

There are two requirements:
1 - Kernel source code
2 - Kernel development package

The source code is where to do the changes, while the development package contains the headers that allow to compile Kernel modules with the right version magic.

There are some options for obtaining the Kernel source code, but here the Fedora way is listed. You could probably download the Kernel from

Install packages, prepare RPM buil tree, and "install" the source code(Fedora way):
[fedora] $ sudo yum install @"Development Tools" rpmdevtools yum-utils ncurses-devel
[fedora] $ rpmdev-setuptree
[fedora] $ yumdownloader --source kernel
[fedora] $ sudo yum-builddep kernel-<version>.src.rpm
[fedora] $ rpm -Uvh kernel-<version>.src.rpm
Prepare the source code for building:
[fedora] $ cd ~/rpmbuild/SPECS
[fedora] $ rpmbuild -bp --target=$(uname -m) kernel.spec

Install kernel-devel:
[fedora] $ sudo yum install kernel-devel

Build Realtek Ethernet modules:
[fedora] $ cd ~/rpmbuild/BUILD/kernel-<version>/linux-<version>
[fedora] $ make -C /lib/modules/`uname -r`/build M=`pwd`/drivers/net/ethernet/realtek modules

Remove original mudules, load new ones:
[fedora] $ sudo rmmod 8139cp
[fedora] $ sudo rmmod 8139too
[fedora] $ sudo insmod ~/rpmbuild/BUILD/kernel-<version>/linux-<version>/drivers/net/ethernet/realtek/8139cp.ko
[fedora] $ sudo insmod ~/rpmbuild/BUILD/kernel-<version>/linux-<version>/drivers/net/ethernet/realtek/8139too.ko

Nice resources:
 - Building a custom kernel


  1. Thanks for this. It was the only page I could find with information on how to create a patched Fedora module.

    However, I get an error at the compile step, saying that some include files can't be found. The makefile in the module source directory includes this extra include directory, but for some reason it's not being done from the top-level makefile that's being run.

  2. Mark, for the command to work, you need to be on the root directory of the kernel source. Can't imagine other issues.

  3. The only way I could get it to work Peter was to run without the -C makefile option.

    Plus before the first make of a specific module directory would work, I had to run a complete "make modules", which can be interrupted as soon as the first module starts compiling.