Linux/Embedded
Basic Module Programming
쓱은감자
2022. 9. 13. 17:39
Basic Module Programming 실습
Source Code & Makefile 작성
- 작업 디렉터리 생성(위치는 아무곳이나 상관 없음)
$ mkdir module
$ cd module
$ mkdir hellomodule
- Source Code 작성
- 생성한 hellomodule 디렉터리 밑에 생성(hellomodule.c)
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
MODULE_LICENSE("GPL")
int module_start(void)
{
printk("Module init\n");
printk("Hello, Linux Module ...\n");
return 0;
}
int module_end(void)
{
printk("Module Clean Up\n");
}
module_init(module_start); // module programming은 따로 main이 없이 정해진 형식에 맞춰 작성,
module_exit(module_end); // init, exit는 필수 항목이고 작성한 함수를 인자로 추가
- Makefile 작성
- 생성한 hellomodule 디렉터리 밑에 생성(Makefile)
obj-m += hellomodule.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
all :
make -C $(KDIR) M=$(PWD) modules
clean :
rm -rf hellomodule.o hellomodule.mod.* hellomodule.ko
빌드 및 결과 확인
- 빌드
$ cd module/hellomodule
$ make
$ ls
- module 설치 및 결과 확인
$ insmod hellomodule.ko
$ lsmod | head -n 5
$ dmsg | tail -n 5
$ rmmod hellomodule
$ dmesg | tail -n 5