Kernel module tips

I sometimes want to make minor tweaks to various linux kernel modules. This page just keeps track of my notes on how to do this easily on Ubuntu, building only the single module that has been modified, rather than building the whole kernel.

first, be sure your ubuntu box is fully up-to-date. otherwise the package manager will give you linux sources/headers that are not in sync with your running kernel.

sudo apt-get update
sudo aptitude upgrade
sudo apt-get build-dep linux

get the kernel source in a working directory:

sudo apt-get install linux-headers-$(uname -r)
mkdir ~/kernel-hacking
cd ~/kernel-hacking
apt-get source linux-image-$(uname -r)
export VER=`uname -r | cut -d "-" -f1 -`
ln -s linux-$VER linux

now make whatever tweaks you want. In this case, I want to tweak the bluetooth module, so I'll go into net/bluetooth and mess around.

when you're ready to build, just build the single module that has been tweaked, in this case net/bluetooth:

cd ~/kernel-hacking/linux/net/bluetooth
make -C /usr/src/linux-headers-$(uname -r) M=$(pwd) bluetooth.ko

then back up the original module (again, in this case, net/bluetooth/bluetooth.ko) and blow it away with the tweaked version:

cp /lib/modules/$(uname -r)/kernel/net/bluetooth/bluetooth.ko /lib/modules/$(uname -r)/kernel/net/bluetooth/bluetooth.ko.old
cp bluetooth.ko /lib/modules/$(uname -r)/kernel/net/bluetooth/bluetooth.ko

now, if all went well, you can rmmod the running module and modprobe the new one. Or just reboot.