Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Linux kernel module creation and installation

1. Create a new directory for your module and navigate to it.


mkdir my_module cd my_module
2. Create a new C source file for your module.
touch my_module.c
3. Open the source file in a text editor and add the necessary module code.
nano my_module.c
4. Save and close the file.
5. Create a makefile for the module.
touch Makefile

nano Makefile
6. Add the following code to the makefile:
makefile
obj-m += my_module.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


7. Save and close the makefile.
8. Compile the module using the make command.
make
9. Install the module using the insmod command.
sudo insmod my_module.ko
10. Verify that the module has been installed using the lsmod command.
lsmod | grep my_module
11. If you need to remove the module, use the rmmod command.
sudo rmmod my_module
Examples of a Linux kernel module creation and installation

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:

 Students should include the necessary header files.


 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.

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:

 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.

Exercise 3: Create a kernel module that prints the Fibonacci sequence.

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.

You might also like