OpenWrt编译环境搭建

OpenWrt编译环境搭建

OpenWrt是一个基于Linux的嵌入式操作系统,专门设计用于智能路由器和网络设备。它采用了模块化的设计理念,可以轻松地定制和扩展路由器的功能。OpenWrt的核心是一个裁剪后的Linux内核,使用buildroot构建文件系统,通过添加软件包和插件来提供丰富的网络功能和应用。

OpenWrt的特点和优势包括:

  1. 开源和自由:OpenWrt是开源项目,源代码可以免费获取和修改。这意味着开发者可以根据自己的需求进行定制化开发,添加新功能或优化性能。
  2. 广泛的硬件支持:OpenWrt支持多种硬件平台和芯片组,包括常见的路由器、交换机和嵌入式设备。这使得开发者可以选择适合自己需求的硬件平台,实现更广泛的应用。
  3. 强大的软件生态系统:OpenWrt拥有丰富的软件包管理系统和活跃的开发者社区,提供了大量的软件包和插件,覆盖了各种网络功能和应用。开发者可以利用这些软件包来构建自己的应用程序,实现路由器的定制化和扩展功能。
  4. 灵活的配置和管理:OpenWrt采用了UCI(Unified Configuration Interface)作为统一的配置接口,通过简洁的配置文件来管理路由器的各种设置。这使得配置和管理变得简单而灵活,同时也提供了远程管理和监控的能力。
  5. 定制化和性能优化:开发者可以根据具体需求进行定制化开发,只选择所需的功能和组件,避免不必要的资源浪费。此外,OpenWrt还支持运行在低功耗硬件上,提供高性能和低能耗的解决方案。

