How to compile a single module in Ubuntu linux

Occasionally you may experience problems in a specific kernel module that requires a patch.  Once patched, you can follow these directions to compile only the module affected, saving a bunch of time.

Assume the following, for this example:

  • You are building a module for Ubuntu 3.2.0-21-generic kernel.
  • Your source tree with the patched module is located in /usr/src/linux-source-3.2.0
  • Your linux header source is located in /usr/src/linux-headers-3.2.0-21-generic
  • Your working folder ( where you are building the new module )   is /usr/src/mytree

In this case, your EXTRAVERSION is -21-generic ( this is the specific flavor of the 3.2.0 kernel we are building ) this is important to note, and will be used later in this example.

Now to create your working folder and change into it:

mkdir /usr/src/mytree
cd /usr/src/mytree

Now that you are in your working folder, you need to copy over your kernel configuration file and Module.symvers file.

cp /boot/config-`uname -r`  .config
cp /usr/src/linux-headers-3.2.0-21-generic/Module.symvers .

Now with those files in place, you need to change to your source folder and prep your work area by running a few make commands.  These commands make use of the EXTRAVERSION we noted above:

cd /usr/src/linux-source-3.2.0
make EXTRAVERSION=-21-generic O=/usr/src/mytree oldconfig
make EXTRAVERSION=-21-generic O=/usr/src/mytree prepare
make EXTRAVERSION=-21-generic O=/usr/src/mytree outputmakefile
make EXTRAVERSION=-21-generic O=/usr/src/mytree archprepare
make EXTRAVERSION=-21-generic O=/usr/src/mytree modules SUBDIRS=scripts

At this point you are ready to compile the module you have patched.  Suppose it is the drivers/net/wireless/rt2x00 module:

make EXTRAVERSION=-21-generic O=/usr/src/mytree modules SUBDIRS=drivers/net/wireless/rt2x00

At this point you should have a drivers/net/wireless/rt2x00 folder in your working folder ( /usr/src/mytree ) which contains the .ko module files.

Simply backup the old modules in /lib/modules/3.2.0-21-generic/kernel/drivers/net/wireless/rt2x00 , and replace them with your new ones, then reload or reboot to test the new modules!

Good Luck!

