Simple examples of Linux Kernel Modules, written as a learning exercise.
This sample simply takes a name as a parameter, and writes a greeting to the kernel log (/var/log/kern.log). cd into greeter/ before running any of the commands before.
makeThe module is compiled to greeter.ko.
# Install:
sudo insmod greeter.ko
# Install with parameters:
sudo insmod greeter.ko name=Frodo
# Uninstall
sudo rmmod greeterbabel is a character driver which adds a device called /dev/babel. This is a device you can 'talk' to. It'll babble back in gibberish.
Install with:
cd babel
make
sudo insmod babel.koThen run the test client, which'll let you chat with the /dev/babel device:
sudo ./babel-clientUninstall with:
sudo rmmod babelMake the /dev/babel device read/write accessible without super user priviledges by adding a rule. First get the Kernal and Subsystem name:
udevadm info -a -p /sys/class/babel/babel
# Will show something like:
# KERNEL=="babel"
# SUBSYSTEM=="babel"Now create a low-priority rule to enable user access:
echo 'KERNEL=="babel", SUBSYSTEM=="babel", MODE="0666"' >> /etc/udev/rules.d/99-babel.rulesReload the driver and it will be accessible without superuser rights.
Show loaded modules with:
lsmodShow menuconfig with:
sudo make menuconfigShow the kernel log with:
dmesgShow info about the module:
modinfo greeter.koNote: if you get the following error:
insmod: ERROR: could not insert module greeter.ko: Invalid parameters
Then make sure you are not trying to install the module from a shared location (such as a shared folder on a virtual machine). Copy it to the home directory and install from there instead.
Trace module calls with:
sudo apt-get install strace
sudo strace ./babel/babel_clientstrace will show low-level system calls in realtime as the program makes them.
The bulk of this code came from:
With the inspiration from the "Introduction to Linux Kernels" webinar from the Linux Foundation.