总之,OpenWrt作为智能路由器开发的强大工具,为开发者提供了广泛的硬件支持、丰富的软件生态系统、灵活的配置和定制化选项。它不仅可以满足智能路由器的基本需求,还提供了更多的扩展和创新可能性。通过利用OpenWrt,开发者可以构建出功能强大、安全可靠的智能路由器,满足用户对于高性能、智能化网络连接的需求。
下面介绍如何搭建OpenWrt编译环境,自己编译OpenWrt的固件。

  1. 基于Ubuntu 22.04,64bit的操作系统,请预留至少10G的磁盘空间。如果后续有定制需求,或者多款型号的固件,建议预留更多。笔者这里是1TB的磁盘。

  2. 先clone仓库:

    git clone https://git.openwrt.org/openwrt/openwrt.git
    

    可能会比较久,取决于网络状况。笔者是23年Q4写的,此时openwrt已经为v23了。

    clone成功后,会在clone的路径下有openwrt的文件夹。

  3. 进入文件夹:

    cd openwrt
    

    更新软件包:

    ./scripts/feed update -a
    ./scripts/feed install -a
    

    后续有需要,也可以手动再更新。

  4. 生成配置文件:

    make defconfig
    

    这一步和电脑安装软件的情况有关。可能会有报错,此时需根据报错日志来判断安装软件,如:

    sudo apt-get install build-essential gcc g++ vim make cmake unzip python3-dev vim git perl wget patchelf gawk libncurses5-dev subversion zlib1g-dev m4 -y
    

    最后的-y表示yes,即同意安装;不用每次都重新输出Y来确认。
    建议有报错后再安装即可,没必要一次性安装推荐的所有软件,减少对电脑磁盘的占用。

  5. 进入定制页面:

    make menuconfig
    

    通过键盘的上下方向键选择机型,包括CPU等,通过空格键选中,保存并退出。

    如果有需要定制的package,也可以在这一步操作,如可以参考笔者的:

    OpenWrt源码编译新增项 - 付时凡 - 博客园 (cnblogs.com)

  6. 编译:

    make -j4 V=sc
    
    • -j4表示使用4核编译,如果电脑的CPU比较强,则推荐,如果有更多的核,可以-j99,编译时会自动调整核心,不过单核的电脑,这个参数就没有意义了。当然,第一次编译,建议先不用该参数,先单核编译,出错率会少一些
    • V=sc表示输出更多的报错日志,方便debug,例如是否是download某些源文件失败等,如果是download失败,可能是国内网络环境受限,可通过报错日志中的URL等信息,手动下载对应的压缩包文件,放在dl目录下,然后重新编译即可
  7. 附完整编译日志,笔者这里是编译时不带V=sc参数,芯片方案为高通的ipq40xx

    make[2]: Entering directory '/home/ubuntu/openwrt/scripts/config'
    make[2]: 'conf' is up to date.
    make[2]: Leaving directory '/home/ubuntu/openwrt/scripts/config'
     make[1] world
     make[2] package/cleanup
     make[2] target/compile
     make[3] -C target/linux compile
     make[2] buildinfo
     make[2] package/compile
     make[3] diffconfig buildversion feedsversion
     make[3] -C package/libs/toolchain compile
     make[3] -C package/libs/libjson-c host-compile
     make[3] -C package/kernel/gpio-button-hotplug compile
     make[3] -C package/libs/ncurses host-compile
     make[3] -C package/system/fwtool host-compile
     make[3] -C package/system/usign host-compile
     make[3] -C package/system/openwrt-keyring compile
     make[3] -C feeds/luci/modules/luci-base host-compile
     make[3] -C package/libs/mbedtls compile
     make[3] -C package/utils/lua host-compile
     make[4] scripts/config/conf
     make[3] -C feeds/luci/contrib/package/csstidy host-compile
     make[3] -C package/libs/jansson compile
     make[3] -C package/libs/libmnl compile
     make[3] -C package/system/ca-certificates compile
     make[3] -C feeds/packages/libs/nghttp2 compile
     make[3] -C package/firmware/ath11k-firmware compile
     make[3] -C package/firmware/ipq-wifi compile
     make[3] -C package/firmware/wireless-regdb compile
     make[3] -C package/kernel/qca-ssdk compile
     make[3] -C package/network/services/dropbear compile
     make[3] -C package/libs/libpcap compile
     make[3] -C package/network/utils/linux-atm compile
     make[3] -C package/network/utils/resolveip compile
     make[3] -C package/libs/libsepol compile
     make[3] -C package/libs/musl-fts compile
     make[3] -C package/utils/e2fsprogs host-compile
     make[3] -C package/libs/libubox host-compile
     make[3] -C package/libs/libnl-tiny compile
     make[3] -C package/libs/libjson-c compile
     make[3] -C package/utils/lua compile
     make[3] -C package/firmware/linux-firmware compile
     make[3] -C package/libs/zlib compile
     make[3] -C package/system/fwtool compile
     make[3] -C package/system/ucert host-compile
     make[3] -C package/libs/libnftnl compile
     make[3] -C feeds/luci/protocols/luci-proto-ppp compile
     make[3] -C feeds/luci/themes/luci-theme-bootstrap compile
     make[3] -C feeds/luci/protocols/luci-proto-ipv6 compile
     make[3] -C feeds/luci/libs/luci-lib-ip compile
     make[3] -C feeds/luci/libs/luci-lib-jsonc compile
     make[3] -C feeds/luci/libs/luci-lib-nixio compile
     make[3] -C feeds/luci/themes/luci-theme-material compile
     make[3] -C feeds/packages/net/curl compile
     make[3] -C package/network/utils/iw compile
     make[3] -C package/libs/pcre2 compile
     make[3] -C package/system/opkg host-compile
     make[3] -C package/libs/libubox compile
     make[3] -C package/kernel/linux compile
     make[3] -C package/libs/ncurses compile
     make[3] -C package/utils/jsonfilter compile
     make[3] -C package/system/usign compile
     make[3] -C package/libs/ustream-ssl compile
     make[3] -C feeds/packages/admin/htop compile
     make[3] -C package/network/ipv6/odhcp6c compile
     make[3] -C package/system/mtd compile
     make[3] -C package/libs/libselinux compile
     make[3] -C package/system/urngd compile
     make[3] -C package/utils/busybox compile
     make[3] -C package/system/ubus compile
     make[3] -C package/system/uci compile
     make[3] -C package/utils/util-linux compile
     make[3] -C package/libs/uclient compile
     make[3] -C package/utils/ucode compile
     make[3] -C package/network/utils/iwinfo compile
     make[3] -C feeds/luci/contrib/package/ucode-mod-html compile
     make[3] -C feeds/packages/net/cgi-io compile
     make[3] -C package/network/services/uhttpd compile
     make[3] -C feeds/luci/contrib/package/ucode-mod-lua compile
     make[3] -C package/network/services/hostapd compile
     make[3] -C package/network/services/dnsmasq compile
     make[3] -C package/network/services/odhcpd compile
     make[3] -C package/utils/e2fsprogs compile
     make[3] -C package/network/config/netifd compile
     make[3] -C package/system/ubox compile
     make[3] -C package/utils/mtd-utils compile
     make[3] -C package/system/opkg compile
     make[3] -C package/network/utils/nftables compile
     make[3] -C feeds/luci/contrib/package/lucihttp compile
     make[3] -C package/system/rpcd compile
     make[3] -C feeds/luci/libs/luci-lib-base compile
     make[3] -C package/kernel/mac80211 compile
     make[3] -C package/system/urandom-seed compile
     make[3] -C package/system/fstools compile
     make[3] -C feeds/luci/applications/luci-app-opkg compile
     make[3] -C package/network/config/firewall4 compile
     make[3] -C feeds/luci/libs/rpcd-mod-luci compile
     make[3] -C feeds/luci/libs/rpcd-mod-rrdns compile
     make[3] -C package/system/procd compile
     make[3] -C package/base-files compile
     make[3] -C package/boot/uboot-envtools compile
     make[3] -C feeds/luci/applications/luci-app-firewall compile
     make[3] -C feeds/luci/modules/luci-base compile
     make[3] -C package/network/services/ppp compile
     make[3] -C feeds/luci/modules/luci-mod-network compile
     make[3] -C feeds/luci/modules/luci-mod-status compile
     make[3] -C feeds/luci/modules/luci-mod-system compile
     make[3] -C feeds/luci/modules/luci-lua-runtime compile
     make[3] -C feeds/luci/modules/luci-mod-admin-full compile
     make[3] -C feeds/luci/modules/luci-compat compile
     make[3] -C feeds/luci/libs/luci-lib-ipkg compile
     make[3] -C package/luci-theme-argon compile
     make[3] -C feeds/luci/collections/luci-light compile
     make[3] -C package/luci-app-argon-config compile
     make[3] -C feeds/luci/collections/luci compile
     make[3] -C package/kernel/qca-nss-dp compile
     make[2] package/install
     make[2] target/install
     make[3] -C target/linux install
     make[2] package/index
     make[2] json_overview_image_info
     make[2] checksum
    

    后续这些便是可选的编译目标,例如需要单独编译procd,则:

    make package/system/procd compile
    
  8. 编译后的镜像文件在bin目录下,如笔者的为:

    bin/targets/ipq40xx/generic/
    

    需要说明,如果target不同,目录自然不同,如上面的ipq40xx,应该是自己选择的target board。

  9. 说明下几个镜像:

    镜像 说明 备注
    initramfs-recovery.itb itb固件,并且带initramfs,即可以进入uboot,直接通过加载该镜像启动,如:bootm xxx.initramfs-recovery.itb 通常可用于测试固件是否正常,此时uboot启动,并不会破坏原有的FW分区等数据
    squashfs-factory.ubi 带factory字样,一般会覆盖旧有的配置,如第一次刷机 nand flash机型才会有,通常将kernelrootfs等封装为一个UBI
    squashfs-sysupgrade.bin 即经过刷机后的可通过openwrt页面直接升级的FW 在刷机稳定后,通过此固件升级;如果已经是openwrt固件,即可直接通过web升级该固件
  10. 一种手动升级的方式,将上述步骤中的sysupgrade.bin,通过tftp/wget等命令传输到样机的/tmp/目录下,然后调用以下指令,也可以升级:

    sysupgrade xxx-squashfs-sysupgrade.bin
    
posted @   付时凡  阅读(482)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示