Simple Linux driver code:
// Mem. c file # include <Linux/init. h> # include <Linux/module. h> module_license ("GPL"); static int mem_init (void) {printk (kern_alert "Hello, kernel \ n"); Return 0;} static void mem_exit (void) {printk (kern_alert "Goodbye, kernel \ n");} module_init (mem_init); module_exit (mem_exit );
Makefile:
Ifneq ($ (kernelrelease),) obj-M: = mem. oelsekerneldir? =/Lib/modules/$ (shell uname-R)/buildpwd :=$ (shell PWD) Default: $ (make)-C $ (kerneldir) m =$ (PWD) modulesendifclean: Rm-RF *. ko *. mod. C *. O *. symvers *. order
After compilation, use insmod and rmmod to load and uninstall the driver module.
For example: sudo insmod mem. Ko
Note that you must add sudo before the command; otherwise, an error is returned: insmod: Error inserting 'mem. Ko ':-1 operation not permitted.
To view the mounted modules, run the following command: lsmod.
To view the output from printk, enter the command dmesg in the terminal.