Create a Kernel Module “simple”. 1.Compile kernel module simple.c $make Note: if
ID: 3798473 • Letter: C
Question
Create a Kernel Module “simple”.
1.Compile kernel module simple.c $make Note: if the compiling succeeds, several files are produced.
2.Load Kernel Module $sudo insmod simple.ko (Check out contents in kernel log buffer.) $dmesg
3.Remove Kernel Module $sudo rmmod simple (Check out contents in kernel log buffer.) $dmesg
simple.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
/* This cis called when the module is loaded. */int simple_init(void)
{
printk(KERN_INFO "Loading Modulen");
return 0;}
/* This function is called when the module is removed. */
void simple_exit(void) {
printk(KERN_INFO "Removing Modulen");
}
obj-m += simple.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
Note: Makefile should be in the same directory as simple.c
Screen shot of dmesg after adding and removing the simple module
simple.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
/* This cis called when the module is loaded. */int simple_init(void)
{
printk(KERN_INFO "Loading Modulen");
return 0;}
/* This function is called when the module is removed. */
void simple_exit(void) {
printk(KERN_INFO "Removing Modulen");
}
Explanation / Answer
Piece modules must have no less than two capacities: a "begin" (introduction) work called init_module() which is called when the module is insmoded into the part, and an "end" (cleanup) work called cleanup_module() which is called just before it is rmmoded. Really, things have changed beginning with kernel.You can now utilize whatever name you like for the begin and end elements of a module, indeed, the new technique is the favored strategy. Nonetheless, many individuals still utilize init_module() and cleanup_module() for their begin and end capacities.
Regularly, init_module() either enlists a handler for something with the part, or it replaces one of the bit capacities with its own code (more often than not code to accomplish something and afterward call the first capacity). The cleanup_module() capacity should fix whatever init_module() did, so the module can be emptied securely.
In conclusion, each bit module needs to incorporate linux/module.h. We expected to incorporate linux/kernel.h just for the large scale development for the printk() log level, KERN_ALERT.
Compiling Kernel Modules
Bit modules should be arranged a bit uniquely in contrast to consistent userspace applications. Previous bit forms obliged us to think much about these settings, which are generally put away in Makefiles. Albeit progressively sorted out, numerous excess settings collected in sublevel Makefiles and made them extensive and somewhat hard to keep up. Luckily, there is another method for doing these things, called kbuild, and the manufacture procedure for outer loadable modules is presently completely incorporated into the standard bit assemble system.