需严格匹配内核版本并验证/lib/modules/$(uname -r)/build指向有效源码树,否则编译报错;编写适配4.19.90+的最小字符设备驱动,使用标准file_operations接口替代已废弃的class_simple。

在银河麒麟OS上为自定义硬件设备编写内核模块驱动,需严格匹配当前运行内核版本、启用正确的编译环境、处理符号导出与模块签名限制,否则编译会失败或加载时提示“Unknown symbol in module”“module verification failed”等致命错误。
确认内核版本与头文件路径
这一步不能跳过,否则后续所有编译都会报 【Unable to find kernel source tree】 错误。银河麒麟V10/V10SPx 的内核头文件路径不统一,/usr/src/kernels/ 下可能有多个目录,但只有软链接 /lib/modules/$(uname -r)/build 指向的那个才是真实可用的源码树。
执行以下命令验证:
uname -r → 输出类似 4.19.90-24.4.v2101.ky10.x86_64
ls -l /lib/modules/$(uname -r)/build → 必须指向一个真实存在的目录,且该目录下要有 Makefile 和 include/ 子目录
若返回 “No such file or directory”,说明 kernel-devel 包未安装或版本不匹配,必须立即补装:sudo yum install -y kernel-devel-$(uname -r)
编写最简字符设备驱动(hello_world.c)
从最小可运行单元开始,避免一上来就写复杂寄存器操作导致调试无从下手。以下代码已适配银河麒麟V10 SP2+ 内核(4.19.90+),删除了旧版内核中已废弃的 class_simple 接口。
创建 hello_world.c:
#include
#include
#include
#include
#include
#define DEVICE_NAME "hello_dev"
#define CLASS_NAME "hello_class"
static int major;
static struct class* hello_class = NULL;
static struct device* hello_device = NULL;
static int dev_open(struct inode *inodep, struct file *filep) {
printk(KERN_INFO "hello_dev: Device opened\n");
return 0;
}
static ssize_t dev_read(struct file *filep, char __user *buffer, size_t len, loff_t *offset) {
const char msg[] = "Hello from Kylin kernel!\n";
if (*offset >= sizeof(msg)) return 0;
if (copy_to_user(buffer, msg + *offset, sizeof(msg) - *offset))
return -EFAULT;
*offset += sizeof(msg) - *offset;
return sizeof(msg) - *offset;
}
static int dev_release(struct inode *inodep, struct file *filep) {
printk(KERN_INFO "hello_dev: Device closed\n");
return 0;
}
static const struct file_operations fops = {
.owner = THIS_MODULE,
.open = dev_open,
.read = dev_read,
.release = dev_release,
};
static int __init hello_init(void) {
major = register_chrdev(0, DEVICE_NAME, &fops);
if (major printk(KERN_ALERT "hello_dev: Failed to register major number\n");
return major;
}
printk(KERN_INFO "hello_dev: Registered with major number %d\n", major);
hello_class = class_create(THIS_MODULE, CLASS_NAME);
if (IS_ERR(hello_class)) {
unregister_chrdev(major, DEVICE_NAME);
return PTR_ERR(hello_class);
}
hello_device = device_create(hello_class, NULL, MKDEV(major, 0), NULL, DEVICE_NAME);
if (IS_ERR(hello_device)) {
class_destroy(hello_class);
unregister_chrdev(major, DEVICE_NAME);
return PTR_ERR(hello_device);
}
return 0;
}
static void __exit hello_exit(void) {
device_destroy(hello_class, MKDEV(major, 0));
class_unregister(hello_class);
class_destroy(hello_class);
unregister_chrdev(major, DEVICE_NAME);
printk(KERN_INFO "hello_dev: Unloaded\n");
}
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Kylin Driver Dev");
MODULE_DESCRIPTION("A minimal character device for Kylin OS");
module_init(hello_init);
module_exit(hello_exit);
编写Makefile并编译模块
银河麒麟默认禁用未签名模块加载,因此编译阶段必须使用系统当前内核的构建环境,且 Makefile 中的 KDIR 路径必须与 /lib/modules/$(uname -r)/build 完全一致,否则会链接到错误头文件,导致编译通过但加载失败。
创建 Makefile:
obj-m += hello_world.o
KDIR := /lib/modules/$(shell uname -r)/build
all:
make -C $(KDIR) M=$(PWD) modules
clean:
make -C $(KDIR) M=$(PWD) clean
执行 make 编译,成功后生成 hello_world.ko 文件。若报错 “error: implicit declaration of function ‘class_create’”,说明内核头文件路径错误或 kernel-devel 版本不匹配。
签名与加载驱动模块
银河麒麟V10 SP2+ 默认启用 Secure Boot 或 MODULE_SIG_FORCE,未签名模块会被拒绝加载,dmesg 中显示 “module verification failed: signature and/or required key missing”。必须签名才能加载。
方法一:使用系统已有的签名密钥(推荐)
sudo /usr/src/kernels/$(uname -r)/scripts/sign-file sha256 \
/usr/src/kernels/$(uname -r)/certs/signing_key.pem \
/usr/src/kernels/$(uname -r)/certs/signing_key.x509 \
hello_world.ko
方法二:临时禁用模块签名检查(仅测试环境)
echo 'options kvm-intel ignore_msrs=1' | sudo tee /etc/modprobe.d/disable-modsig.conf
sudo dracut -f
sudo reboot
⚠️ 注意:【禁用签名检查会降低系统安全性,生产环境严禁使用】
签名完成后执行:
sudo insmod hello_world.ko
dmesg | tail -5 → 应看到 “Registered with major number” 成功日志
ls /dev/hello_dev → 设备节点应存在
设置开机自动加载
第一步:将模块复制到标准路径
sudo cp hello_world.ko /lib/modules/$(uname -r)/extra/
sudo depmod -a
第二步:写入模块名到配置文件
echo "hello_world" | sudo tee /etc/modules-load.d/hello_world.conf
第三步:(可选)添加别名与权限规则
echo 'KERNEL=="hello_dev", MODE="0666", GROUP="wheel"' | sudo tee /etc/udev/rules.d/99-hello-dev.rules
sudo udevadm control --reload-rules
重启后执行 lsmod | grep hello_world,确认模块已自动加载。


















