Building kernel for RPi2 with bluetooth_6lowpan for Raspbian

Building kernel for RPi2 with bluetooth_6lowpan

In this guide, you will have two choices:

  • Cross-compilation build

  • Native build

Choose the one that fits your needs. Cross-compilation is the fastest choice.

Dependencies:

In addition to the already installed tools in Raspbian, you'll need

  • Git
  • kernel-package

These can be installed using apt:

sudo apt-get install git kernel-package -y

“kernel-package” is needed for generating the .deb package, and git is used to fetch the repository from Raspberry Pi’s github account. We'll fetch the default branch, which holds 3.18.11 at the time of writing this blog post.

Obtaining the kernel

Open a terminal and maneuver to a clean directory, I made a directory ~/raspbian.

Now we need to clone the kernel:

git clone https://github.com/raspberrypi/linux.git

Grab a coffee. This will take a while.

Option 1: Cross-compilation on x86/x64

The easiest option is to grab the toolchain from RaspberryPi. Go into a clean directory and clone:

git clone https://github.com/raspberrypi/tools.git

Now we’ll have to define this path for easier building. For building on x86 (32-bit) environment:

export CCPREFIX=/path/to/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-

For building on x64 (64-bit) environment:

export CCPREFIX=/path/to/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-

Go to the kernel directory that you just cloned. We’ll have to ensure that the directory is cleaned

hkn@hkn-VirtualBox:~/raspbian/kernel/linux$ make mrproper

Set the default configuration to bcm2709, which is the chipset for Raspberry Pi 2

hkn@hkn-VirtualBox:~/raspbian/kernel/linux$ ARCH=arm CROSS_COMPILE=${CCPREFIX}
 make bcm2709_defconfig

Now we’ll start compiling

hkn@hkn-VirtualBox:~/raspbian/kernel/linux$ ARCH=arm CROSS_COMPILE=${CCPREFIX} INSTALL_MOD_PATH=${MODULES_TEMP} make –j5

hkn@hkn-VirtualBox:~/raspbian/kernel/linux$ ARCH=arm CROSS_COMPILE=${CCPREFIX} INSTALL_MOD_PATH=${MODULES_TEMP} make –j5 modules

On multi-core CPUs, you can utilize the cores via the “-j” operation. On my PC, I have 4 cores, therefore I add “-j 5” to the above command to speed things up. You should set the number to “NUM_OF_CPU_CORES + 1” to ensure that your cores are working full-time.  

Option 2: Native build

It’s recommended to have 10 GB free space on your Raspberry for kernel compilation.

The steps here are very similar to the section about cross-compilation, just that we remove a couple of defines.

First we need to ensure that you have your build essentials in order:

pi@raspberry:~/$ sudo apt-get install build-essential –y

Set the default configuration to bcm2709, which is the chipset for Raspberry Pi 2

pi@raspberry:~/raspbian/kernel/linux$ make bcm2709_defconfig

Now we’ll start compiling

pi@raspberry:~/raspbian/kernel/linux$ make –j5 && make –j5 modules

Generate .deb packages

For cross-compilation approach:

hkn@hkn-VirtualBox:~/raspbian/kernel/linux$ CONCURRENCY_LEVEL=5 DEB_HOST_ARCH=armhf fakeroot make-kpkg --append-to-version –name_of_your_choice --revision `date +%Y%m%d%H%M%S` --ARCH=arm --cross-compile ${CCPREFIX} kernel_image kernel_headers

CONCURRENCY_LEVEL sets the number of jobs. I used 5 since I have four CPU cores.

You will see warnings similar to this when generating the .deb files:

dpkg-architecture: warning: specified GNU system type arm-linux-gnu does not match gcc system type x86_64-linux-gnu, try setting a correct CC environment variable

Don’t worry; this is normal when cross-compiling, and actually reassuring that we did compile the kernel for ARM architecture.

For native build approach:

pi@raspberry:~/raspbian/kernel/linux$ CONCURRENCY_LEVEL=5 DEB_HOST_ARCH=armhf fakeroot make-kpkg --append-to-version –name_of_your_choice --revision `date +%Y%m%d%H%M%S` --initrd kernel_image kernel_headers