Bookmark the permalink.
There are to How to compile a single module in Ubuntu linux

  1. Thank you for this guide! I wanted to debug the i915 module on my Thinkpad X30 running Xubuntu 12.04 but couldn’t find any up to date instructions on compiling a signle kernel module. Following your guide everything worked like a charm!

    Just in case someone might also be interested in messing up with i915, here’s what I did

    sudo apt-get build-dep --no-install-recommends linux-image-$(uname -r)
    apt-get source linux-image-$(uname -r)
    mkdir ~/my_i915
    cd ~/my_i915
    cp /usr/src/linux-headers-3.2.0-24-generic/Module.symvers .
    cp /boot/config-`uname -r` .config
    cd ~/linux-3.2.0/
    make EXTRAVERSION=-24-generic O=~/my_i915 oldconfig
    make EXTRAVERSION=-24-generic O=~/my_i915 prepare
    make EXTRAVERSION=-24-generic O=~/my_i915 outputmakefile
    make EXTRAVERSION=-24-generic O=~/my_i915 archprepare
    make EXTRAVERSION=-24-generic O=~/my_i915 modules SUBDIRS=scripts
    vim drivers/gpu/drm/i915/intel_display.c
    make EXTRAVERSION=-24-generic O=~/my_i915 modules SUBDIRS=drivers/gpu/drm/i915
    sudo cp ~/my_i915/drivers/gpu/drm/i915/i915.ko /lib/modules/3.2.0-24-generic/kernel/drivers/gpu/drm/i915/
    sudo depmod -a
    sudo update-initramfs -u


    • Vladyslav,

      Thanks so much for this. I was looking for something almost exactly like this, but for the radeon driver under ubuntu 13.04. I adapted your instructions for the radeon and it worked great.

      I really appreciate that you posted this.

      Take care.


    • Thanks Vladyslav! I’m using this as a recipe for experimenting with the sata_sil driver.


  2. Pingback October 14, 2012: Compiling a single kernel module - Dogs | Dogs

  3. Vladyslav, thanks for sharing – I am also having a problem with i915 and I need to compile it. Will give a try – hope your instructions will help me also 🙂


  4. Pingback February 24, 2014: Linux: Apple Wireless A1314 Fn Key Not Registered, Looks Like Software Bug | Click & Find Answer !

  5. This actually made my day. Now I have a “working” driver for my cx231xx based usb capture-device.

    According to http://streibelt.de/blog/2013/06/23/kernel-patch-for-cx23103-video-grabber-linux-support/

    I ended up editing “drivers/media/usb/cx231xx/cx231xx-cards.c

    with…

    So in drivers/media/video/cx231xx/cx231xx-cards.c go to struct usb_device_id cx231xx_id_table[] and add these two lines e.g. at the beginning of the definition:

    {USB_DEVICE(0x1D19, 0x6109),
    .driver_info = CX231XX_BOARD_PV_XCAPTURE_USB},


  6. I prepare simple script that should handle this job as well:

    usage:
    myscript.sh drivers/net/wireless/rt2x00

    #!/bin/bash
    
    MY_TREE=/usr/src/mytree
    KERNEL_VERSION=`uname -r`
    TMP=(${KERNEL_VERSION/-/ })
    CONFIG_FILE=/boot/config-${KERNEL_VERSION}
    SYMVERS_FILE=/usr/src/linux-headers-${KERNEL_VERSION}/Module.symvers
    KERNEL_SRCNAME=linux-source-${TMP[0]}
    KERNEL_SOURCES=/usr/src/${KERNEL_SRCNAME}
    DRIVER_SOURCES=${KERNEL_SOURCES}/${1}
    KERNEL_TARBAL=/usr/src/${KERNEL_SRCNAME}/${KERNEL_SRCNAME}.tar.bz2
    EXTRA_VERSION=-${TMP[1]}
    
    exist(){
        if [ ! -e "${1}" ]; then
            echo "File or directory ${1} does not exists!" && exit;
        else
            echo -e "${2} \t ${1}"
        fi
    }
    echo "Kernel version is: ${KERNEL_VERSION}"
    exist ${CONFIG_FILE} CONFIG_FILE
    exist ${SYMVERS_FILE} SYMVERS_FILE
    exist ${KERNEL_SOURCES} KERNEL_SOURCES
    
    mkdir -p ${MY_TREE}
    cd ${MY_TREE}
    cp ${CONFIG_FILE} ${MY_TREE}
    cp ${SYMVERS_FILE} ${MY_TREE}
    cd ${KERNEL_SOURCES}
    
    if [ ! -f "${KERNEL_SOURCES}/Makefile" ];then
        echo "Missing ${KERNEL_SOURCES}/Makefile, try find tarbal with sources..."
        if [ -f "${KERNEL_TARBAL}" ];then
            echo "Found compressed kernel in: ${KERNEL_TARBAL}, decompressing..."
            [ -d "./${KERNEL_SRCNAME}" ] || tar jxf ${KERNEL_TARBAL}
            KERNEL_SOURCES=${KERNEL_SOURCES}/${KERNEL_SRCNAME}
            DRIVER_SOURCES=${KERNEL_SOURCES}/${1}
        else
            echo "Directory ${KERNEL_SOURCES} looks like broken kernel source!"
            exit
        fi
    fi
    exist ${KERNEL_SOURCES} KERNEL_SOURCES
    exist ${DRIVER_SOURCES} DRIVER_SOURCES
    
    cd ${KERNEL_SOURCES}
    echo "Current location is: `pwd`"
    
    make EXTRAVERSION=${EXTRA_VERSION} O=${MY_TREE} oldconfig
    make EXTRAVERSION=${EXTRA_VERSION} O=${MY_TREE} prepare
    make EXTRAVERSION=${EXTRA_VERSION} O=${MY_TREE} outputmakefile
    make EXTRAVERSION=${EXTRA_VERSION} O=${MY_TREE} archprepare
    make EXTRAVERSION=${EXTRA_VERSION} O=${MY_TREE} modules SUBDIRS=scripts
    make EXTRAVERSION=${EXTRA_VERSION} O=${MY_TREE} modules SUBDIRS=${DRIVER_SOURCES}
    
    echo "All done :)"
    

  7. Pingback October 29, 2015: Unix:Linux: Apple Wireless A1314 Fn key not registered, looks like software bug – Unix Questions

  8. Pingback November 4, 2015: Ubuntu:how do I compile just wireless drivers in kernel? – Ubuntu Linux Questions

  9. Pingback April 7, 2016: Linux: Apple Wireless A1314 Fn key not registered, looks like software bug – Stars Blog

Leave a Reply

Your email address will not be published. Required fields are marked *