Linux Kernel Modules

Lsmod

Show the status of modules in the Linux Kernel
Let's look at two modules
[email protected]:~$ lsmod | grep button
button 16384 1 i915
[email protected]:~$ lsmod | grep pcspkr
pcspkr 16384 0
Challenge
Find out where button and pcspkr are under:
  • /lib/modules/
  • Kernel Configuration
  • Mainline

Hello World Module Source Code

Make a "helloworld" directory
[email protected]:~$ mkdir helloworld
Create helloworld.c file under our helloworld directory and add the C code below, this is a simple Hello World Kernel Module
[email protected]:~/helloworld$ helloworld.c
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
static int module_init_function(void)
{
printk(KERN_INFO "Module? Hello!\n");
return 0;
}
static void module_exit_function(void)
{
printk(KERN_INFO "Module? Bye!\n");
}
MODULE_LICENSE("GPL");
MODULE_AUTHOR("xe1gyq");
MODULE_DESCRIPTION("My First Linux Kernel Module");
module_init(module_init_function);
module_exit(module_exit_function);

Hello World Module Makefile

Create the Makefile under helloworld directory and add the code below
[email protected]:~/helloworld$ nano Makefile
obj-m += helloworld.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

Hello World Module Compilation

[email protected]:~/helloworld$ ls
helloworld.c Makefile
[email protected]rkstation:~/helloworld$
Now compile your Hello World Module
[email protected]:~/helloworld$ make
make -C /lib/modules/4.12.0-kali2-amd64/build M=/home/user/helloworld modules
make[1]: Entering directory '/usr/src/linux-headers-4.12.0-kali2-amd64'
CC [M] /home/user/helloworld/helloworld.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/user/helloworld/helloworld.mod.o
LD [M] /home/user/helloworld/helloworld.ko
make[1]: Leaving directory '/usr/src/linux-headers-4.12.0-kali2-amd64'
[email protected]:~/helloworld$

Hello World Module Files

[email protected]:~/helloworld$ ls
helloworld.c helloworld.ko helloworld.mod.c helloworld.mod.o helloworld.o Makefile modules.order Module.symvers
[email protected]:~/helloworld$
[email protected]:~/helloworld$ modinfo helloworld.ko
[email protected]:~/helloworld$ insmod helloworld.ko
[email protected]:~/helloworld$ dmesg
[email protected]:~/helloworld$ lsmod
[email protected]:~/helloworld$ cat /proc/modules
[email protected]:~/helloworld$ rmmod helloworld
[email protected]:~/helloworld$ dmesg