国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
How to: Compile Linux kernel modules

How to: Compile Linux kernel modules

by LinuxTitli · 36 comments

This is one the essential and important task. Many time we upgradeour kernel and some precompiled drivers won't work with Linux.Especially if you have weird hardware; then vendor may send you drivercode aka C files to compile. Or even you can write your own Linux kerneldriver. Compiling kernel driver is easy. Kernel 2.6.xx makes it evenmuch more easier. Following steps are required to compile driver asmodule:

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 kernelmodules / 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 simplycreate the Makefile file as follows (assuming your kernel module nameis 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 cansee all actual compile command stored in .foo* files in same directory.

5) Once module compiled successfully, load it using insmod ormodprobe command. You need to be root user or privileged user to runinsmod:
# 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 toload kernel boot time. This file should contain the names of kernelmodules that are to be loaded at boot time, one per line. First copyyour module to /lib/modules/$(uname -r)/kernel/drivers. Following aresuggested 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, modules
  • 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.

本站僅提供存儲服務,所有內容均由用戶發(fā)布,如發(fā)現有害或侵權內容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Ubuntu make fails with No such file or directory /lib/modules/3.4.0/build
Ubuntu12.10 內核源碼外編譯 linux模塊--編譯驅動模塊的基本方法
ubuntu 2.6.22 內核樹建立
linux documents--modules.txt
hello內核模塊編譯的全過程
內核模塊HelloKernel編程實例詳解
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯系客服!

聯系客服