piModules

QUICK KERNEL MODULE COMPILING ON THE RASPBERRY PI

If you're running the Raspbian distribution on the Pi, making kernel modules on the Pi itself is a little more difficult than normal since kernel header and kernel source packages aren't available. The following instructions will allow you to make your modules without compiling the whole kernel, which saves several hours of setup time.

You'll need about 1G free disk space on the SD card. You'll also need Internet access on your Pi. You don't need one of the newer models: a Model A or an older Model B is more than enough.

I'm assuming that you're familiar with the Unix command line. If not, you should go through one of the tutorials available on the web.

Boot up and log in with a freshly imaged SD card of the latest Raspbian. Here's a quick link to download it. NOTE: Don't run raspi-update after booting.

I maintain a repository with pre-configured linux source. I try to keep it updated to match the current Raspbian release. Run the following commands to download it:

    sudo su

    cd /root

    git clone --depth 1 https://github.com/mincepi/linux-4.9.59.git

    cd linux-4.4.50

If you have a model 2 or 3 Pi, you also have to run the following four commands:

    mv -f .config7 .config

    mv -f Module7.symvers Module.symvers

    make clean

    make oldconfig

Copy your module code to the custom directory and add your module name to the Kbuild file. For example, if your module is test.c, add a space then test.o after pi2hd44780.o in the Kbuild file.

To compile run the following commands:

    sudo apt-get install -y bc ncurses-dev

    cd /root/linux-4.4.50

    make modules_prepare

    ./modules.sh

Your module will compile. If there are errors, fix them and re-run modules.sh

That's all there is to it. Your module file will be located in the custom directory, and will have a .ko extension.

HOW I BUILD THE KERNEL SOURCE AND UPLOAD IT TO GITHUB (posted here mostly so I don't forget how)

I've used several methods over the years. This is what I'm using currently:

Install the latest Raspbian on an SD card, install it in a Pi B+ and boot up.

Log in and run the following commands:

    sudo su

    cd /root

Expand /usr/share/doc/rasperrypi-bootloader/changelog.debian.gz and write down the first 8 characters of the topmost firmware hash.

Use a web browser and go to https://github.com/raspberrypi/firmware. Look in the commits for the above firmware hash and write down what number it is. For instance, if there are 2 commits ahead if it, it's commit 3.

Run git clone --depth 3 https://github.com/raspberrypi/firmware, replacing the 3 with your commit number.

Change to the firmware directory and run git checkout -f hash, replacing hash with your 8 character firmware hash.

Run cat extra/git_hash and write down the first 8 characters of the linux hash.

Use a web browser and go to https://github.com/raspberrypi/linux. Look in the commits for the above linux hash and write down what number it is. For instance, if there are 3 commits ahead if it, it's commit 4.

Run cd /root, then run git clone --depth 4 -b 4.4.y --single-branch https://github.com/raspberrypi/linux, replacing the 4 with your commit number.

Change to the linux directory and run git checkout -f hash, replacing hash with your 8 character linux hash.

Copy the Module.symvers and Module7.symvers from firmware/extra to the linux directory.

Copy the module.sh file and custom directory from the previous source to the linux directory.

Edit the Makefile and add a space then + at the end of the EXTRAVERSION line.

While still in the linux directory and run the following to copy the config file and initialize the source:

    modprobe configs

    gunzip -c /proc/config.gz > .config

    make oldconfig

    make modules_prepare

    ./module.sh

Power off and boot this SD card in a Model 2, and login as root.

Copy it's config file as above but name it .config7

Edit the /root/linux/.gitignore file and remove the .*, *.ko, and Module.symvers lines.

Create a /root/linux/README.md file with contents describing the Raspbian version this source corresponds to.

Rename the linux directory to match the linux version: for example linux-4.4.50

Using a web browser, make a new repository on Github, named after the kernel version.

Run the following commands to upload the source. Change linux-4.4.50 to your repository name:

    cd /root/linux-4.4.50

    rm -rf .git

    git config --global user.name "mincepi"

    git config --global user.email "mincepi@gmail.com"

    git init

    git add .

    git commit -m "initial commit"

    git remote add origin https://github.com/mincepi/linux-4.4.50.git

    git push -u origin master

Edit the Raspbian link, the git command, the two cd commands, and possibly the gcc install command in the download instructions at the top of this page. You're all done!

NEW METHOD, STILL BEING TESTED

I've used several methods over the years. This is what I'm using currently:

Install the latest Raspbian on an SD card, install it in a Pi B+ and boot up.

Log in and run the following commands:

    sudo su

    cd /root

    mkdir linux

    cd linux

    gunzip -c /usr/share/doc/raspberrypi-bootloader/changelog.Debian.gz | grep -m 1 of > firmware.txt

Use the hash (the 40 characters after "* firmware as of ") contained in firmware.txt in the following:

    wget -O firmware.tar.gz https://github.com/raspberrypi/firmware/archive/93aae1391121c44c7bbddf66abaf38725ffa2dc0.tar.gz

    tar -xvzf firmware.tar.gz --strip-components 2 --wildcards *extra

Use the hash contained in git_hash in the following:

    wget -O linux.tar.gz https://github.com/raspberrypi/linux/archive/6820d0cbec64cfee481b961833feffec8880111e.tar.gz

    tar -xvzf linux.tar.gz --strip-components 1

Copy the module.sh file and custom directory from the previous source to the current directory.

Likewise copy any other module support files.

Edit the Makefile and add a space then + at the end of the EXTRAVERSION line.

Edit the .gitignore file and remove the .*, *.ko, and Module.symvers lines.

Create a README.md file with contents describing the Raspbian version this source corresponds to.

Run the following to copy the config file and initialize the source:

    modprobe configs

    gunzip -c /proc/config.gz > .config

    cp .config .config6

    make oldconfig

    make modules_prepare

    ./module.sh

Make a v6 directory in custom and copy the new kernel modules to it.

Power off and boot this SD card in a Model 2 or 3, and login as root.

Log in and run the following commands:

    sudo su

    cd /root/linux

    modprobe configs

    gunzip -c /proc/config.gz > .config

    cp .config .config7

    make oldconfig

    make modules_prepare

    ./module.sh

Make a v7 directory in custom and copy the new kernel modules to it.

Edit the .gitignore file and remove the .*, *.ko, and Module.symvers lines.

Create a README.md file with contents describing the Raspbian version this source corresponds to.

Rename the linux directory (the current directory) to match the linux version: for example linux-4.9.59

Using a web browser, make a new repository on Github, named after the kernel version.

Run the following commands to upload the source. Change linux-4.9.59 to your repository name:

    cd /root/linux-4.9.59

    rm -rf .git

    git config --global user.name "mincepi"

    git config --global user.email "mincepi@gmail.com"

    git init

    git add .

    git commit -m "initial commit"

    git remote add origin https://github.com/mincepi/linux-4.9.59.git

    git push -u origin master

Edit the Raspbian link, the git commands, the two cd commands, and possibly the gcc install command in the download instructions at the top of this page. 

Edit the hash values. You're all done!

Revised 12-28-2017

Copyright mincepi

Feel free to contact me with suggestions or corrections at my gmail address: mincepi

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

This site has been tested to display correctly using Gnome Web on the Raspberry Pi