Professional Documents
Culture Documents
Creation Et Installation de Modules Noyau Linux
Creation Et Installation de Modules Noyau Linux
nano Makefile
6. Add the following code to the makefile:
makefile
obj-m += my_module.o
1. Creating a Hello World Module: Open a text editor and create a new file with the following code:
#include <linux/init .h >
#include <linux/module .h >
static int hello_init (void)
{
printk (KERN_ALERT "Hello, world!\n");
return 0 ;
}
static void hello_exit (void)
{ printk (KERN_ALERT "Goodbye, cruel world!\n"); }
module_init (hello_init); module_exit (hello_exit);
MODULE_LICENSE ("GPL");
MODULE_AUTHOR ("Your Name");
MODULE_DESCRIPTION ("A simple hello world module");
MODULE_VERSION (" 1.0 ");
Save the file with the extension .c (e.g. hello.c)
4. Compiling the Module:
Open a terminal and navigate to the directory where the hello.c file is located.
Run the following command to compile the module:
make -C /lib/modules/$( uname -r)/build M= $PWD modules
5. Installing the Module:
Run the following command to install the module:
sudo insmod hello.ko
Check that the module is loaded by running the command:
lsmod | grep hello
You should see output similar to the following:
hello 16384 0
6. Removing the Module:
Run the following command to remove the module:
sudo rmmod hello
Check that the module has been unloaded by running the command:
lsmod | grep hello
You should not see any output.
Exercices
Exercise 1: Create a kernel module that prints "Hello, World!" when loaded.
arduinoCopy code
#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_AUTHOR ( "Your Name" );
MODULE_DESCRIPTION ( "Hello World module" );
Corrections:
Exercise 2: Create a kernel module that adds two numbers and prints the result.
arduinoCopy code
#include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h> static int a = 2 ;
static int b = 3 ; static int __init sum_init(void) { int sum = a + b; printk (KERN_INFO "The sum of
%d and %d is %d\n" , a, b, sum); return 0 ; } static void __exit sum_exit(void) {
printk (KERN_INFO "Goodbye, World!\n" ); } module_init (sum_init); module_exit (sum_exit);
MODULE_LICENSE ( "GPL" ); MODULE_AUTHOR ( "Your Name" );
MODULE_DESCRIPTION ( "Sum module" );
Corrections:
arduinoCopy code
#include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h> static int a = 0 ;
static int b = 1 ; static int __init fib_init(void) { int i, c; for (i = 0 ; i < 10 ; i++) { c = a + b;
printk (KERN_INFO "%d\n" , c); a = b; b = c; } return 0 ; } static void __exit fib_exit(void) {
printk (KERN_INFO "Goodbye, World!\n" ); } module_init (fib_init); module_exit (fib_exit);
MODULE_LICENSE ( "GPL" ); MODULE_AUTHOR ( "Your Name" );
MODULE_DESCRIPTION ( "Fibonacci module" );
Corrections:
Students should define the module license, author, and description.
Students should use printk instead of printf to print messages.
Students should use module_init and module_exit to define the initialization and cleanup functions.
Students should use appropriate __init and __exit macros to mark the functions.
Students should use proper variable names and types.
Students should print the Fibonacci sequence correctly.