feature: simple module structur. This module taken simple int parameter

This commit is contained in:
RouxAntoine 2024-04-20 23:44:46 +02:00
commit 250977e7f8
Signed by: antoine
GPG Key ID: 098FB66FC0475E70
4 changed files with 103 additions and 0 deletions

12
.gitignore vendored Normal file
View File

@ -0,0 +1,12 @@
# ignore intellij related files
.idea/
*.iml
# ignore module generated files
*.cmd
*.ko
*.o
*.d
*.mod*
Module.symvers
modules.order

6
Makefile Normal file
View File

@ -0,0 +1,6 @@
obj-m += hello.o
KDIR := /usr/src/linux-headers-$(shell uname -r)
%:
$(MAKE) -Wall -C $(KDIR) M=$(PWD) $@

55
README.md Normal file
View File

@ -0,0 +1,55 @@
# build my own linux kernel module
run a linux container
```shell
docker run --name linux --privileged -v /lib/modules:/lib/modules -v ./:/root/data -v /usr/src:/usr/src -it -d debian
```
setup into container the required dependencies
```shell
docker exec -it linux bash
apt update && apt install -y gcc make kmod procps git
```
cleaning
```shell
docker rm -f linux
```
show module information
```shell
modinfo hello.ko
```
search if module is loaded
```shell
lsmod | grep hello
```
(un)/load module
```shell
insmod hello.ko [myint=4]
rmmod hello.ko
```
load module and load is dependencies module
```shell
ln -s $(pwd)/hello.ko /lib/modules/$(uname -r)
depmod -a
modprobe hello
modprobe -r hello
rm /lib/modules/$(uname -r)/hello.ko
```
show kernel logs
```shell
dmesg -w
```

30
hello.c Normal file
View File

@ -0,0 +1,30 @@
#include <linux/init.h> /* Needed for the __init, __initdata and __exit macros. */
#include <linux/module.h> /* for all kernel modules */
#include <linux/kernel.h> /* for KERN_INFO */
#include <linux/printk.h> /* Needed for pr_info() */
#include <linux/moduleparam.h> /* Needed for module_param() */
static int intInitData __initdata = 3;
static int intParameter = 420;
module_param(intParameter, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
MODULE_PARM_DESC(intParameter, "An integer");
static int __init start_module(void) /* module entry point */
{
pr_info("Hello , user %d !\n", intInitData);
return 0;
}
static void __exit exit_module(void) /* module exit point */
{
printk(KERN_INFO "Goodbye , user param %d!\n", intParameter);
return;
}
module_init(start_module);
module_exit(exit_module);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("My first cool kernel module");
MODULE_AUTHOR("user");