Adventures in Linux Kernel - Task 01

Not so long ago, after I started researching Linux kernel I got curious about how can I contribute into the kernel and I found. This challenge is about step by step immersion into kernel module writing. There will be more!

Authors of this challenge ask not to post code publicly. I don’t agree with them because this challenge is for people who are willing to learn and they’re apriory would’t search for ready-to-use answers. However I won’t do it in respect of creators of this challenge. But if you need a nudge feel free to contact me via email.

The first task was to write a kernel module. It is pretty easy if you are familiar with C language. There’re only three functions we need: init_module, cleanup_module and printk. The main function here is init_module. From man we have:

init_module() loads an ELF image into kernel space, performs any necessary symbol relocations, initializes module parameters to values provided by the caller, and then runs the module’s init function. This system call requires privilege.

On success, these system calls return 0. On error, -1 is returned and errno is set appropriately.

It’s a main function in kernel module (almost). cleanup_module is a function which is called when a module is unloaded.

Then we can compile the module and load it:

make
insmod ./hello-1.ko

Then look at loaded modules:

cat /proc/modules | grep hello

Additionally you can see the last messages by using:

dmesg | tail -1
[ 1325.631657] Hello world!

It turned out to be not so easy and I got the following response:

Please print to the kernel debug log level.

Please read the requirements for the Makefile and allow the module to bebuilt against any kernel source tree on the filesystem, not just thosekernels that happened to be installed in /lib/ at some point in time.

Yes. I was asked to do the following:

Write a Linux kernel module, and stand-alone Makefile, that when loaded prints to the kernel debug log level, “Hello World!”

The Makefile should be able to build the kernel module against the source of the currently-running kernel as well as being able to accept an arbitrary kernel sources directory from an environment variable.

I found this helpful for writing Makefile to build a kernel for multiple distributions.

As for debug messages, they are simple and could be found here.

Not so long ago, after I started researching Linux kernel I got curious about how can I contribute into the kernel and I found. This challenge is about step by step immersion into kernel module writing. There will be more!

Authors of this challenge ask not to post code publicly. I don’t agree with them because this challenge is for people who are willing to learn and they’re apriory would’t search for ready-to-use answers. However I won’t do it in respect of creators of this challenge. But if you need a nudge feel free to contact me via email.

The first task was to write a kernel module. It is pretty easy if you are familiar with C language. There’re only three functions we need: init_module, cleanup_module and printk. The main function here is init_module. From man we have:

init_module() loads an ELF image into kernel space, performs any necessary symbol relocations, initializes module parameters to values provided by the caller, and then runs the module’s init function. This system call requires privilege.

On success, these system calls return 0. On error, -1 is returned and errno is set appropriately.

It’s a main function in kernel module (almost). cleanup_module is a function which is called when a module is unloaded.

Then we can compile the module and load it:

make
insmod ./hello-1.ko

Then look at loaded modules:

cat /proc/modules | grep hello

Additionally you can see the last messages by using:

dmesg | tail -1
[ 1325.631657] Hello world!

It turned out to be not so easy and I got the following response:

Please print to the kernel debug log level.

Please read the requirements for the Makefile and allow the module to bebuilt against any kernel source tree on the filesystem, not just thosekernels that happened to be installed in /lib/ at some point in time.

Yes. I was asked to do the following:

Write a Linux kernel module, and stand-alone Makefile, that when loaded prints to the kernel debug log level, “Hello World!”

The Makefile should be able to build the kernel module against the source of the currently-running kernel as well as being able to accept an arbitrary kernel sources directory from an environment variable.

I found this helpful for writing Makefile to build a kernel for multiple distributions.

As for debug messages, they are simple and could be found here.