Linux kernel source code tree build and load the hello module, linuxhello
Before loading the module, the book said that we should first establish the kernel source code tree. So, how should we establish the kernel source code tree?
First, you must first know the kernel version of your OS and use uname-r to obtain the kernel version.
In this directory ls, we can see that this directory already exists =! That is, the original system already has the kernel source code tree.
Now that the kernel source code tree already exists, we need to load the hello module into the kernel.
Below is the source code of hello
1 #include <linux/init.h> 2 #include <linux/module.h> 3 MODULE_LICENSE("Dual BSD/GPL"); 4 static int hello_init(void) 5 { 6 printk(KERN_ALERT "Hello, world\n"); 7 return 0; 8 } 9 static void hello_exit(void) 10 { 11 printk(KERN_ALERT"Goodbye, cruel world\n"); 12 } 13 module_init(hello_init); 14 module_exit(hello_exit);
Write Makefile for code
1 obj-m := hello.o 2 KERNELDIR := /lib/modules/3.16.0-30/build 3 PWD := $(shell pwd) 4 modules: 5 $(MAKE) -C $(KERNELDIR) M=$(PWD) modules 6 modules_install: 7 $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
Run make
Generate the corresponding hello. ko File
Then load the hello. ko module. Be sure to load it in the root mode. Then, lsmod will view the loaded module (the book says hello world will be output on the console, but my console does not output it)
Uninstall Module
Although no output is made on the console, you can view the output of the hello module in/var/log/syslog.