#include
int init_func(void)
{
printk("Hello world 1.\n");
return 0;
}
void cleanup_func(void)
{
printk("Goodbye world 1.\n");
}
module_init(init_func);
module_exit(cleanup_func);
where module_init() and module_exit are macros that label the certain functions as initializing and exiting.
Now create a Makefile
obj-m += helloworld.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
if you forget to give helloworld.o and give it as helloworld.c then you will get an error saying
"target /home/vamshi/helloworld.c doesn't match the target pattern Building modules, stage 2." so be careful.
then run the command "make" for compiling the helloworld module.
Loading a Module
=============
modprobe can be used to load a module. "modprobe" is a program to add or remove modules from the kernel module.but this requires the module to be installed in /lib/modules/
insmod -- Installs a loadable kernel module in running linux kernel.
so to load the helloworld module run the command
# insmod helloworld.ko
After loading your module the function registered as the initial function will be run. Try running the dmesg command. You should see a long message ending similar to the following:
# dmesg
...
VFS: Mounted root (reiserfs filesystem) readonly.
Freeing unused kernel memory: 144k freed
eth0: link up
Hello world!
To get the list of currently loaded modules we can use the command "lsmod"
"modinfo" is a command to print out the information about one or more modules.
"rmmod" this command is to unload a module.
#rmmod helloworld.ko
0 comments:
Post a Comment