This kernel module will create two files, under /sys/kernel/hello
:
hello
, which contains the string 'Hello, world!'hello_num
, which reads 500 by default
Both are writable by root. hello
will dump it's contents to the kernel log, while hello_num
will
save the number given by the users to a variable.
git clone https://github.com/akoskovacs/kmod_example.git
cd kmod_example
./install-deps.sh
make insert
Currently the install-deps.sh
script can install all the neccessary dependencies only for the Raspbian Linux distribution.
The insert command will build the module using the current kernel's headers, then it will replace the any other module
named hello
(if any) with the newly built one.
cat /sys/kernel/hello/hello
Will give the string Hello, world!, proving that the module does work.
cat /sys/kernel/hello/hello_num
Reads 500 initially when the module's internal variable is going live, but it can be modified later to any integer number.
sudo sh -c "echo -n 'this is a test' > /sys/kernel/hello/hello"
The sh -c
is needed for sudo
to work properly. If you are root, the echo -n 'this is a test' > /sys/kernel/hello/hello
should also work.
The result could be seen in the kernel logs, by issuing:
dmesg | tail
, you would get:
[12449.884238] hello: Hello, I am a cool small module!
[12712.550500] hello: I got 'this is a test' :D
If the -n
is not used the end quote mark will start in the next line due to the end line mark also written to the file.
The line: sudo sh -c "echo -n '900' > /sys/kernel/hello/hello_num"
, will change the internal integer
variable to 900 instead of the initial 500. This can be tested, by reading the sysfs attribute again:
cat /sys/kernel/hello/hello_num