OpenWrt
一、什么是 OpenWrt
- 面向网络设备(路由器)的嵌入式 Linux
- 包管理(opkg)
- Web 界面(LuCI)
- 灵活、可定制
二、适用场景
- 路由器、网关、AP
- 软路由(iKuaiOS、ROS)
- 物联网网关
三、与 Buildroot/Yocto 区别
- OpenWrt:网络设备专属,包管理一流
- Buildroot:通用,但包管理弱
- Yocto:通用,工业级,但复杂
四、获取与编译
git clone https://github.com/openwrt/openwrt.git
cd openwrt
./scripts/feeds update -a
./scripts/feeds install -a
make menuconfig
make -j$(nproc)
# 输出
ls bin/targets/<arch>/<subtarget>/
├── openwrt-<arch>-<subtarget>-squashfs-sysupgrade.itb
├── openwrt-*-kernel.bin
└── ...
五、配置
Target System (MediaTek Ralink ARM)
Subtarget (MT7621 based boards)
Target Profile (My Device)
包选择:
- Base system → busybox、opkg
- Network → iptables、dnsmasq、samba、curl
- LuCI → web 界面
六、目录结构
openwrt/
├── feeds/ 第三方包
├── package/ 系统包
│ ├── base/
│ ├── kernel/
│ └── network/
├── target/ 各架构 BSP
│ ├── linux/
│ ├── ramips/
│ ├── ipq40xx/
│ └── ...
├── toolchain/
├── scripts/
├── tools/
├── config/
├── include/
├── rules.mk
└── Makefile
七、自定义包
# package/myapp/Makefile
include $(TOPDIR)/rules.mk
PKG_NAME:=myapp
PKG_VERSION:=1.0
PKG_RELEASE:=1
PKG_SOURCE_URL:=https://github.com/myvendor/myapp/archive/v$(PKG_VERSION).tar.gz
PKG_HASH:=...
include $(INCLUDE_DIR)/package.mk
define Package/myapp
SECTION:=net
CATEGORY:=Network
TITLE:=My Application
URL:=https://example.com
DEPENDS:=+libuv
endef
define Build/Compile
$(MAKE) $(TARGET_CONFIGURE_OPTS) -C $(PKG_BUILD_DIR)
endef
define Package/myapp/install
$(INSTALL_DIR) $(1)/usr/bin
$(INSTALL_BIN) $(PKG_BUILD_DIR)/myapp $(1)/usr/bin/
endef
$(eval $(call BuildPackage,myapp))
八、SDK
为二次开发提供:
make menuconfig
# Tools → SDK
make -j$(nproc)
# bin/sdk/
九、刷机
# 命令行
sysupgrade -n openwrt-...-sysupgrade.itb
# 浏览器
访问 192.168.1.1 上传升级
十、LuCI
make menuconfig
# LuCI → Modules → LuCI → 启用需要的模块
十一、安全与 OpenWrt
- CVE 修复及时
- 固件签名校验(新版支持)
- 适合安全敏感场景