Kernel Modul erstellen

Aus Xinux Wiki
Zur Navigation springen Zur Suche springen

Vorbereitung

  • Linux Header installieren
  • apt install gcc make linux-headers-$(uname -r)

Module Quellcode hello.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>

static int __init hello_init(void) {
    printk(KERN_INFO "Hello, World!\n");
    return 0;
}

static void __exit hello_exit(void) {
    printk(KERN_INFO "Goodbye, World!\n");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("A simple Hello World module");
MODULE_AUTHOR("Your Name");

Makefile

obj-m += hello.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

Kompilierung

  • make

Laden

  • insmod hello

Entladen

  • rmmod hello