Openwrt自带的图形化配置工具非常方便,但如果自己编译的时候经常要改设置,需要选中或者取消的package散落在图形配置的各个角落,那无疑是非常痛苦的一件事,下面用bash脚本来优化这一流程。
下面以生成x86固件为例
首先备份你已有的.config
配置文件,然后make menuconfig
,在架构中选中x86->x86_64->generic x86/64>,根据你的需求配置好架构,然后保存为template.config
模板文件。
打开这个模板文件,现在里面的是你所选架构(板子)的所有默认配置了,后面需要什么包就从里面搜索就行。文件开头是架构信息,复制出来
#
# Automatically generated file; DO NOT EDIT.
# OpenWrt Configuration
#
CONFIG_MODULES=y
CONFIG_HAVE_DOT_CONFIG=y
CONFIG_HOST_OS_LINUX=y
# CONFIG_HOST_OS_MACOS is not set
# CONFIG_TARGET_airoha is not set
# CONFIG_TARGET_sunxi is not set
# CONFIG_TARGET_meson is not set
# CONFIG_TARGET_silicon is not set
# CONFIG_TARGET_apm821xx is not set
# CONFIG_TARGET_ath25 is not set
# CONFIG_TARGET_ath79 is not set
# CONFIG_TARGET_bcm27xx is not set
# CONFIG_TARGET_bcm53xx is not set
# CONFIG_TARGET_bcm47xx is not set
# CONFIG_TARGET_bcm4908 is not set
# CONFIG_TARGET_bcm63xx is not set
# CONFIG_TARGET_bmips is not set
# CONFIG_TARGET_octeon is not set
# CONFIG_TARGET_gemini is not set
# CONFIG_TARGET_mpc85xx is not set
# CONFIG_TARGET_mxs is not set
# CONFIG_TARGET_lantiq is not set
# CONFIG_TARGET_malta is not set
# CONFIG_TARGET_pistachio is not set
# CONFIG_TARGET_mvebu is not set
# CONFIG_TARGET_kirkwood is not set
# CONFIG_TARGET_mediatek is not set
# CONFIG_TARGET_ramips is not set
# CONFIG_TARGET_at91 is not set
# CONFIG_TARGET_tegra is not set
# CONFIG_TARGET_layerscape is not set
# CONFIG_TARGET_imx is not set
# CONFIG_TARGET_octeontx is not set
# CONFIG_TARGET_oxnas is not set
# CONFIG_TARGET_armvirt is not set
# CONFIG_TARGET_ipq40xx is not set
# CONFIG_TARGET_ipq60xx is not set
# CONFIG_TARGET_ipq806x is not set
# CONFIG_TARGET_ipq807x is not set
# CONFIG_TARGET_realtek is not set
# CONFIG_TARGET_rockchip is not set
# CONFIG_TARGET_sifiveu is not set
# CONFIG_TARGET_arc770 is not set
# CONFIG_TARGET_archs38 is not set
# CONFIG_TARGET_omap is not set
# CONFIG_TARGET_uml is not set
# CONFIG_TARGET_zynq is not set
CONFIG_TARGET_x86=y
CONFIG_TARGET_x86_64=y
# CONFIG_TARGET_x86_generic is not set
# CONFIG_TARGET_x86_legacy is not set
# CONFIG_TARGET_x86_geode is not set
# CONFIG_TARGET_MULTI_PROFILE is not set
CONFIG_TARGET_x86_64_DEVICE_generic=y
CONFIG_HAS_SUBTARGETS=y
CONFIG_HAS_DEVICES=y
CONFIG_TARGET_BOARD="x86"
CONFIG_TARGET_SUBTARGET="64"
CONFIG_TARGET_PROFILE="DEVICE_generic"
CONFIG_TARGET_ARCH_PACKAGES="x86_64"
CONFIG_DEFAULT_TARGET_OPTIMIZATION="-Os -pipe"
其中的注释可以删除掉,然后新建一个bash脚本gen-config-x86.sh
#!/bin/bash
[[ -f .config ]] && mv .config .config.bak
touch .config
# 刚才的架构信息
echo '
CONFIG_TARGET_x86=y
CONFIG_TARGET_x86_64=y
CONFIG_TARGET_x86_64_DEVICE_generic=y
CONFIG_HAS_SUBTARGETS=y
CONFIG_HAS_DEVICES=y
CONFIG_TARGET_BOARD="x86"
CONFIG_TARGET_SUBTARGET="64"
CONFIG_TARGET_PROFILE="DEVICE_generic"
CONFIG_TARGET_ARCH_PACKAGES="x86_64"
' >>.config
# 选中的软件包
enable_packages=(
luci-app-diskman # luci-app-应用
luci-app-diskman_INCLUDE_btrfs_progs # package的二级菜单选项
kmod-usb3 # 内核模块等
minicom # 命令行工具
)
for ((i = 0; i < ${#enable_packages[@]}; i++)); do
echo "CONFIG_PACKAGE_${enable_packages[i]}=y" | tee -a .config
done
# 主要用于禁用默认配置里选中的luci-app-,其他地方的默认配置不建议去动
disable_packages=(
luci-app-turboacc
)
for ((i = 0; i < ${#disable_packages[@]}; i++)); do
echo "CONFIG_PACKAGE_${disable_packages[i]}=n" | tee -a .config
done
module_packages=(
luci-app-mwan3
)
for ((i = 0; i < ${#module_packages[@]}; i++)); do
echo "CONFIG_PACKAGE_${module_packages[i]}=m" | tee -a .config
done
make defconfig
这个脚本里方便修改需要的软件包和需要禁用的包,集中管理,具体有那些可以设置,可以在刚才的template.config
模板文件里查找,包括软件包的二级菜单选项,都可以这样设置,很方便。
在make defconfig
之前,.config
文件里是这样的
CONFIG_TARGET_x86=y
CONFIG_TARGET_x86_64=y
CONFIG_TARGET_x86_64_DEVICE_generic=y
CONFIG_HAS_SUBTARGETS=y
CONFIG_HAS_DEVICES=y
CONFIG_TARGET_BOARD="x86"
CONFIG_TARGET_SUBTARGET="64"
CONFIG_TARGET_PROFILE="DEVICE_generic"
CONFIG_TARGET_ARCH_PACKAGES="x86_64"
CONFIG_PACKAGE_luci-app-diskman=y
CONFIG_PACKAGE_luci-app-diskman_INCLUDE_btrfs_progs=y
CONFIG_PACKAGE_kmod-usb3=y
CONFIG_PACKAGE_minicom=y
CONFIG_PACKAGE_luci-app-turboacc=n
CONFIG_PACKAGE_luci-app-mwan3=m
执行make defconfig
之后,对于y
和m
的选项,会自动补齐相关的依赖,n
的模块会取消选中,取消依赖。
通过这种方法添加的软件包主要是luci-app-
服务,kmod
内核模块,以及一些命令行工具,太底层的lib不要去动,defconfig
会帮我们补齐的。
如果有多个不同架构的设备,可以创建对应的gen-config-{arch}.sh
脚本,然后根据需求在里面添加所需的应用的驱动包。