Raspberry Pi is a Linux based single board computer. It can be configured to be used as a Linux Kernel Development or Linux Device Driver Development tool. Raspberry Pi can be confirmed easily learning tool for kernel development.
Well this would be first question that would pop up in your head isn’t it? Shouldn’t I be using my safe virtual machine setup for learning kernel development. Well basically you could but you would be missing out all the fun that actual hardware has to offer you.
There are several advantages of using RPi as you kernel development tool. Firstly, you are working on a actual system with all the real hardware which won’t be the case if you are using a virtual machine setup. Secondly you have access to all the tools that you would use on an Linux workstation.The thing that I like about using Raspberry Pi for kernel development is that you can do all you experiments safely.Safely in the sense all that you are putting at risk is the current installation in cases when you mess the system badly, you can get the system back to a known state using OS image backups. Now imagine the chaos that would ensue when you crash you workstation with all you data.
Kernel developers keep a sacrifical system for their development in case things went south. Here comes in Raspberry Pi, your own inexpensive sacrificial development system. Follow along for steps on how to configure a Raspberry Pi for kernel development.
ssh
file in the sd card to enable ssh on boot. This would make sure you can remotely access your raspberry pi i.e. your development system.This is needed as network operations need system time to be proper, eg. for installing some package.
sudo timedatectl set-timezone Asia/Kolkata
sudo date -s "11 Feb 2020 20:00"
sudo apt-get update
sudo apt install raspberrypi-kernel-headers
You need the kernel headers as during kernel development process you will be including these header files. They need to be the ones with which the your kernel was orignally built.
Save the following as hello.c
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/init.h> /* Needed for the macros */
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Suyash Singh Bitti");
MODULE_DESCRIPTION("Hello World Lodable Kernel Module");
static int __init hello_start(void)
{
printk(KERN_INFO "Hello World Kernel Module \n");
return 0;
}
static void __exit hello_end(void)
{
printk(KERN_INFO "Bye.!\n");
}
module_init(hello_start);
module_exit(hello_end);
Save the following as Makefile
. Make sure you are using tabs for indentation and not spaces.
obj-m = hello.o
all:
make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Compile the module by running make
tail -f /var/log/kern.log
to have a look on the kernel logs. -f
option tells tail
to show changes in the file at realtime. This way you won’t have keep checking the logs yourself.sudo insmod hello.ko
lsmod
command.sudo rmmod hello
sudo apt-get install git
git config --global user.name "Suyash"
git config --global user.email "suyash@example.com"
git config --global core.editor nano
These steps should configure your Raspberry Pi for initial linux kernel development.
Hi, I'm Suyash! I'm an Embedded Software Engineer. I work on Linux Device Drivers, Linux System Programming, Embedded System Design. You can know more about me here.