編譯進內核的驅動怎麼卸載
A. linux怎麼卸載驅動
Linux下大部分驅動都是以模塊方式載入到內核的,所以要刪除一個驅動,主要從模塊下手。所以首先要學會如何查看已經載入的模塊:
lsmod
第一列是模塊的名字,根據模塊名字往往可以猜出哪個模塊是你要刪除的驅動。如果對硬體不熟悉的話就要試著通過其他命令來找相關的信息。
比如我要刪除無線網卡的驅動,那麼我可以用:
lspci | grep less
lspci命令會列出所有PCI設備,而grep會找出返回結果中包含less的行。無線是Wireless,所以返回結果就會包含有我的無線網卡的信息,比如:
02:00.0 Network controller: Realtek Semiconctor Co., Ltd. RTL8192E Wireless LAN Controller (rev 01)
可以看出型號是RTL8192E,這個時候再用lsmod命令,查看有沒有與8192相關的模塊名就可以了。
找到模塊名字後(比如我的無線網卡叫做r8192e_pci)就可以用如下命令卸載模塊:
rmmod 模塊名
對於我的無線網卡就是rmmod r8192e_pci。這時候驅動就已經成功被卸載了,但是是臨時的,重啟又會重新載入,如果要永久卸載這個驅動的話還需要改一點東西。
在Ubuntu下進入/etc/modprobe.d目錄下(其他發行版會有些差別),編輯blacklist.conf文件,加入一行(一般加在最下面):
blacklist 模塊名
這樣就將你要刪的驅動加入了黑名單,以後就再也見不到它啦!: )
B. linux 內核驅動問題
1,重新編譯內核浪費時間,可以考慮把fedora的it20驅動移植過來!參考網上的makefile寫個,自己編譯一下。把編譯得到的ko放到某目錄下, 在啟動腳本里載入它!
2, 如果想重新編譯內核也可以,把fedora9的it驅動替換你的驅動(把源文件名字改成一樣的),前提是你有內核源代碼!一般安裝完fedora後,在源碼里有個.config文件,這就是默認的配置文件,你也不許要再make menuconfig了,直接make oldconfig make 就行。
C. 如何編譯一個linux下的驅動模塊
Linux內核源碼路徑:/usr/src/linux(這個源碼是從kernel.org網站download的2.4.18版本)
按照《linux設備驅動開發詳解》一書中的步驟實現經典例子"hello,world!"的例子。
具體步驟如下:
=============================================
1.源碼如下:
/*
* hello.c -- the example of printf "hello world!" in the screen of driver program
*/
#include <linux/init.h>
#include <linux/mole.h>
MODULE_LICENSE("Dual BSD/GPL");/* declare the license of the mole ,it is necessary */
static int hello_init(void)
{
printk(KERN_ALERT "Hello World enter!\n");
return 0;
}
static int hello_exit(void)
{
printk(KERN_ALERT "Hello world exit!\n");
}
mole_init(hello_init); /* load the mole */
mole_exit(hello_exit); /* unload the mole */
進入目錄:
[root@Alex_linux /]#cd /work/jiakun_test/moletest
[root@Alex_linux moletest]# vi hello.c
然後拷入上面書上的源碼。
2.編譯代碼:
1>.首先我在2.4內核的虛擬機上進行編譯,編譯過程如下:
[root@Alex_linux moletest]#gcc -D__KERNEL__ -I /usr/src/linux -DMODULE -Wall -O2 -c -o hello.o hello.c
其中-I選項指定內河源碼,也就是內核源碼樹路徑。編譯結果:
hello.c:1:22: net/sock.h: No such file or directory
hello.c: In function `hello_init':
hello.c:6: warning: implicit declaration of function `printk'
hello.c:6: `KERN_ALERT' undeclared (first use in this function)
hello.c:6: (Each undeclared identifier is reported only once
hello.c:6: for each function it appears in.)
hello.c:6: parse error before string constant
hello.c: In function `hello_exit':
hello.c:11: `KERN_ALERT' undeclared (first use in this function)
hello.c:11: parse error before string constant
hello.c: At top level:
hello.c:13: warning: type defaults to `int' in declaration of `mole_init'
hello.c:13: warning: parameter names (without types) in function declaration
hello.c:13: warning: data definition has no type or storage class
hello.c:14: warning: type defaults to `int' in declaration of `mole_exit'
hello.c:14: warning: parameter names (without types) in function declaration
hello.c:14: warning: data definition has no type or storage class
在網上查詢有網友提示沒有引入kernel.h
解決:vi hello.c
在第一行加入:#include <linux/kernel.h>
再次編譯仍然報KERN_ALERT沒有聲明
修改編譯條件-I,再次編譯:
[root@Alex_linux moletest]#gcc -D__KERNEL__ -I /usr/src/linux -DMODULE -Wall -O2 -c -o hello.o hello.c
[root@Alex_linux moletest]#ls
hello.c hello.o Makefile
[root@Alex_linux moletest]#
2>.接著我嘗試在2.6內核的虛擬機上進行編譯
編譯過程如下:
[root@JiaKun moletest]# ls
hello.c makefile
[root@JiaKun moletest]# vi hello.c
[root@JiaKun moletest]# make
make -C /mylinux/kernel/2.4.18-rmk7 M=/home/alex/test/moletest moles
make: *** /mylinux/kernel/2.4.18-rmk7: No such file or directory. Stop.
make: *** [moles] Error 2
[root@JiaKun moletest]# vi makefile
[root@JiaKun moletest]# make
make -C /usr/src/kernels/2.6.18-53.el5-i686 M=/home/alex/test/moletest moles
make[1]: Entering directory `/usr/src/kernels/2.6.18-53.el5-i686'
scripts/Makefile.build:17: /home/alex/test/moletest/Makefile: No such file or directory
make[2]: *** No rule to make target `/home/alex/test/moletest/Makefile'. Stop.
make[1]: *** [_mole_/home/alex/test/moletest] Error 2
make[1]: Leaving directory `/usr/src/kernels/2.6.18-53.el5-i686'
make: *** [moles] Error 2
[root@JiaKun moletest]# mv makefile Makefile
[root@JiaKun moletest]# make
make -C /usr/src/kernels/2.6.18-53.el5-i686 M=/home/alex/test/moletest moles
make[1]: Entering directory `/usr/src/kernels/2.6.18-53.el5-i686'
CC [M] /home/alex/test/moletest/hello.o
Building moles, stage 2.
MODPOST
CC /home/alex/test/moletest/hello.mod.o
LD [M] /home/alex/test/moletest/hello.ko
make[1]: Leaving directory `/usr/src/kernels/2.6.18-53.el5-i686'
[root@JiaKun moletest]# ls
hello.c hello.ko hello.mod.c hello.mod.o hello.o Makefile Mole.symvers
3.執行代碼,載入驅動模塊:
2.4內核載入模塊:
insmod ./hello.o
但是此時並沒有輸出printk列印的信息。但是可以在/var/log/messages 中看到列印的信息,這是由於KERN_ALERT優先順序不夠高。這里
需要修改為:KERN_EMERG。再次編譯,載入模塊即可以看到結果
2.6內核載入模塊:
[root@JiaKun moletest]# insmod hello.ko
[root@JiaKun moletest]#
Message from syslogd@ at Sat Jul 26 19:52:44 2008 ...
JiaKun kernel: Hello, world
有的朋友可能會出現insmod命令找不到的錯誤,這可能有下面幾個原因:
<1> 你的系統沒有安裝mole-init-tools工具,關於此問題,只需安裝即可,但是一般裝完系統是有這個命令的。
<2> 環境變數沒有添加導致不能使用該命令。使用echo $PATH即可查看PATH環境變數,發現沒有/sbin這個路徑,所以你當然不能使用insmod這個命令了。解決的方法很簡單,只需在命令行輸入:
PATH = "$PATH:/sbin"即可添加。(insmod在/sbin這個目錄下,你可以使用whereis insmod查看)。
<3> insmod這個命令需要在root許可權下才能使用。
載入完成後你可以輸入lsmod查看hello這個模塊哦。
4.卸載驅動模塊:rmmod hello.
載入模塊後就可在屏幕上看到如下信息:Hello world enter.
卸載時就可在屏幕上看到如下信息:hello world exit.
[root@JiaKun moletest]# rmmod hello.ko
[root@JiaKun moletest]#
Message from syslogd@ at Sat Jul 26 19:52:58 2008 ...
JiaKun kernel: Goodbye, cruel world
另外,如果有多個文件,則按下列方式編寫Makefile文件(file1.c、file2.c):
obj -m := molename.o
mole-objs := file1.o file2.o
轉載僅供參考,版權屬於原作者。祝你愉快,滿意請採納哦
D. 嵌入式linux下如何卸載內核已有的一些驅動
用lsmod確認要卸載的驅動,
然後用rmmod卸載就可以了。
E. ubuntu卸載已安裝的ko驅動
ubuntu是不需要卸載已安裝的陪租鎮ko驅動。ko文件是kernelobject文件(內核模塊),該文件的型桐意義就是把內核的一些功能移動到內核外邊,需要的蘆粗時候插入內核,不需要時卸載。
F. 用ctex編譯時怎麼終止前一個應用
一、手工載入測試 1、insmod 。/key_test。ko 載入驅動模塊到內核 2、cat /proc/moles |grep key_test 查看key_test模塊在內核中的地址,不加過濾器可以看到全部載入的模塊。 3、lsmod 顯示模塊,這時可以看到所有的模塊名字,後面跟的是主設備號和次設備號。
4、rmmod key_test 把模塊從內核里卸載。 二、動態載入 1、把key_test。c源代碼放到內核源代碼的/drives/char/下,因為這是屬字元型驅動,放在這編譯到zImage中。 2、這時我們make menuconfig 編譯內核是看不到key_test這個選項的。
我們把這個選項寫到菜單裡面才行。在內核源代碼的/drives/char/下有一個Kconfig文件,打開 (1) vi Kconfig 加幾行到裡面: config ConFig_key_test bool "key test" //前面那個bool換成tristate就是支持模塊化編譯 上面句是在make menuconfig時會出現key test這個選項在drive/char子菜單下,bool前面是TAB鍵 ------help---------- 這句是出現在菜單選項下面的 This key test help。
這句是你的驅動的說明會出現在help裡面 (2)在/drivers/char目錄下的Makefile文件里加上一句: obj-$(CONFIG_key_test) += key_test。o 上面這句是讓Make時把key_test編譯到內核中。
(3) make menuconfig 把key_test選項選取上 (4) make zImage 生成zImage文件,重啟動載入這個新編的內核。 3、lsmod就能看到key_test了,但是還不能用,沒有介面,也就是/dev下面沒有 4、mknod /dev/key_test c 121 0 這是創建設備到/dev下,使普通程序可以調用了,121是在源代碼里定義的它的主設備號,0是次設備號。
5、cat /dev/key_test 這是相當於open這個設備了,或者寫一個程序直接調用open、write等函數。
G. linux下怎麼編譯安裝驅動
linux 編譯安裝驅動有兩種,動態載入與靜態載入
動態載入
一,編譯,在指點內核樹下編譯,生成.o文件或.ko文件
二,將生成的.o或.ko文件拷到相應目錄,一般是/lib/mole/kernel下面
三,用insmod命令載入,用rmmod命令卸載
靜態載入
靜態載入主要就是編譯內核。就是將編寫好的驅動放進內核相應的目錄下面。然後編譯內核。然後運行編譯好的內核。
H. linux 驅動模塊編譯到內核後不改變內核 怎麼修改或更換驅動
insmod,rmmod
I. 編譯內核模塊常見有關問題怎麼解決
第一次把自己編譯的驅動模塊載入進開發板,就出現問題,還好沒花費多長時間,下面列舉出現的問題及解決方案
1:出現insmod: error inserting 'hello.ko': -1 Invalid mole format
法一(網上的):是因為內核模塊生成的環境與運行的環境不一致,用linux-2.6.27內核源代碼生成的模塊,可能就不能在linux-2.6.32.2內核的linux環境下載入,需要在linux-2.6.27內核的linux環境下載入。
a.執行判渣 uname -r //查看內核版本
b.一般出錯信息被記辯戚錄在文件/var/log/messages中,執行下面命令看錯誤信息
# cat /var/log/messages |tail
若出現類似下面:
Jun 4 22:07:54 localhost kernel:hello: version magic '2.6.35.6-45.fc14.i686.PAE
' should be '2.6.35.13-92.fc14.i686.PAE'
則把 Makefile里的KDIR :=/lib/moles/2.6.35.6-45.fc14.i686.PAE/build1 改為
KDIR :=/lib/moles/2.6.35.13-92.fc14.i686.PAE/build1 //改成自己內核源碼路徑
(這里的build1是一個文件鏈接,鏈接到/usr/src/kernels/2.6.35.6-45.fc14.i686.PAE和13-92的)
然並卵,我的fedora 14 /usr/src/kernels下並沒有2.6.35.13-92.fc14.i686.PAE,只有2.6.35.13-92.fc14.i686,雖然不知道兩者有什麼區別,但改成2.6.35.13-92.fc14.i686還是不行,照樣這個問題,還好後來在看教學視頻的到啟發
法二:改的還是那個位置
KDIR :=/opt/FriendlyARM/linux-2.6.32.2 //把這里改成你編譯生成kernel的那個路徑
all:
$ (MAKE) -C $ (KDIR) M = $ (PWD) moles ARCH=arm CROSS_COMPILE=arm-linux- //加這掘灶悄句
2. [70685.298483] hello: mole license 'unspecified' taints kernel.
[70685.298673] Disabling lock debugging e to kernel taint
方法:在模塊程序中加入: MODULE_LICENSE("GPL");
3. rmmod: chdir(2.6.32.2-FriendlyARM): No such file or directory 錯誤解決
方法:lsmod 可查看模塊信息
即無法刪除對應的模塊。
就是必須在/lib/moles下建立錯誤提示的對應的目錄((2.6.32.2)即可。
必須創建/lib/moles/2.6.32.2這樣一個空目錄,否則不能卸載ko模塊.
# rmmod nls_cp936
rmmod: chdir(/lib/moles): No such file or directory
但是這樣倒是可以卸載nls_cp936,不過會一直有這樣一個提示:
rmmod: mole 'nls_cp936' not found
初步發現,原來這是編譯kernel時使用make moles_install生成的一個目錄,
但是經測試得知,rmmod: mole 'nls_cp936' not found來自於busybox,並不是來自kernel
1).創建/lib/moles/2.6.32.2空目錄
2).使用如下源碼生成rmmod命令,就可以沒有任何提示的卸載ko模塊了[luther.gliethttp]
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
int main(int argc, char *argv[])
{
const char *modname = argv[1];
int ret = -1;
int maxtry = 10;
while (maxtry-- > 0) {
ret = delete_mole(modname, O_NONBLOCK | O_EXCL);//系統調用sys_delete_mole
if (ret < 0 && errno == EAGAIN)
usleep(500000);
else
break;
}
if (ret != 0)
printf("Unable to unload driver mole \"%s\": %s\n",
modname, strerror(errno));
}
3).把生成的命令復制到文件系統
# arm-linux-gcc -static -o rmmod rmmod.c
# arm-linux-strip -s rmmod
# cp rmmod /nfs/
cp /nfs/rmmod /sbin
代碼如下:
proc.c
[html] view plain
<span style="font-size:18px;">#include <linux/mole.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/proc_fs.h> /* Necessary because we use the proc fs */
#define procfs_name "proctest"
MODULE_LICENSE("GPL");
struct proc_dir_entry *Our_Proc_File;
int procfile_read(char *buffer,char **buffer_location,off_t offset, int buffer_length, int *eof, void *data)
{ int ret;
ret = sprintf(buffer, "HelloWorld!\n");
return ret;
}
int proc_init()
{ Our_Proc_File = create_proc_entry(procfs_name, 0644, NULL);
if (Our_Proc_File == NULL) {
remove_proc_entry(procfs_name, NULL);
printk(KERN_ALERT "Error: Could not initialize /proc/%s\n",procfs_name);
return -ENOMEM; }
Our_Proc_File->read_proc = procfile_read;//
// Our_Proc_File->owner = THIS_MODULE;
Our_Proc_File->mode = S_IFREG | S_IRUGO;
Our_Proc_File->uid = 0;
Our_Proc_File->gid = 0;
Our_Proc_File->size = 37;
printk("/proc/%s created\n", procfs_name);
return 0;
}
void proc_exit()
{ remove_proc_entry(procfs_name, NULL);
printk(KERN_INFO "/proc/%s removed\n", procfs_name);
}
mole_init(proc_init);
mole_exit(proc_exit);</span></span></span></span></span>
[html] view plain
<span style="font-size:18px;">
ifneq ($(KERNELRELEASE),)
obj-m :=proc.o
else
KDIR :=/opt/FriendlyARM/linux-2.6.32.2
#KDIR :=/lib/moles/2.6.35.13-92.fc14.i686.PAE/build1
PWD :=$(shell pwd)
all:
$(MAKE) -C $(KDIR) M=$(PWD) moles ARCH=arm CROSS_COMPILE=arm-linux-
clean:
rm -f *.ko *.o *.mod.o *.mod.c *.symvers
endif</span></span></span></span></span>
make後生成proc.ko,再在開發板上insmod proc.ko即可
執行 dmesg 就可以看到 產生的內核信息啦
J. Linux無法卸載驅動模塊
在網上查了查,原來是現在的內核模塊在插入卸載時都會要轉到喊簡/lib/畝裂moles/內核版本號/ 這個目錄里。鄭耐褲
所以只要建立這個目錄並且把要使用的模塊.ko文件復制到這個目錄就行了。
最近在使用busybox 1.13.1時發現要卸載必須要完全匹配模塊名才行,原來在老標本的使用模塊文件名就能卸載,現在發現不行了。