August 2015 ## This guide has been updated to the latest version of Raspbian ##
Compiling the kernel on the Raspberry Pi can take some time, Im not sure how long it takes as I have never waited long enough, I gave up after 4 hours.
Below is a guide on how to compile the kernel on a faster PC and then transfer the new kernel and modules over to the Raspberry Pi.
Cross compiling is when you compile on a different platform then what the kernel will be used for. This is mostly done when the host receiving the kernel is on a slow or legacy device.
In this guide, I will use Ubuntu 12.10 running within VMWare on a Windows 10 Quad core PC. Kernel compile time is 15 mins.
Network connectivity is needed between the VMWare host and the Raspberry Pi.
Please take note of what host the cammands are run on.
This is the Raspberry Pi;
This is the Ubunti VMWare host;
Prepare the Raspberry Pi
-Download and install Raspbian “wheezy”.
For this guide, I used 2017-01-11-raspbian-jessie.img
-Configure your Pi. E.g. Memory split, overclocking, etc..
-Update firmware and reboot.
Install BC which is used by rpi-update later on.
Prepare the sources and tools
-Install packages used for cross compiling on the Ubuntu box.
-Install toolchain.
-Add the toolchain to your path, this helps further on.
-Download the current source for your kernel onto the Raspberry. We will use ‘rpi-source’.
More info here https://github.com/notro/rpi-source/wiki
pi@raspberrypi ~ $ sudo wget https://raw.githubusercontent.com/notro/rpi-source/master/rpi-source -O /usr/bin/rpi-source && sudo chmod +x /usr/bin/rpi-source && /usr/bin/rpi-source -q --tag-update pi@raspberrypi ~ $ rpi-source --skip-gcc
Once finished, you will end up with a new folder and tar file in your home directory, these contain the source.
E.g linux-df068289972288ecd46a02906df91358e1eca56b.tar.gz
-We need to copy the tar file over to the Ubuntu box. Replace ‘raspberrypi’ below with the IP address of your Raspberry Pi if hostname lookup fails.
-Now extract the contents.
-Grab the current config off the Raspberry Pi.
-Copy the .config from the Raspberry Pi to the Ubuntu box using SCP. CD into the Linux source folder first.
mark@ubuntu~$ sudo scp pi@raspberrypi:config .
mark@ubuntu~$ mv config .config
Build Source and Compile Kernel
-CD into the Linux source on the Ubuntu box. And enter the following commands;
For Pi1 and compute module 1
mark@ubuntu~$ KERNEL=kernel
mark@ubuntu~$ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcmrpi_defconfig
For Pi2, Pi3 and CM3
mark@ubuntu~$ KERNEL=kernel7
mark@ubuntu~$ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2709_defconfig
-Now compile the kernel.
For all versions of Pi, use.
To enable parallel processing for a faster compile. If you have a dual core processor add -j 3 to the end of the command below. If you have quad core, add -j 6
-Create modules on the Ubuntu box which we will copy over later.
pi@ubuntu:~$ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- INSTALL_MOD_PATH=../modules/ modules_install
-Create the new kernel image and copy it to your home folder ~
For Pi1 and compute module 1
For Pi2, Pi3 and compute module 3
-Copy the new kernel over to the Raspberry Pi and place it into the /boot directory.
For Pi1 and compute module 1
For Pi2, Pi3 and compute module 3
-Now copy over the new modules and device tree info. First remove the unneeded directories, otherwise you will be copying over 1.9GB
pi@ubuntu:$ rm -f -r ../modules/lib/modules/4.1.4/source
pi@raspberrypi ~ $ sudo rm -f -r /lib/modules
pi@raspberrypi ~ $ sudo rm -f -r /lib/firmware
pi@raspberrypi ~ $ sudo scp -r pi@ubuntu:/home/pi/modules/lib /
pi@raspberrypi ~ $ sudo scp -r pi@ubuntu:/home/pi/linux-df068289972288ecd46a02906df91358e1eca56b/arch/arm/boot/dts/*.dtb /boot/
pi@raspberrypi ~ $ sudo scp -r pi@ubuntu:/home/pi/linux-df068289972288ecd46a02906df91358e1eca56b/arch/arm/boot/dts/overlays/*.dtb* /boot/overlays/
-Reboot.
[wp_ad_camp_1]
sudo wget https://raw.githubusercontent.com/notro/rpi-source/master/rpi-source -O /usr/bin/rpi-source && sudo chmod +x /usr/bin/rpi-source && /usr/bin/rpi-source -q -tag-update
gives….
rpi-source: error: unrecognized arguments: -tag-update
try with
--tag-update
I cant undersatnd and execute what is this? pi@ubuntu:~$../tools/mkimage/mkknlimg arch/arm/boot/zImage ~/kernel.img
can you please correct it?
should this be “sudo scp pi@ubuntu:kernel.img /boot/kernel.img” be replaced with “sudo scp pi@ubuntu:kernel.img /boot/kernel7.img” for Pi 2 B ?? please help..
correct. it is kernel7.img. I have updated the page above
Also please fix this line “pi@ubuntu:~$../tools/mkimage/mkknlimg arch/arm/boot/zImage ~/kernel.img” i think there is something wrong witht this line
try this
scripts/mkknlimg arch/arm/boot/zImage ~/kernel.img
I have updated the webpage above.
Mark Thanks a lot ..for updating, its been a great help.
This is working absolutely fine…just a minor correction “sudo scp pi@ubuntu:kernel.img /boot/kernel7.img” should be pi@ubuntu:kernel7.img …thanks
Hey Mark I am trying to compile simple kernel Module(Hello) getting this errors….can you help with this??
pi@raspberrypi:~/test_modules$ make
make -C /lib/modules/4.4.45-v7/build M=/home/pi/test_modules modules
make[1]: Entering directory ‘/lib/modules/4.4.45-v7/build’
CC [M] /home/pi/test_modules/hello.o
./scripts/recordmcount: 3: ./scripts/recordmcount: Syntax error: Unterminated quoted string
scripts/Makefile.build:264: recipe for target ‘/home/pi/test_modules/hello.o’ failed
make[2]: *** [/home/pi/test_modules/hello.o] Error 2
Makefile:1402: recipe for target ‘_module_/home/pi/test_modules’ failed
make[1]: *** [_module_/home/pi/test_modules] Error 2
make[1]: Leaving directory ‘/lib/modules/4.4.45-v7/build’
Makefile:5: recipe for target ‘all’ failed
make: *** [all] Error 2