Linux Driver Development Series -01

Durgesh Saxena
3 min readAug 25, 2020

--

Hello World driver

LINUX-Learning starts here.

Lets build driver than we can talk a bit about it. I think you already know Linux, importance of driver development and C language.

Virtual Machine

I will say, you should prepare a virtual machine with any Linux distribution. You can also use your PC with Linux on it but you need to consider hard times. You are now trying to intervene into the most critical space of Operating System. There is a high possibility that you will crash your Operating System multiple times while this journey. So, Virtual machines will save your time in recovery. But final decision is up-to you…

I’m using VMware with Ubuntu 14.04 LTS currently. You can use any higher LTS version too.

Linux Driver

#include<linux/module.h>
#include<linux/init.h>
#include<linux/kernel.h>

static int hello_init(void){
printk(KERN_ALERT “Hello Durgesh; it’s your first driver.\n”);
return 0;
}

static void hello_exit(void){
printk(KERN_INFO “Goodbye Durgesh;\n”);
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_AUTHOR(“Durgesh”);
MODULE_LICENSE(“GPL”);
MODULE_DESCRIPTION(“Durgesh’s first driver with GPL licence.”);

Let’s copy above source in hello.c file and save in folder. This is your first Linux driver. I believe, it is very clear, what this driver does.

Every Linux driver should implement “module_init” and “module_exit” API’s provided by Linux kernel. These 2 API’s are basically your drivers entry and driver exit point respectively. You can implement your own function and pass them in these API if you like to do something on these events. Currently, i’m just printing 2 messages, one on entry and other on exit.

Makefile

We mostly use “make” utility to build sources in Linux. If you are using “make” than you have 2 choices with this utility.

  1. separate commands

make -C /lib/modules/$(uname -r)/build M=$(pwd) modules

This command will build final Driver module which can be inserted into Linux Kernel.

make -C /lib/modules/$(uname -r)/build M=$(pwd) clean

This command will clean and remove files created by “make” command.

2. Makefile

You can create “Makefile” and copy these commands into it so you don’t need to type and remember them.

obj-m:=hello.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules

clean:
make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) clean

Lets copy and save above lines in “Makefile” in the same folder.

You can still use “make” command but you can type “make all” or “make clean” accordingly.

Build Driver Module

You can now, execute “make” command to build module. “make” will generate following files.

hello.ko hello.mod.o modules.order hello.mod.c hello.o Module.symvers

For us, interesting file is “hello.ko”. You are ready with first driver if you see this file in folder.

Testing

insmod

Run “insmode hello.ko” command to insert module into kernel. Your driver is inserted If you don’t see any errors after executing this command.

Let’s verify it…

Run “dmesg | tail” command. This command will show you last few lines of debug messages. You should see “Hello Durgesh; it’s your first driver.” if everything goes well.

Congratulations…. You have your first Linux driver up and running.

rmmod

Lets remove driver from kernel space as it is not doing anything and also you will learn, how to remove module. “rmmod hello” command will remove driver.

Run “dmesg | tail” command to see driver messages too. You should see “Goodbye Durgesh;”.

Error handing

I would like to give you some pointers for error handing while building and testing driver. Normally these steps are very easy but you never know.

  1. Use google and search for solution.
  2. Replace all the spaces with TAB from Makefile if you see compilation errors.
  3. check if you have build directory under “make -C /lib/modules/$(shell uname -r)/build” path mentioned in Makefile.

No Build Directory

Follow bellow steps.

  1. Run “apt search linux-headers-$(uname -r)” to search Linux headers for Linux distribution. This command will automatically add installation request if headers are available.
  2. Run “sudo apt update”. I believe, you should see build folder and linux headers after running this command. In case, you don’t than run last command.
  3. Run “sudo apt install linux-headers-$(uname -r)”

Why we need build directory and Linux headers?

Check your driver sources. you have some headers included. Also, there are lots of other information, files and resources required to compile driver module. All this data will come along with Linux headers installation. Remember, You can not build driver without Linux headers and build tools…

--

--

Durgesh Saxena
Durgesh Saxena

Written by Durgesh Saxena

Durgesh Saxena is a result-oriented professional with over 17 years of IT experience. He is trained in Reiki, hypnosis, and the Law of attraction.