hello.c:
#include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h> MODULE_LICENSE("Dual BSD/GPL"); MODULE_AUTHOR("xudonglee"); static int hello_init(void) { printk(KERN_ALERT "Hello, world!\n"); return 0; } static void hello_exit(void) { printk(KERN_ALERT "Goodbye, My Dear World!\n"); } module_init(hello_init); module_exit(hello_exit);
Makefile:
obj-m = hello.o KVERSION = $(shell uname -r) all: make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules clean: make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean
The files:
order in terminal:
make
insmod hello.ko
tail /var/log/messages(read the log of output)
rmmod hello.ko
tail /var/log/messages(read the log of output)