The .deb files will be placed in the parent directory. Write “ls ../*.deb” and you should have two files:

../linux-headers-*.deb
../linux-image-*.deb

Installing the kernel on your Raspberry Pi 2

If you cross-compiled, you’ll need to transfer your kernel header and kernel image to the Pi. This can be done via scp for instance.

scp linux-header*.deb linux-image*.deb pi@pi-ip:~

This will transfer the files to your home directory.

Now login to your raspberry pi and update your system, and install the new kernel:

sudo dpkg –i linux-header*.deb linux-image*.deb

The kernel is now installed, but we need to specify in our boot settings that it should use this new kernel.

pi@raspberry ~ $ cd /boot
pi@raspberry /boot $ ls | grep vmlinuz
vmlinuz-3.18.11-rpi2-v7+

Here we found the name of our new kernel image. For simplicity, we will make a symbolic link (shortcut) to ‘vmlinuz’:

pi@raspberry /boot $ sudo ln –s vmlinuz-3.18.11-rpi2-v7+ vmlinuz

Now we specify the kernel and append it to config.txt:

pi@raspberry /boot $ sudo sh –c ‘echo “kernel=vmlinuz” >> config.txt’

Reboot and enjoy a fresh new kernel with the bluetooth_6lowpan module.

  • hi zou,

    i did the exact same steps that u performed but while making the debian packages i get an error

    the error is install: cannot stat 'REPORTING_BUGS' no such file or directory

  • I'm just working through this for a PI 3, even though the bluetooth seems to be installed already, I want to go through the process and see how it all hangs together.

    Where do you select what is built into the kernel? There's no selection in the steps above that I can see, however obviously it's possible to build without things like 6lowpan, because that used to be the default.

    I'm going to guess at the commands I need for the PI 3, assume I just change the chip name to the newer broadcom and continue.

  • It seems that Bluetooth 6lowpan is now natively supported on Raspbian 2015-05-05.

    pi@raspberrypi ~ $ uname -a 
    Linux raspberrypi 3.18.11-v7+ #781 SMP PREEMPT Tue Apr 21 18:07:59 BST 2015 armv7l GNU/Linux
     
    pi@raspberrypi ~ $ ls /lib/modules/3.18.11-v7+/kernel/net/bluetooth/
    bluetooth_6lowpan.ko  bluetooth.ko bnep  hidp  rfcomm
    

    Just load "bluetooth_6lowpan" in /etc/modules.

    BR, zou

  • When I tried the native-build on my raspberry-pi-2 with 16G sd card I meeted a problem :

    WARNING: missing /lib/modules/3.18.16-rpi2-6lowpan-v7+ Device driver support needs thus be built-in linux image!

    ERROR: could not open directory /lib/modules/3.18.16-rpi2-6lowpan-v7+: No such file or directory FATAL: could not search modules: No such file or directory

    ERROR: ld.so: object 'libfakeroot-sysv.so' from LD_PRELOAD cannot be preloaded: ignored.

    WARNING: could not open /var/tmp/mkinitramfs_kQya3Z/lib/modules/3.18.16-rpi2-6lo wpan-v7+/modules.order: No such file or directory

    WARNING: could not open /var/tmp/mkinitramfs_kQya3Z/lib/modules/3.18.16-rpi2-6lo wpan-v7+/modules.builtin: No such file or directory /usr/sbin/update-initramfs: 204: /usr/sbin/update-initramfs: cannot create /var/ lib/initramfs-tools/3.18.16-rpi2-6lowpan-v7+: Permission denied

    run-parts: /etc/kernel/postinst.d/initramfs-tools exited with return code 2 arch/arm/boot/Makefile:99: recipe for target 'install' failed make[3]: *** [install]

    Error 1 arch/arm/Makefile:311: recipe for target 'install' failed make[2]: *** [install] Error 2 make[2]: Leaving directory '/home/pi/raspbian/kernel/linux' debian/ruleset/targets/image.mk:34: recipe for target 'debian/stamp/install/linu x-image-3.18.16-rpi2-6lowpan-v7+' failed make[1]: *** [debian/stamp/install/linux-image-3.18.16-rpi2-6lowpan-v7+]

    Error 2 make[1]: Leaving directory '/home/pi/raspbian/kernel/linux' debian/ruleset/local.mk:105: recipe for target 'kernel_image' failed make: *** [kernel_image] Error 2

    and I don't know how to fix the problem