티스토리 뷰

OS/Linux

[Ubuntu] Module Programming

후야 2017.05.09 23:27

Ubuntu 16.04.02 (64bit)


1. Makefile

Makefile 은 아래와 같이 작성 가능

ifneq ($(KERNELRELEASE),)
	obj-m := hello.o
else
	KERNELDIR ?= /lib/modules/$(shell uname -r)/build
	PWD := $(shell pwd)
default:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
	rm *.o *.mod.c *.symvers *.order
endif

clean:
	rm *.ko *.o *.mod.c *.symvers *.order


2. Test Module Code

code 작성
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");

static int who_init_module(void) {
	//printk("<1> Hello, world\n");
	printk(KERN_ALERT "Hello, world\n");
	return 0;
}

static void who_exit_module(void) {
	//printk("<1> Goodbye, world\n");
	printk(KERN_ALERT "Goodbye, world\n");
}

module_init(who_init_module);
module_exit(who_exit_module);


3. Test

make

sudo insmod hello.ko
lsmod | grep hello
dmesg | tail

sudo rmmod hello.ko
lsmod | grep hello
dmesg | tail


댓글
댓글쓰기 폼