c - 如何编写用户空间 linux block 设备驱动程序?

标签 c linux compiler-errors linux-device-driver

我想编写一个 linux block 设备驱动程序。驱动程序不需要访问硬件,因此它可以在用户空间中。

首先,我尝试构建 an example block device driver使用这个 Makefile:

obj-m = sbd.o
KVERSION = $(shell pwd)
PWD = $(shell pwd)

all:
    make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules

但是,我遇到了这些我不知道如何修复的错误。这是标准输出和标准错误:

make -C /lib/modules/2.6.31-19-generic/build M=/home/andreas/sp/nivoa/src/driver/sbd modules
make[1]: Entering directory `/usr/src/linux-headers-2.6.31-19-generic'
  CC [M]  /home/andreas/sp/nivoa/src/driver/sbd/sbd.o
/home/andreas/sp/nivoa/src/driver/sbd/sbd.c:72: error: expected ‘)’ before ‘*’ token
/home/andreas/sp/nivoa/src/driver/sbd/sbd.c:128: warning: initialization from incompatible pointer type
/home/andreas/sp/nivoa/src/driver/sbd/sbd.c: In function ‘sbd_init’:
/home/andreas/sp/nivoa/src/driver/sbd/sbd.c:143: error: ‘sbd_request’ undeclared (first use in this function)
/home/andreas/sp/nivoa/src/driver/sbd/sbd.c:143: error: (Each undeclared identifier is reported only once
/home/andreas/sp/nivoa/src/driver/sbd/sbd.c:143: error: for each function it appears in.)
/home/andreas/sp/nivoa/src/driver/sbd/sbd.c:146: error: implicit declaration of function ‘blk_queue_hardsect_size’
make[2]: *** [/home/andreas/sp/nivoa/src/driver/sbd/sbd.o] Error 1
make[1]: *** [_module_/home/andreas/sp/nivoa/src/driver/sbd] Error 2
make[1]: Leaving directory `/usr/src/linux-headers-2.6.31-19-generic'
make: *** [all] Error 2

如有任何帮助,我们将不胜感激。

谢谢, 安德烈亚斯

最佳答案

在用户空间中没有一个“官方”的方法来做 block 驱动,但是人们经常通过(滥用)使用 NBD 驱动通过环回网络与守护进程交谈,守护进程在普通套接字上监听并说出NBD 协议(protocol)。有关详细信息,请参阅 NBD 文档。

您的示例是针对内核模式 block 设备的,需要将其构建为内核模块。由于内核内部总是在变化,它现在可能是不兼容的。

关于c - 如何编写用户空间 linux block 设备驱动程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2266677/

相关文章:

c++ - C 中移位运算符的真正强大用途是什么?

linux - 为 CentOS 移植一个 debian 包到 YUM

linux - 用于获取操作系统版本的 Shell 脚本

c# - 从C#调用VB.Net函数

c - 如何使用 fseek() 正确移动 fread() 函数中的指针? (C)

c++ - 是否有工具/解决方案来编写一个循环,其中仅在每 X 次迭代中检查条件?

c - 无法指定矩阵乘法的起始行和结束行

linux - RHEL 7 上的恶魔功能

c++ - GCC编译器在查找头文件时不搜索子目录

c# - 运行时编译错误