


실제 장치

가상 장치

| 구분 | c (Character) | b (Block) |
|---|---|---|
| 데이터 단위 | Byte 단위 스트림 | Block 단위 (512B, 4KB 등) |
| 접근 방식 | 순차 접근 | 임의 위치 접근 가능 |
| Buffer/Cache | 대부분 바로 전달 | 커널이 Cache 후 처리 |
| 실제 장치 | UART, I2C, 키보드, GPIO | SD카드, SSD, USB 메모리 |
| 가상 장치 | /dev/null, /dev/zero, /dev/random | loop0 ~ loop7 |
ls -l /dev | cut -c1 | sort | uniq -c
ls -l 결과에서 크기 자리에 주번호, 부번호가 표시된다.
crw-rw---- 1 root i2c 89, 20 ... i2c-20
brw-rw---- 1 root disk 7, 0 ... loop0
형식
sudo mknod [파일명] [파일종류] [Major] [Minor]
생성 → 확인
sudo mknod /dev/deviceFile c 100 0
ls -al /dev/deviceFile
ls -al 결과 맨 앞이 c, 크기 자리가 100, 0이면 정상이다.~/test2 폴더에 devicedriver.c, Makefile을 만든다.
devicedriver.c
#include <linux/module.h> // module_init 등 모듈 매크로
#include <linux/printk.h> // pr_info 등 커널 로그
#include <linux/init.h> // __init / __exit 매크로
#include <linux/fs.h> // file_operations, register_chrdev
#define NOD_MAJOR 100 // mknod로 만든 장치 파일의 Major 번호
#define NOD_NAME "deviceFile" // 등록할 Character Device 이름
MODULE_LICENSE("GPL");
// deviceFile에 open() 호출 시 실행
static int deviceDriver_open(struct inode *inode, struct file *filp){
pr_info("Open Device\n");
return 0;
}
// deviceFile에 close() 호출 시 실행
static int deviceDriver_release(struct inode *inode, struct file *filp){
pr_info("Close Device\n");
return 0;
}
static struct file_operations fops = {
.owner = THIS_MODULE,
.open = deviceDriver_open,
.release = deviceDriver_release,
};
// insmod 시 실행
static int __init deviceDriver_init(void)
{
int ret = register_chrdev(NOD_MAJOR, NOD_NAME, &fops);
if( ret < 0 ){
pr_alert("Register File\n");
return ret;
}
pr_info("Insmod Module\n");
return 0;
}
// rmmod 시 실행
static void __exit deviceDriver_exit(void)
{
unregister_chrdev(NOD_MAJOR, NOD_NAME);
pr_info("Unload Module\n");
}
module_init(deviceDriver_init);
module_exit(deviceDriver_exit);
Makefile
KERNEL_HEADERS=/lib/modules/$(shell uname -r)/build
obj-m += devicedriver.o
PWD := $(CURDIR)
driver:
make -C $(KERNEL_HEADERS) M=$(PWD) modules
clean:
make -C $(KERNEL_HEADERS) M=$(PWD) clean
/lib/modules/$(uname -r)/build는 현재 실행 중인 커널의 소스(헤더) 위치를 가리킨다.빌드 → 적재 → 확인 → 해제
make
sudo insmod ./devicedriver.ko
sudo cat /dev/deviceFile
sudo rmmod devicedriver
dmesg | tail
커널 로그
Insmod Module
Open Device
Close Device
Unload Module

sudo dmesg -wH
journalctl -kf로도 같은 내용을 볼 수 있다.<linux/fs.h>static int deviceDriver_open(struct inode *inode, struct file *filp)

int register_chrdev(unsigned int major, const char *name, const struct file_operations *fops);
void unregister_chrdev(unsigned int major, const char *name);
등록 확인
cat /proc/devices | grep deviceFile
100 deviceFile
sudo reboot
ls -al /dev/deviceFile
ls: '/dev/deviceFile'에 접근할 수 없습니다: 그런 파일이나 디렉터리가 없습니다
~/test3 폴더에 devicedriver.c, Makefile(test2와 동일)을 만든다.
devicedriver.c
#include <linux/module.h>
#include <linux/printk.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/device.h> // class_create, device_create
#define NOD_NAME "deviceFile" // 만들 장치 파일 이름 (/dev/deviceFile)
MODULE_LICENSE("GPL");
static int NOD_MAJOR; // 커널이 할당한 Major 번호
static struct class *cls; // Device File을 관리할 class
static dev_t dev; // Major + Minor 장치 번호
static int deviceDriver_open(struct inode *inode, struct file *filp){
pr_info("Open Device\n");
return 0;
}
static int deviceDriver_release(struct inode *inode, struct file *filp){
pr_info("Close Device\n");
return 0;
}
static struct file_operations fops = {
.owner = THIS_MODULE,
.open = deviceDriver_open,
.release = deviceDriver_release,
};
static int __init deviceDriver_init(void)
{
// major를 0으로 주면 커널이 빈 번호를 할당해서 return
NOD_MAJOR = register_chrdev(0, NOD_NAME, &fops);
if( NOD_MAJOR < 0 ){
pr_alert("Register File\n");
return NOD_MAJOR;
}
pr_info("Insmod Module\n");
dev = MKDEV(NOD_MAJOR, 0); // 장치 번호 생성
cls = class_create(NOD_NAME); // class 생성
device_create(cls, NULL, dev, NULL, NOD_NAME); // /dev/deviceFile 생성
pr_info("Major number %d\n", NOD_MAJOR);
pr_info("Device file : /dev/%s\n", NOD_NAME);
return 0;
}
static void __exit deviceDriver_exit(void)
{
device_destroy(cls, dev); // /dev/deviceFile 삭제
class_destroy(cls); // class 삭제
unregister_chrdev(NOD_MAJOR, NOD_NAME); // 드라이버 해제
pr_info("Unload Module\n");
}
module_init(deviceDriver_init);
module_exit(deviceDriver_exit);
빌드 → 적재 → 확인 → 해제
make
sudo insmod ./devicedriver.ko
ls -al /dev/deviceFile
sudo cat /dev/deviceFile
sudo rmmod devicedriver
ls -al /dev/deviceFile
dmesg | tail
커널 로그
Insmod Module
Major number 2xx
Device file : /dev/deviceFile
Open Device
Close Device
Unload Module

<linux/device.h>NOD_MAJOR = register_chrdev(0, NOD_NAME, &fops);
dev_t MKDEV(major, minor)

struct class *class_create(const char *name);
uname -r로 커널 버전을 확인한다.struct device *device_create(struct class *class, struct device *parent,
dev_t devt, void *drvdata, const char *fmt, ...);

void device_destroy(struct class *class, dev_t devt);
void class_destroy(struct class *class);
| 구분 | mknod 방식 | device_create 방식 |
|---|---|---|
| Device File 생성 | 사용자가 mknod 실행 | insmod 시 드라이버가 생성 |
| Major 번호 | 직접 지정 (중복 위험) | 커널이 동적 할당 |
| 재부팅 후 | 파일 사라짐, 다시 mknod | insmod하면 다시 생성 |
| 모듈 해제 시 | 파일 남아 있음 | 파일 자동 삭제 |