如何建立你的第一个linux模块驱动
2013-05-29 13:52
375 查看
转载:http://www.cyberciti.biz/tips/compiling-linux-kernel-module.html
This is one the essential and important task. Many time we upgrade our kernel and some precompiled drivers won't work with Linux. Especially if you have weird hardware; then vendor may send you driver code aka C files to compile. Or even you can write your
own Linux kernel driver. Compiling kernel driver is easy. Kernel 2.6.xx makes it even much more easier. Following steps are required to compile driver as module:
1) You need running kernel source code; if you don't have a source code download it from
kernel.org. Untar kernel source code (tar ball) in /usr/src using tar command:
To be frank kernel headers are more than sufficient to compile kernel modules / drivers. See how to install kernel headers under
Debian / Ubuntu Linux or
RHEL / CentOS / Fedora Linux.
2) Next go to your kernel module source code directory and simply create the Makefile file as follows (assuming your kernel module name is foo):
3) Add following text to it:
4) Compile module using make command (module build can be done by any user) :
It will finally creates the foo.ko module in current directory. You can see all actual compile command stored in .foo* files in same directory.
5) Once module compiled successfully, load it using insmod or modprobe command. You need to be root user or privileged user to run insmod:
2)Add following c source code to it:
This is an example modified from original
source for demonstration purpose.
3) Save the file. Create new Makefile as follows:
Append following make commands:
4) Save and close the file.
5) Compile hello.c module:
6) Become a root user (use su or sudo) and load the module:
Note you can see message on screen if you are logged in as root under run level 3.
7) Verify that module loaded:
8) See message in /var/log/message file:
9) Unload the module:
10) Load module when Linux system comes up. File /etc/modules use to load kernel boot time. This file should contain the names of kernel modules that are to be loaded at boot time, one per line. First copy your module to /lib/modules/$(uname -r)/kernel/drivers.
Following are suggested steps:
(a) Create directory for hello module:
(b) Copy module:
(c) Edit /etc/modules file under Debian Linux:
(d) Add following line to it:
(e) Reboot to see changes. Use lsmod or dmesg command to verify module loaded or not.
OR
Documentation located in your kernel source directory (for example /usr/src/linux-2.6.xx.xx/Documentation/) and README file located under kernel source code tree /usr/src/linux-2.6.xx.xx/README
Read TLDP.org tutorial online.
This is one the essential and important task. Many time we upgrade our kernel and some precompiled drivers won't work with Linux. Especially if you have weird hardware; then vendor may send you driver code aka C files to compile. Or even you can write your
own Linux kernel driver. Compiling kernel driver is easy. Kernel 2.6.xx makes it even much more easier. Following steps are required to compile driver as module:
1) You need running kernel source code; if you don't have a source code download it from
kernel.org. Untar kernel source code (tar ball) in /usr/src using tar command:
$ tar -zxvf kernel* -C /usr/src
To be frank kernel headers are more than sufficient to compile kernel modules / drivers. See how to install kernel headers under
Debian / Ubuntu Linux or
RHEL / CentOS / Fedora Linux.
2) Next go to your kernel module source code directory and simply create the Makefile file as follows (assuming your kernel module name is foo):
$ vi Makefile
3) Add following text to it:
obj-m = foo.o KVERSION = $(shell uname -r) all: make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules clean: make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean
4) Compile module using make command (module build can be done by any user) :
$ make
It will finally creates the foo.ko module in current directory. You can see all actual compile command stored in .foo* files in same directory.
5) Once module compiled successfully, load it using insmod or modprobe command. You need to be root user or privileged user to run insmod:
# insmod foo.ko
Example: hello.c module
1) hello.c C source code. Copy following code and save to hello.c$ mkdir demo; cd demo $ vi hello.c
2)Add following c source code to it:
#include <linux/module.h> /* Needed by all modules */ #include <linux/kernel.h> /* Needed for KERN_INFO */ #include <linux/init.h> /* Needed for the macros */ static int __init hello_start(void) { printk(KERN_INFO "Loading hello module...\n"); printk(KERN_INFO "Hello world\n"); return 0; } static void __exit hello_end(void) { printk(KERN_INFO "Goodbye Mr.\n"); } module_init(hello_start); module_exit(hello_end);
This is an example modified from original
source for demonstration purpose.
3) Save the file. Create new Makefile as follows:
$ vi Makefile
Append following make commands:
obj-m = hello.o KVERSION = $(shell uname -r) all: make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules clean: make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean
4) Save and close the file.
5) Compile hello.c module:
$ make
6) Become a root user (use su or sudo) and load the module:
$ su - # insmod hello.ko
Note you can see message on screen if you are logged in as root under run level 3.
7) Verify that module loaded:
# lsmod | less
8) See message in /var/log/message file:
# tail -f /var/log/message
9) Unload the module:
# rmmod hello
10) Load module when Linux system comes up. File /etc/modules use to load kernel boot time. This file should contain the names of kernel modules that are to be loaded at boot time, one per line. First copy your module to /lib/modules/$(uname -r)/kernel/drivers.
Following are suggested steps:
(a) Create directory for hello module:
# mkdir -p /lib/modules/$(uname -r)/kernel/drivers/hello
(b) Copy module:
# cp hello.ko /lib/modules/$(uname -r)/kernel/drivers/hello/
(c) Edit /etc/modules file under Debian Linux:
# vi /etc/modules
(d) Add following line to it:
hello
(e) Reboot to see changes. Use lsmod or dmesg command to verify module loaded or not.
# cat /proc/modules
OR
# lsmod | less
See also:
Read man pages of lsmod, rmmod, modprobe, modulesDocumentation located in your kernel source directory (for example /usr/src/linux-2.6.xx.xx/Documentation/) and README file located under kernel source code tree /usr/src/linux-2.6.xx.xx/README
Read TLDP.org tutorial online.
相关文章推荐
- 如何编译linux第一个模块 hello.c
- 【ZYNQ_LINUX】如何把自己的驱动编译进内核或模块(Kconfig和Makefile
- 如何编译linux第一个模块 hello.c
- Linux设备驱动3快速参考汇总--建立和运行模块
- 如何编译linux第一个模块 hellomod.ko
- rhel/linux下如何找出网卡驱动的内核模块名称
- 如何查看LINUX下的一个USB设备使用的驱动模块
- 如何编译linux第一个模块 hello.c
- 从第一个linux模块做起—字符驱动
- 如何获得Linux系统的内置模块和设备驱动列表
- 如何编译linux第一个模块 hello.c(Centos5.3)
- 如何编译linux第一个模块 hellomod.ko 分类: arm-linux-Ubuntu 2013-07-22 16:30 286人阅读 评论(0) 收藏
- 如何编译linux第一个模块 hellomod.ko
- Linux 内核模块编程的第一个字符设备驱动
- UBuntu8.10 开发第一个基本Linux 驱动内核模块
- 如何编译linux第一个模块 hellomod.ko
- 【Linux开发】如何查看Linux kernel的内置模块驱动列表和进程ID
- volcanol_Linux_ 问题汇总系列_4_Thinkpad_E40_0578MDC_在Fedora 13 Linux(FC13)中如何安装无线网卡驱动
- 手把手教你如何建立自己的Linux系统(转)(二)