openwrt中自带一整套包管理机制,正确使用openwrt的包管理机制可有效提高开发效率
本文介绍如何在openwrt中增加一个自定义的内核模块包
1、在package/kernel下创建一个应用程序目录mydrv,并在mydrv下再创建一个src用于存放源码文件
$ cd package/kernel
$ mkdir mydrv
$ cd mydrv
$ mkdir src
2、在mydrv目录下创建Makefile,内容如下
$ cd mydrv/
$ vim Makefile
include $(TOPDIR)/rules.mk
include $(INCLUDE_DIR)/kernel.mk
PKG_NAME:=mydrv
PKG_RELEASE:=1
include $(INCLUDE_DIR)/package.mk
define KernelPackage/$(PKG_NAME)
SUBMENU:=Other modules
TITLE:=This is a $(PKG_NAME) driver
FILES:=$(PKG_BUILD_DIR)/$(PKG_NAME).ko
AUTOLOAD:=$(call AutoLoad,30,,1)
KCONFIG:=
endef
define KernelPackage/$(PKG_NAME)/description
This is a $(PKG_NAME) device.
endef
EXTRA_KCONFIG:= \
CONFIG_MYDRV=m
EXTRA_CFLAGS:= \
$(patsubst CONFIG_%, -DCONFIG_%=1, $(patsubst %=m,%,$(filter %=m,$(EXTRA_KCONFIG)))) \
$(patsubst CONFIG_%, -DCONFIG_%=1, $(patsubst %=y,%,$(filter %=y,$(EXTRA_KCONFIG)))) \
MAKE_OPTS:= \
ARCH="$(LINUX_KARCH)" \
CROSS_COMPILE="$(TARGET_CROSS)" \
SUBDIRS="$(PKG_BUILD_DIR)/" \
EXTRA_CFLAGS="$(EXTRA_CFLAGS)" \
$(EXTRA_KCONFIG)
define Build/Prepare
mkdir -p $(PKG_BUILD_DIR)
$(CP) ./src/* $(PKG_BUILD_DIR)/
endef
define Build/Compile
$(MAKE) -C "$(LINUX_DIR)" \
$(MAKE_OPTS) \
modules
endef
$(eval $(call KernelPackage,$(PKG_NAME)))
包名称、版本:
PKG_NAME:=mydrv
PKG_RELEASE:=1
描述:
define KernelPackage/$(PKG_NAME)
SUBMENU:=Other modules
TITLE:=This is a $(PKG_NAME) driver
FILES:=$(PKG_BUILD_DIR)/$(PKG_NAME).ko
AUTOLOAD:=$(call AutoLoad,30,,1)
KCONFIG:=
endef
define KernelPackage/$(PKG_NAME)/description
This is a $(PKG_NAME) device.
endef
准备操作:
define Build/Prepare
mkdir -p $(PKG_BUILD_DIR)
$(CP) ./src/* $(PKG_BUILD_DIR)/
endef
编译操作:
define Build/Compile
$(MAKE) -C "$(LINUX_DIR)" \
$(MAKE_OPTS) \
modules
endef
3、进入src/目录,创建Makefile、Kconfig、mydrv.c
$ cd src/
$ vim Makefile
obj-${CONFIG_MYDRV}+= mydrv.o
$ vim Kconfig
config MYDRV
tristate "test mydrv"
help
This is a example, for debugging kernel model.
If unsure, say N.
$ vim mydrv.c
#include
#include
static __init int mydrv_init(void)
{
printk(KERN_ALERT "helloworld!\n");
return 0;
}
static __exit void mydrv_exit(void)
{
printk(KERN_ALERT "helloworld exit!\n");
}
module_init(mydrv_init);
module_exit(mydrv_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Stone");
4、回到根目录,使用make menuconfig配置mydrv模块
$ cd openwrt/
$ make menuconfig
kernel modules --->
Other modules --->
5、在根目录编译模块
$ cd openwrt/
$ make package/mydrv/compile V=s
6、变成成功后可在bin/targets/ramips/mt76x8/packages下看到生成的ipk包
7、导入ipk包到设备,使用opkg安装此包
$ opkg install kmod-mydrv_4.14.101-1_mipsel_24kc.ipk
成功安装后即可加载mydrv.ko