Support #686
Updated by Daniel Curtis almost 10 years ago
This is a guide on how I compile a kernel module for Raspbian on a Raspberry Pi 2. 
 h2. Prepare the Environment 
 * Make sure the system is up to date: 
 <pre> 
 apt-get update && apt-get upgrade 
 </pre> 
 * Install a few dependencies: 
 <pre> 
 apt-get install build-essential bc automake autoconf libtool gawk alien fakeroot zlib1g-dev uuid-dev libblkid-dev libselinux-dev parted lsscsi wget 
 </pre> 
 h2. Download the Kernel 
 * Clone the latest raspberry pi toolchain: 
 <pre> 
 cd /usr/src 
 git clone https://github.com/raspberrypi/tools 
 </pre> 
 * Export the toolchain path: 
 <pre> 
 export CCPREFIX=/usr/src/tools/arm-bcm2708/arm-bcm2708-linux-gnueabi/bin/arm-bcm2708-linux-gnueabi- 
 </pre> 
 * Download the latest linux kernel for the raspberry pi: 
 <pre> 
 cd /usr/src 
 git clone https://github.com/raspberrypi/linux 
 </pre> 
 * Then checkout the current kernel version: 
 <pre> 
 cd /usr/src/linux 
 zgrep "* firmware as of" /usr/share/doc/raspberrypi-bootloader/changelog.Debian.gz | head -1 | awk '{ print $5 }' | git checkout - 
 </pre> 
 * Then export the kernel source path: 
 <pre> 
 export KERNEL_SRC=/usr/src/linux 
 </pre> 
 h2. Compile the Kernel Modules 
 * Load the configs kernel module: 
 <pre> 
 modprobe configs 
 </pre> 
 * Load the current config into the kernel source directory: 
 <pre> 
 zcat /proc/config.gz > /usr/src/linux/.config 
 </pre> 
 * Prepare the kernel build environment: 
 <pre> 
 cd /usr/src/linux 
 make mrproper 
 </pre> 
 * Use the previous kernel config with the new kernel build environment: 
 <pre> 
 make oldconfig 
 </pre> 
 * Build the kernel: 
 <pre> 
 make -j4 
 </pre> 
 * Now use a make file that references the kernel source directory to built the kernel module only, use a line like: 
 <pre> 
 PREFIX = /usr/src/tools/arm-bcm2708/arm-bcm2708-linux-gnueabi/bin/arm-bcm2708-linux-gnueabi- 
 make -C /usr/src/linux M=$(SRC) modules 
 </pre> 
 h2. Resources 
 * http://lostindetails.com/blog/post/Compiling-a-kernel-module-for-the-raspberry-pi-2 
 * http://openstack.prov12n.com/openstack-on-raspberry-pi-part-2-getting-started/ 
 * https://raspberrypicloud.wordpress.com/2013/03/12/building-an-lxc-friendly-kernel-for-the-raspberry-pi/ 
 * http://elinux.org/Raspberry_Pi_Kernel_Compilation