Most asked interview questions on Linux kernel Modules
During the Linux Device Driver interview, Linux Kernel modules are a very important topic so I will cover some basic interview questions on Kernel modules which surely gonna help you in your professional interviews.
1- What are the Linux Kernel modules and their types?
Kernel modules are pieces of code that can be added/removed from the Kernel address space dynamically.
- In-tree modules-
These modules are part of the Kernel source code and are generated during the kernel compilation.
these modules are always present at this location /lib/modules/`uname -r`.
- Out tree modules-
These modules are built and load from userspace.
Located where the user created it.
2- Write a simple kernel module program.
We will create a hello.c kernel module source file and see how to insert this module in kernel code.
//Simple hello kernel module #include <linux/kernel.h> #include <linux/module.h> static int hello_module_init(void) { printk(KERN_INFO"%s: Module loaded\n", __func__); return 0; } static void hello_module_exit(void) { printk(KERN_INFO"%s: Module removed\n", __func__); } module_init(hello_module_init); module_exit(hello_module_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("tutorialadda");
module_init is the entry point of the kernel modules and hello_module_init function is registered with module_init. so hello_module_init function is executed when this module is inserted into the kernel.
Similarly, module_exit is the exit point of kernel modules and hello_module_exit function is registered with module_exit. so hello_module_exit function is called when this module is removed from the kernel.
To compile and build this module, we need to create the Makefile
obj-m := hello.o all: make -C /lib/modules/`uname -r`/build M=$(PWD) modules clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Now run the make command it will create the module hello.ko
$ make # To load module run below command $ sudo insmod hello.ko # To remove the module $ sudo rmmod hello
3- Can we load this module using modprobe command?
No, modprobe load module from the Linux module standard path(/lib/modules/`uname -r`) because modprobe only loads modules that are present in the module.dep file.
module.dep file generated during the kernel compilation and it contains the module name with their dependency
This file is located at /lib/modules/`uname -r`/ path.
Example-
kernel/drivers/char/nvram.ko: kernel/drivers/char/ppdev.ko
kernel/drivers/parport/parport.ko
Above modules are present in modules.dep and ppdev.ko module depends on parport.ko so modprobe first load parport.ko then ppdev.ko
Similarly, nvram.ko doesn't have any dependency so modprobe loads this module directly.
To load out tree modules through modprobe we need to do some extra effort.
Steps to load out tree module using modprobe command
You already generated the module then follow the below steps to insert the module using modprobe command.
- Create a symlink of your module to the Linux module standard path.
$ sudo ln -s /home/tutorialadda/test/hello.ko /lib/modules/`uname -r`/
- Run depmod tool to add your module to modules.dep file
sudo depmod -a
- Load module using modprobe command
$ modeprobe hello.ko
4- What is varmagic in the kernel module?
Varmagic is a variable that stores information on the kernel version.
The kernel uses this variable to know on which kernel version this module is compiled.
You can un the modinfo command to know your module varmagic.