0% found this document useful (0 votes)
38 views

Load Kernel Module

This document provides instructions to create a simple "Hello World" kernel module in Linux: 1. It first installs the kernel headers for the running kernel version and changes to that directory. 2. It then provides C code for a simple module that prints a message on initialization and exit. 3. A Makefile is created to build and clean the module. 4. The module is then built, installed into the running kernel, and the dmesg output is checked to verify the messages were printed.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Load Kernel Module

This document provides instructions to create a simple "Hello World" kernel module in Linux: 1. It first installs the kernel headers for the running kernel version and changes to that directory. 2. It then provides C code for a simple module that prints a message on initialization and exit. 3. A Makefile is created to build and clean the module. 4. The module is then built, installed into the running kernel, and the dmesg output is checked to verify the messages were printed.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

sudo apt-get update

apt-cache search linux-headers-$(uname -r)


sudo apt-get install (the version you get from the command above
for example:linux-headers-3.16.0-4-amd64)
cd /usr/src/linux-headers-3.16.0-4-amd64/
ls
- now go to Desktop
mkdir module
cd module
gedit hello.c
insert the following code:
////////////////////////////////
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/sched.h>

static int __init hello_init(void)


{
printk(KERN_INFO "Hello World! pid = %d\n", current->pid);
return 0;
}

static void __exit hello_exit(void)


{
printk(KERN_INFO "Hello Bye!\n");
}

module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Amro Okasha");
MODULE_DESCRIPTION("Test Module: Hello World");
MODULE_VERSION("1.0");
////////////////////////////////
touch Makefile
gedit Makefile
insert the following code:
////////////////////////////////
obj-m += hello.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
////////////////////////////////
sudo dmesg
make
sudo insmod kernal.ko
lsmod
sudo dmesg

You might also like