Linux Module

From MK Wiki EN
Revision as of 17:19, 28 June 2018 by MkWikiEnSysOp (talk | contribs) (5 revisions imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This documents contains my experiences with Linux Kernel Module development. They are needed for my bachelor thesis #1.

testdev.c

It is about making a kernel module like this one: The Linux Kernel Module Programming Guide - Chapter 4. Character Device Files. I copied the source from chapter 4.1 to a file, renamed it to "testdev.c" and replaced

#define DEVICE_NAME "chardev"

with

#define DEVICE_NAME "testdev"

Next, I corrected an error that might be caused by either changes in the kernel interfaces or by a stricter compiler. In line 72, I removed the assignment, so that the code looks like this:

unregister_chrdev(Major, DEVICE_NAME);

Then I removed the following two lines (if (ret < 0)...) because they don't make any sense any more, because ret isn't assigned any more.

Makefile

obj-m += testdev.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

Loading and unloading

Any actions takes by the kernel and the module can be watched using

tail -f /var/log/syslog

Loading the kernel module:

insmod testdev.ko

Yields something like the following in /var/log/syslog:

Oct 10 06:31:27 eeepc1104 kernel: [25056.347595] testdev: module license 'unspecified' taints kernel.
Oct 10 06:31:27 eeepc1104 kernel: [25056.347611] Disabling lock debugging due to kernel taint
Oct 10 06:31:27 eeepc1104 kernel: [25056.349748] I was assigned major number 251. To talk to
Oct 10 06:31:27 eeepc1104 kernel: [25056.349764] the driver, create a dev file with
Oct 10 06:31:27 eeepc1104 kernel: [25056.349777] 'mknod /dev/testdev c 251 0'.
Oct 10 06:31:27 eeepc1104 kernel: [25056.349787] Try various minor numbers. Try to cat and echo to
Oct 10 06:31:27 eeepc1104 kernel: [25056.349798] the device file.
Oct 10 06:31:27 eeepc1104 kernel: [25056.349807] Remove the device file and module when done.

Check that module is loaded:

cat /proc/modules | grep "testdev"

Outputs something like:

testdev 12655 0 - Live 0xf8771000 (P)

Then create device file (see hint in /var/log/syslog):

mknod /dev/testdev c 251 0

Very nice:

root@eeepc1104:/home/michael/Projects/Tests/testdev# cat /dev/testdev 
I already told you 0 times Hello world!

Unload module:

rmmod testdev

Removing device node:

rm /dev/testdev