My first kernel module

hello_world.c

#include <linux/module.h>
#include <linux/init.h>

static __exit void hello_world_exit(void)
{
	printk(KERN_DEBUG "Bye Bye World!\n");
}

static __init int hello_world_init(void)
{
	printk(KERN_DEBUG "Hello World!\n");
	return 0;
}

module_init(hello_world_init);
module_exit(hello_world_exit);

Makefile

ifneq (${KERNELRELEASE},)                                                        
	obj-m := hello_world.o
else
KERNELDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) clean 
endif

Command to load module:

sudo insmod hello_world.ko

Command to unload module:

sudo rmmod hello_world.ko
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s