Hello World Kernel
file hello.c
#include <linux/module.h>
#include <linux/init.h>
#define HELLO_AUTHOR "Rajesh6115 <sahoorajesh.d@gmail.com>"
#define HELLO_DESC "A Simple Hello World Module"
static int __init hello_init(void);
static void __exit hello_exit(void);
MODULE_LICENSE("GPL");
MODULE_AUTHOR(HELLO_AUTHOR); /* Who wrote this module? */
MODULE_DESCRIPTION(HELLO_DESC); /* What does this module do */
static int __init hello_init(void){
printk(KERN_INFO "Hello World Linux Kernel --------INIT Message");
return 0;
}
static void __exit hello_exit(void){
printk(KERN_INFO "Good Bye Linux Kernel --------EXIT Message");
}
module_init(hello_init);
module_exit(hello_exit);
BUILD AND RUNNIG A Hello MODULE
For Building a Module We Normally Take the help of Kernel Build Script.which present in '/lib/modules/<kernel version>/build' path.
Make File for Hello Module
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
Compile by make command
Out put in my system
rajesh@ubuntu:~/rajesh/kernelmod/hello$ make
make -C /lib/modules/3.12.6-rajesh-first/build M=/home/rajesh/rajesh/kernelmod/hello modules
make[1]: Entering directory `/host/linux-3.12.6'
CC [M] /home/rajesh/rajesh/kernelmod/hello/hello.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/rajesh/rajesh/kernelmod/hello/hello.mod.o
LD [M] /home/rajesh/rajesh/kernelmod/hello/hello.ko
make[1]: Leaving directory `/host/linux-3.12.6'
rajesh@ubuntu:~/rajesh/kernelmod/hello$
Some New files will generate in same directoryAnd our driver file is 'hello.ko'
Run/Load the module by using 'insmod' Command
To execute that Command you should be root user or use sudo to execute
rajesh@ubuntu:~/rajesh/kernelmod/hello$ sudo insmod ./hello.ko
[sudo] password for rajesh:
rajesh@ubuntu:~/rajesh/kernelmod/hello$
'printk' function log the message to kernel log file.And we can see the same by 'dmesg' commandrajesh@ubuntu:~/rajesh/kernelmod/hello$ dmesg | tail -5
[ 1094.480372] audit_printk_skb: 177 callbacks suppressed
[ 1094.480375] type=1400 audit(1392792445.131:71): apparmor="DENIED" operation="capable" parent=1 profile="/usr/sbin/cupsd" pid=797 comm="cupsd" pid=797 comm="cupsd" capability=36 capname="block_suspend"
[ 1599.091190] perf samples too long (2510 > 2500), lowering kernel.perf_event_max_sample_rate to 50000
[ 6380.140339] Hello World Linux Kernel --------INIT Message
Unload/Remove the module by using 'rmmod' Command
To execute that Command you should be root user or use sudo to execute
rajesh@ubuntu:~/rajesh/kernelmod/hello$ sudo rmmod hello
Again we will see exit message using 'dmesg'rajesh@ubuntu:~/rajesh/kernelmod/hello$ dmesg | tail -5
[ 1094.480375] type=1400 audit(1392792445.131:71): apparmor="DENIED" operation="capable" parent=1 profile="/usr/sbin/cupsd" pid=797 comm="cupsd" pid=797 comm="cupsd" capability=36 capname="block_suspend"
[ 1599.091190] perf samples too long (2510 > 2500), lowering kernel.perf_event_max_sample_rate to 50000
[ 6380.140339] Hello World Linux Kernel
[ 6423.920972] Good Bye Linux Kernel --------EXIT Message
No comments:
Post a Comment