# Linux DKMS动态模块支持 # DKMS简介 > DKMS全称是Dynamic Kernel Module Support > Linux模块和内核是有依赖关系的,如果遇到因为发行版更新造成的内核版本的变动,之前编译的模块是无法继续使用的,我们只能手动再编译一遍。 > DKMS可以帮我们维护内核外的这些驱动程序,在内核版本变动之后可以自动重新生成新的模块。 *安装dkms* `sudo apt-get install dkms` dkms的使用流程如下: ``` uninstall --------------------------- - - V - --------------- --------------- --------------- ------------------- - - add - - build - - install - - - Not in tree - ------> - Added State - ------> - Built State - ------> - Installed State - - - - - - - - - --------------- --------------- --------------- ------------------- ^ - - - - - - - -------------------------------------------------------------------------- remove ``` # DKMS主要的命令 add、build、install、uninstall和remove 例如: ``` $ dkms status virtualbox, 4.3.10, 3.13.0-49-generic, x86_64: installed ``` dkms要求代码的目录必须以 - 的格式命名, 例如: hello-0.1 # 实例: 使用DKMS管理 rtlwifi驱动 ## 准备rtlwifi源文件 rtlwifi代码的github地址为: `http://github.com/lwfinger/rtlwifi_new.git` 将下载的代码拷贝到/usr/src 例如: ``` $ ls /usr/src/ linux-headers-3.13.0-49 rtlwifi-1.0 linux-headers-3.13.0-49-generic virtualbox-4.3.10 ``` 还需要在rtlwifi-1.0中新建一个文件dkms.conf ``` PACKAGE_NAME="rtlwifi" PACKAGE_VERSION="1.0" CLEAN="make clean" BUILT_MODULE_NAME[0]="rtlwifi" BUILT_MODULE_LOCATION[0]="." DEST_MODULE_LOCATION[0]="kernel/drivers/net/wireless/rtlwifi" BUILT_MODULE_NAME[1]="rtl_pci" BUILT_MODULE_LOCATION[1]="." DEST_MODULE_LOCATION[1]="kernel/drivers/net/wireless/rtlwifi" BUILT_MODULE_NAME[2]="rtl_usb" BUILT_MODULE_LOCATION[2]="." DEST_MODULE_LOCATION[2]="kernel/drivers/net/wireless/rtlwifi" BUILT_MODULE_NAME[3]="btcoexist" BUILT_MODULE_LOCATION[3]="btcoexist" DEST_MODULE_LOCATION[3]="kernel/drivers/net/wireless/rtlwifi/btcoexist" BUILT_MODULE_NAME[4]="rtl8723be" BUILT_MODULE_LOCATION[4]="rtl8723be" DEST_MODULE_LOCATION[4]="kernel/drivers/net/wireless/rtlwifi/rtl8723be" AUTOINSTALL="yes" ``` > PACKAGE_NAME和PACKAGE_VERSION和文件夹的命名是一致的。 DEST_MODULE_LOCATION[0]用来设定模块安装的目的地址,本例是"/lib/module/$(KVERSION)/updates" DEST_MODULE_LOCATION[#] 在下面这些发行版中被忽略: *Fedora Core 6 and higher, Red Hat Enterprise Linux 5 and higher* *Novell SuSE Linux Enterprise Server 10 and higher, Novell SuSE Linux10.0 and higher, and Ubuntu* Instead , the proper distribution-specific directory is used. AUTOINSTALL="yes"表示在Linux引导之后DKMS会自动对这个模块执行Build和Install的动作,当然如果模块已经处于该状态的话,相应的动作是不用再执行的。 > ## 将rtlwifi添加到dkms `sudo dkms add -m rtlwifi -v 1.0` 其中-m指定module的名字, -v 指定module version 所以rtlwifi的源文件路径为: /usr/src/rtlwifi-1.0 ``` $ sudo dkms add -m rtlwifi -v 1.0 Creating symlink /var/lib/dkms/rtlwifi/1.0/source -> /usr/src/rtlwifi-1.0 DKMS: add completed. ``` 查看dkms的状态: ``` $ dkms status rtlwifi, 1.0: added virtualbox, 4.3.10, 3.13.0-49-generic, x86_64: installed ``` 可以看到rtlwifi的状态为added ## 使用dkms build rtlwifi ``` sudo dkms build -m rtlwifi -v 1.0 Kernel preparation unnecessary for this kernel. Skipping... Building module: cleaning build area.... make KERNELRELEASE=3.13.0-49-generic -C /lib/modules/3.13.0-49-generic/build M=/var/lib/dkms/rtlwifi/1.0/build.................... cleaning build area.... DKMS: build completed. ``` 查看dkms状态: ``` $ dkms status rtlwifi, 1.0, 3.13.0-49-generic, x86_64: built virtualbox, 4.3.10, 3.13.0-49-generic, x86_64: installed ``` rtlwifi的状态为built 生成的ko模块位于: /var/lib/dkms/rtlwifi/1.0/3.13.0-49-generic/x86_64/module ## 使用dkms 安装rtlwifi ``` $ sudo dkms install --force -m rtlwifi -v 1.0 rtlwifi: Running module version sanity check. - Original module - Installation - Installing to /lib/modules/3.13.0-49-generic/updates/dkms/ rtl_pci.ko: Running module version sanity check. - Original module - Installation - Installing to /lib/modules/3.13.0-49-generic/updates/dkms/ rtl_usb.ko: Running module version sanity check. - Original module - Installation - Installing to /lib/modules/3.13.0-49-generic/updates/dkms/ btcoexist.ko: Running module version sanity check. - Original module - Installation - Installing to /lib/modules/3.13.0-49-generic/updates/dkms/ btcoexist.ko: Running module version sanity check. - Original module - Multiple same named modules! - 2 named btcoexist.ko in /lib/modules/3.13.0-49-generic/ - Installation - Installing to /lib/modules/3.13.0-49-generic/updates/dkms/ rtl8723be.ko: Running module version sanity check. - Original module - Installation - Installing to /lib/modules/3.13.0-49-generic/updates/dkms/ depmod.... DKMS: install completed. ``` 查看dkms 状态: ``` $ dkms status rtlwifi, 1.0, 3.13.0-49-generic, x86_64: installed virtualbox, 4.3.10, 3.13.0-49-generic, x86_64: installed ``` ## 从dkms系统移出rtlwifi 从/lib/modules下移除: sudo dkms uninstall -m rtlwifi -v 1.0 从/var/lib/dkms下彻底删除 sudo dkms remove -m rtlwifi -v 1.0 --all