Talk: Linux device drivers

(1) Implementing a Hello World module from scratch

Follow the tutorial at "The Linux Kernel Module Programming Guide" from section 2.1 to 2.7 "Hello World"

https://www.tldp.org/LDP/lkmpg/2.6/html/index.html

Implement a Hello World module as described.

Note

Bare in mind this is an old tutorial, you will need to chase some info to adapt it (feel free to contribute with this page ;).

If you doesn't have sudo power

You can use virtme to be able to insert your module into the kernel. See appendix (A).

(2) Implementing a simple char device driver

Check section 4. Character Device Files of the previous linked tutorial.

(A) Inserting your module into your virtualized kernel with virtme

(1) Using the installed kernel

You have to acquire busybox beforehand, since the installed kernel probably needs a initramfs to boot.

cd <path> # where path is the directory you built your module
# Download busybox binary
curl -LO https://busybox.net/downloads/binaries/1.28.1-defconfig-multiarch/busybox-x86_64
# Give execution permission to busybox
chmod a+x busybox-x86_64

Be sure that you have your module and your Makefile are written correctly. You can build them by following the tutorial from tldp cited in section (1). Once you are done, that is, you have your hello-*.ko built, you can run virtme.

# run virtme with the installed kernel, with the downloaded busybox,
# giving read permission to your current dir to the virtualized environment
# and changing to the current directory
virtme-run --installed-kernel --busybox ./busybox-x86_64 --pwd

Now you should be in the virtualized environment.

insmod hello-<number>.ko # insert your module
rmmod hello-<number>.ko # remove your module
dmesg

(2) Using your compiled kernel

If you noticed, the Makefile showed in the tldp tutorial uses your installed kernel root to compile your module. It is infered by the path /lib/modules/$(shell uname -r)/build, which links to the kernel source.

If you try to insert this module, compiled for one version of Linux Kernel, into a kernel with other version, the insertion will fail.

So you just need to change to which kernel source the Makefile is pointing.

Let's modify this Makefile with your path:

MY_KERNEL_ROOT=<path_to_your_kernel_tree_root>

all:
        make -C $(MY_KERNEL_ROOT) M=$(PWD) modules

clean:
        make -C $(MY_KERNEL_ROOT) M=$(PWD) clean

Now run make. And run virtme:

cd <path> # where path is the directory where your Makefile is located
make
# run virtme with the custom kernel source tree,
# giving read permission to your current dir to the virtualized environment
# and changing to the current directory
virtme-run --kdir <path_to_your_kernel_tree_root> --pwd

Now you should be in the virtualized environment.

insmod hello-<number>.ko # insert your module
rmmod hello-<number>.ko # remove your module
dmesg

Done?

\o/

Try to understand the concepts and differences between a driver, a device, a bus, major and minor numbers in the context of the kernel.

Don’t forget to update the spreadsheet for tracking our progress.