잡동사니/GPT가 알려주는
[GPT가 알려주는] 모듈 커널에 적재
heylo
2025. 4. 4. 17:57
1.
✅ 2. 커널 헤더(kernel headers)란?
커널이 제공하는 데이터 구조, 함수, 상수, 인터페이스 정의 파일들 (.h 파일들)
- 우리가 커널 모듈(.ko)을 컴파일할 때 꼭 필요해.
- /usr/src/linux-headers-$(uname -r)/ 경로에 설치돼.
✅ 3. 커널 헤더 설치하기
3-1.
$ sudo apt-get update
[sudo] password for joo: Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease Get:2 http://security.ubuntu.com/ubuntu jammy-security InRelease [129 kB] Get:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [128 kB] Get:4 http://archive.ubuntu.com/ubuntu jammy-backports InRelease [127 kB] Get:5 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages [2212 kB] Get:6 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [2460 kB] Get:7 http://security.ubuntu.com/ubuntu jammy-security/main Translation-en [340 kB] Get:8 http://archive.ubuntu.com/ubuntu jammy-updates/main Translation-en [404 kB] Get:9 http://security.ubuntu.com/ubuntu jammy-security/main amd64 c-n-f Metadata [13.6 kB] Get:10 http://security.ubuntu.com/ubuntu jammy-security/restricted amd64 Packages [3127 kB] Get:11 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 c-n-f Metadata [18.5 kB] Get:12 http://security.ubuntu.com/ubuntu jammy-security/restricted Translation-en [556 kB] Get:13 http://archive.ubuntu.com/ubuntu jammy-updates/restricted amd64 Packages [3259 kB] Get:14 http://security.ubuntu.com/ubuntu jammy-security/universe amd64 Packages [970 kB] Get:15 http://archive.ubuntu.com/ubuntu jammy-updates/restricted Translation-en [577 kB] Get:16 http://security.ubuntu.com/ubuntu jammy-security/universe Translation-en [208 kB] Get:17 http://archive.ubuntu.com/ubuntu jammy-updates/universe amd64 Packages [1198 kB] Get:18 http://archive.ubuntu.com/ubuntu jammy-updates/universe Translation-en [295 kB] Fetched 16.0 MB in 6s (2472 kB/s) Reading package lists... Done
3-2.
sudo apt-get install linux-headers-$(uname -r)
- uname -r → 현재 사용 중인 커널 버전 출력
- 그 버전에 맞는 헤더를 설치함!
3-3.
$ uname -r
5.15.0-84-generic
✅ 4. 커널 모듈 실습 해보기 💥 (진짜 간단 버전)
📄 1단계: 예제 모듈 코드 만들기 (hello.c)
// hello.c
#include <linux/module.h>
#include <linux/kernel.h>
int init_module(void) {
printk(KERN_INFO "Hello, kernel!\n");
return 0;
}
void cleanup_module(void) {
printk(KERN_INFO "Goodbye, kernel!\n");
}
MODULE_LICENSE("GPL");
📄 2단계: Makefile 만들기
# Makefile
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
📦 3단계: 모듈 빌드
make
결과: hello.ko 파일 생김 → 이게 커널 모듈!
🚀 4단계: 커널에 모듈 적재 & 제거
sudo insmod hello.ko # 모듈 적재 (insert module)
sudo rmmod hello # 모듈 제거 (remove module)
dmesg | tail # 커널 로그 확인
출력:
Hello, kernel!
Goodbye, kernel!
🧠 정리 요약
apt-get | 패키지 설치 도구 (sudo 필요) |
커널 헤더 | 커널 함수/구조 정의 헤더 파일, 모듈 빌드시 필요 |
insmod | 커널에 모듈 넣기 |
rmmod | 모듈 제거하기 |
dmesg | 커널 로그 보기 (printk 확인용) |
✅ 바로 실습할 준비 됐으면?
내가 hello.c + Makefile을 자동 생성해주는 스크립트 줄 수도 있어
그리고 insmod, rmmod 한 번에 다 해주는 run.sh도 만들어줄게.
지금부터 한 번에 자동으로:
- hello.c 커널 모듈 파일 생성
- Makefile 생성
- run.sh 스크립트로 make, insmod, dmesg, rmmod 전부 자동화
✅ 1. 작업 디렉토리 만들기
mkdir ~/kernel_hello
cd ~/kernel_hello
✅ 2. 자동 생성 스크립트 실행
cat << 'EOF' > hello.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("joo");
MODULE_DESCRIPTION("Hello kernel module example");
static int __init hello_init(void) {
printk(KERN_INFO "👋 Hello, Kernel Module!\n");
return 0;
}
static void __exit hello_exit(void) {
printk(KERN_INFO "👋 Goodbye, Kernel Module!\n");
}
module_init(hello_init);
module_exit(hello_exit); EOF
cat << 'EOF' > Makefile
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 EOF
cat << 'EOF' > run.sh
#!/bin/bash
set -e
echo "[1] 빌드 중..."
make
echo "[2] 모듈 적재 중 (insmod)..."
sudo insmod hello.ko
echo "[3] 커널 메시지:"
dmesg | tail -n 10
echo "[4] 모듈 제거 중 (rmmod)..."
sudo rmmod hello
echo "[5] 커널 메시지:"
dmesg | tail -n 10
echo "[✅ 완료!]"
EOF
chmod +x run.sh
✅ 3. 실행하기!
./run.sh