OpenWrt 编译步骤与命令详解

您所在的位置:网站首页 openwrt怎么换主题 OpenWrt 编译步骤与命令详解

OpenWrt 编译步骤与命令详解

2023-04-30 08:57| 来源: 网络整理| 查看: 265

前言

编译 Open­Wrt 的过程就像是复读机,除了选择系统组件外,几乎每次编译都是复制粘贴相同的命令。而理解每一条命令的作用、什么时候该去执行,这样才能更好的去解决编译中遇到的问题,更顺利的编译出固件。

下载资源站点 http://182.140.223.146/dl/ 

golang module 代理站点 https://goproxy.io/

首次编译

克隆 Open­Wrt 源码

git clone https://github.com/coolsnowwolf/lede openwrt 这里以 Lean 大佬的源码仓库为例子,毕竟很多人都在用它。命令末尾加了openwrt是指克隆代码到openwrt目录,目的是为了规范化,因为有时并不是编译这个的源码。

进入源码目录

cd openwrt

下载 feeds 源中的软件包源码

./scripts/feeds update -a feeds 是扩展的软件包,独立于 Open­Wrt 源码之外,所以需要单独进行拉取和更新。

安装 feeds 中的软件包

./scripts/feeds install -a

调整 Open­Wrt 系统组件

make menuconfig 首次编译建议只选择架构,其它都不要动,这样编译成功率会更高。如果不打算调整组件则输入make defconfig,它会检测编译环境并生成默认的编译配置文件。

预下载编译所需的软件包

make download -j8 V=s -j8是指使用8个线程下载,理论上是数字越大下载越快,但似乎有个上限,实测5线程以上其实速度相差不了多少,在网络好的情况下,基本在5分钟以内能下载完。

检查文件完整性

find dl -size -1024c -exec ls -l {} \; 此命令可以列出下载不完整的文件(根据我多次编译的经验得出小于1k的文件属于下载不完整),如果存在这样的文件可以使用find dl -size -1024c -exec rm -f {} \;命令将它们删除,然后重新执行make download下载并反复检查,确认所有文件完整可大大提高编译成功率,避免浪费时间。

开始编译

make -j1 V=s -j1:使用单线程编译。新手推荐单线程编译,一是因为玄学问题可能成功率高,二是方便查看错误日志。

 

V=s:输出详细日志,用于编译失败时找出错误。而且满屏代码在跑能装逼,一跑就是几个小时,装逼更持久。

再次编译

进入源码目录(如果不在此目录)

cd openwrt 更新 TIPS: 短期内再次编译可忽略更新这个步骤。

更新系统软件包

sudo sh -c "apt update && apt upgrade -y" 主要作用是更新在编译环境搭建时所安装的编译组件

拉取 Open­Wrt 源码更新

git pull

更新 feeds 源中的软件包源码

./scripts/feeds update -a

安装 feeds 中的软件包

./scripts/feeds install -a 文件清理

清除旧的编译产物(可选)

make clean 在源码有大规模更新或者内核更新后执行,以保证编译质量。此操作会删除/bin和/build_dir目录中的文件。

清除旧的编译产物、交叉编译工具及工具链等目录(可选)

make dirclean 更换架构编译前必须执行。此操作会删除/bin和/build_dir目录的中的文件(make clean)以及/staging_dir、/toolchain、/tmp和/logs中的文件。

清除 Open­Wrt 源码以外的文件(可选)

make distclean 除非是做开发,并打算 push 到 GitHub 这样的远程仓库,否则几乎用不到。此操作相当于make dirclean外加删除/dl、/feeds目录和.config文件。

还原 Open­Wrt 源码到初始状态(可选)

git clean -xdf 如果把源码改坏了,或者长时间没有进行编译时使用。

清除临时文件

rm -rf tmp 删除执行make menuconfig后产生的一些临时文件,包括一些软件包的检索信息,删除后会重新加载package目录下的软件包。若不删除会导致一些新加入的软件包不显示。

删除编译配置文件

rm -f .config 在不删除的情况下如果取消选择某些组件它的依赖组件不会自动取消,所以对于需要调整组件的情况下建议删除。 编译

调整 Open­Wrt 系统组件

make menuconfig 如果不打算调整组件则输入make defconfig,它会检测编译环境并根据更新自动调整编译配置文件。

预下载编译所需的软件包

make download -j8 V=s

检查文件完整性

find dl -size -1024c -exec ls -l {} \; 此命令可以列出下载不完整的文件(根据我多次编译的经验得出小于1k的文件属于下载不完整),如果存在这样的文件可以使用find dl -size -1024c -exec rm -f {} \;命令将它们删除,然后重新执行make download下载并反复检查,确认所有文件完整可大大提高编译成功率,避免浪费时间。

开始编译

make -j$(nproc) || make -j1 || make -j1 V=s 多线程编译失败后自动进入单线程编译,失败则输出详细日志。 尾巴

很少有人会告诉你为什么要这样做,而是会要求你必须要这样做。

 

 

本地编译路由器固件

这里从 coolsnowwolf/lede 源码仓库上介绍的编译步骤开始操作:

首先是注意事项 首先装好 Ubuntu 64bit,推荐 Ubuntu 18 LTS x64 不要用 root 用户 git 和编译!!! 国内用户编译前最好准备好软件源下载加速 默认登陆IP 192.168.1.1, 密码 password(后面文章中对 IP 有修改 编译前准备 命令行输入 sudo apt-get update -y 对系统进行更新 输入 sudo apt-get -y install build-essential asciidoc binutils bzip2 gawk gettext git libncurses5-dev libz-dev patch python3.5 python2.7 unzip zlib1g-dev lib32gcc1 libc6-dev-i386 subversion flex uglifyjs git-core gcc-multilib p7zip p7zip-full msmtp libssl-dev texinfo libglib2.0-dev xmlto qemu-utils upx libelf-dev autoconf automake libtool autopoint device-tree-compiler g++-multilib antlr3 gperf wget swig 安装编译固件需要的依赖 使用 git clone https://github.com/coolsnowwolf/lede 下载源码,然后 cd lede 进入源码目录,下面的所有操作都是基于源码目录内的操作 个性化配置需要的功能

启用必要的软件源:使用 vim 编辑器来编辑 ./feeds.conf.default 文件

$ vim ./feeds.conf.default

然后将 #src-git helloworld https://github.com/fw876/helloworld 这一行的注释取消掉。编辑完成后这个文件看起来像这样:

src-git packages https://github.com/coolsnowwolf/packages src-git luci https://github.com/coolsnowwolf/luci src-git routing https://git.openwrt.org/feed/routing.git src-git telephony https://git.openwrt.org/feed/telephony.git src-git freifunk https://github.com/freifunk/openwrt-packages.git #src-git video https://github.com/openwrt/video.git #src-git targets https://github.com/openwrt/targets.git #src-git management https://github.com/openwrt-management/packages.git #src-git oldpackages http://git.openwrt.org/packages.git #src-link custom /usr/src/openwrt/custom-feed src-git helloworld https://github.com/fw876/helloworld 修改路由器固件的初始网络配置 配置 LAN 口的网络

如果不进行这一步骤的操作,该路由器固件安装成功后,路由器 LAN 口管理 IP 为 192.168.1.1。如果断网操作肯定是没问题的,但是如果在现有的网络环境下部署旁路由(旁路网关)的话,一般就会和主路由器的 IP 地址冲突。所以这里介绍一下如何在编译固件前修改路由器固件的默认 IP。

修改 zzz-default-settings 文件,在 exit 0 这一行上方添加自定义配置。具体操作如下:

$ vim ./package/lean/default-settings/files/zzz-default-settings # 打开要编辑的文件 uci set network.lan.ipaddr='192.168.1.254' # 默认 IP 地址 uci set network.lan.proto='static' # 静态 IP uci set network.lan.type='bridge' # 接口类型:桥接 uci set network.lan.ifname='eth0' # 网络端口:默认 eth0,第一个接口 uci set network.lan.netmask='255.255.255.0' # 子网掩码 uci set network.lan.gateway='192.168.1.1' # 默认网关地址(主路由 IP) uci set network.lan.dns='192.168.1.1' # 默认上游 DNS 地址 uci commit network 接着进行下一步编译

输入如下命令,更新编译源,并进入配置页面:

$ ./scripts/feeds update -a $ ./scripts/feeds install -a $ make menuconfig # 进入配置界面

配置页面如下图所示:

自定义需要的路由器固件

首先,根据路由器的平台和架构,在 Target System、Subtarget、Target Profile 中选择相应的选项。然后,作为旁路由,需要安装的插件其实很少,基本上就是广告过滤、解锁网易云等。具体操作如下:

如果不需要在路由上实现 USB 文件共享/Samba 多媒体共享,需要在 Extra-packages 菜单中取消选择 automount 和 autosamba

进入 Luci - Applications 选择需要的插件,这里我列出部分常见的插件:

# 我常用的几个,只做旁路由使用 luci-app-firewall # 防火墙和端口转发,必备 luci-app-adbyby-plus # 广告屏蔽大师 Plus+ luci-app-sfe # Turbo ACC 网络加速(开启 Fast Path 转发加速) luci-app-unblockNeteaseMusic # 解锁网易云 # 按需选择 luci-app-wol # 网络唤醒 luci-app-accesscontrol # 上网时间控制 luci-app-arpbind # IP/MAC 绑定 luci-app-autoreboot # 计划重启 luci-app-ddns # 动态域名解析 luci-app-filetransfer # 上传 ipk 文件功能 luci-app-frpc # 内网穿透 Frp luci-app-ipsec-vpnd # IPSec 服务端 luci-app-nlbwmon # 网络带宽监视器 luci-app-ramfree # 释放内存 luci-app-rclone # rclone luci-app-samba # 网络共享(samba) luci-app-upnp # 通用即插即用 UPnP(端口自动转发) luci-app-vlmcsd # KMS 服务器(WIN 激活工具) luci-app-vsftpd # FTP 服务器 luci-app-webadmin # Web 管理页面设置 luci-app-xlnetacc # 迅雷下载 luci-app-zerotier # 虚拟局域网 luci-app-aria2 # Aria2 下载 luci-app-hd-idle # 硬盘休眠 luci-app-mwan3 # MWAN 负载均衡 luci-app-openvpn # OpenVPN 客户端 luci-app-openvpn-server # OpenVPN 服务端 luci-app-pptp-server # PPTP 服务端 luci-app-sqm # 流量智能队列管理(QOS) luci-app-transmission # BT 下载 luci-app-usb-printer # USB 打印服务器 luci-app-wireguard # VPN 服务器 WireGuard 状态 luci-app-wrtbwmon # 实时流量监测

配置完成后光标移动到 Save ,回车保存。

继续编译命令 make -j8 download V=s 下载dl库(国内请尽量做好软件源加速) 输入 make -j1 V=s (-j1 后面数字1是线程数。第一次编译推荐用单线程)进行编译你要的固件。

编译完成后固件保存在 ./bin/targets/ 文件夹内。

使用 Github 的 Action 功能自动编译

首先需要有一个 Github 帐号

打开 coolsnowwolf/lede 固件页面,点击 Folk 将该源码 Folk 到自己的帐号。然后以该源码自带的 Action 为例来修改。文件路径为 .github/workflows/openwrt-ci.yml。

文件第 13/14 行为自动运行周期,默认值如下:

schedule: - cron: 0 20 * * * # 每天的 20:00 执行该文件定义的动作 提取本地自定义固件和默认配置的差异

在上面本地编译时,当完成“配置完成后光标移动到 Save ,回车保存。”后,会在源码根目录生成一个 .config 文件,此时运行下面命令来提取配置差异,并保存在 diff.config 文件中备用:

make defconfig ./scripts/diffconfig.sh > diff.config # 将差异保存在 diff.config 文件中 在 openwrt-ci.yml 中添加自定义配置 启用软件源

修改 openwrt-ci.yml 第46行的 Update feeds 动作,将 echo 'src-git helloworld https://github.com/fw876/helloworld' >> ./feeds.conf.default 添加进去,添加完成后该部分如下:

- name: Update feeds run: | echo 'src-git helloworld https://github.com/fw876/helloworld' >> ./feeds.conf.default sed -i 's/\"#src-git\"/\"src-git\"/g' ./feeds.conf.default ./scripts/feeds update -a ./scripts/feeds install -a 添加自定义的固件配置

修改 openwrt-ci.yml 第52行的 Generate configuration file 动作,将之前提取的差异文件 diff.config 中的内容全部拷贝出来,添加中间那一行:

- name: Generate configuration file run: | rm -f ./.config* touch ./.config cat >> .config .config [*] tar.gz  # 压缩包格式[*] ext4  ---> # 可选ext4的最大节点数以及block大小[*] Build GRUB images (Linux x86 or x86_64 host only)[*]   Use Console Terminal (in addition to Serial)   # 开启串口调试终端(ttyS0) Serial port device # 串口设备名,一般为ttyS0(115200) Serial port baud rate # 波特率,选择的与串口工具设置的波特率必须一致(3)   Seconds to wait before booting the default entry # grub倒计时,单位为秒(4) Kernel partition size (in MB)  # 内核的大小,单位为MB,一般3、4MB足够(64) Root filesystem partition size (in MB) # 根文件系统大小,这个值越大,镜像体积越大,一般路由系统几十MB足够3、镜像配置(系统脚本及IP配置)[*] Image configuration  --->--- Preinit configuration options  [*]   Suppress stderr messages during preinit  (2)   Failsafe wait timeout  [ ]   Show all preinit network messages  [ ]   Suppress network message indicating failsafe  ()    Preinit network interface  # 默认IP地址  (192.168.1.1) IP address for preinit network messages  (255.255.255.0) Netmask for preinit network messages  (192.168.1.255) Broadcast address for preinit network messages4、基础系统本配置项为构建基本的文件系统,基本工具,库,等。默认即可。

Base system  --->      base-files................................... Base filesystem for OpenWrt # 基本文件系统 dropbear........................................ Small SSH2 client/server #ssh服务器-*- libc........................................................... C library # C库-*- libgcc............................................... GCC support library # gcc支持库-*- libpthread.......................................... POSIX thread library # 线程库-*- librt................................ POSIX.1b RealTime extension library# 运行时库5、root权限命令如sudo等命令

Administration  --->  6、Boot Loader此项无内容Boot Loaders  ---->   

7、开发相关开发专用,会安装如gcc、gdb、ar、patch、binutils等工具,普通用户无须关注。Development  --->    

8、固件个别模块的固件,如是,则要加入内核。X86平台,无须理会。Firmware  --->       

9、内核模块真正的内核配置在此处。本模块较重要,内容较多。但本文使用的X86平台上需要配置的东西不多。Kernel modules  ---> 9.1、块设备Block Devices  --->-*- kmod-scsi-core....................................... SCSI device support9.2、文件系统本次移植使用的文件系统为EXT4。其它不需要。Filesystems  ---> kmod-fs-ext4..................................... EXT4 filesystem support-*- kmod-fs-nfs....................................... NFS filesystem support-*- kmod-fs-nfs-common......................... Common NFS filesystem modules9.3、硬件监控模块如LM75,不使用。Hardware Monitoring Support  --->  9.4、I2C支持该X86平台使用IGB网络驱动,需要I2C的支持,故选择。I2C support  ---> kmod-i2c-core................................................ I2C support

9.5、输入模块如USB鼠标、键盘,等。Input modules  --->   -*- kmod-hid..................................................... HID Devices kmod-hid-generic.............................. Generic HID device support-*- kmod-input-core........................................ Input device core-*- kmod-input-evdev...................................... Input event device  9.6、本地语言支持Native Language Support  --->-*- kmod-nls-base.................................... Native Language Support9.7、netfilter扩展iptables的选项需要内核的支持,在此进行选择。文本使用默认值。Netfilter Extensions  --->9.8、网络设备该X86平台使用的网络设备驱动为IGB,其它不选。Network Devices  ---> -*- kmod-ifb........................... Intermediate Functional Block support kmod-igb....... Intel(R) 82575/82576 PCI-Express Gigabit Ethernet support # IGB驱动-*- kmod-libphy.................................................. PHY library # PHY库,必须-*- kmod-mii..................................................... MII library9.9、网络支持对网络的支持,比如8021q、DNS、ipv6。Network Support  ---> kmod-8021q........................................... 802.1Q VLAN support-*- kmod-dnsresolver.................................. In-kernel DNS Resolver-*- kmod-ipv6................................................... IPv6 support9.10、其它模块openwrt的menuconfig涉及大量其它东西,而内核只是其中一部分,故不像真正的kernel的menuconfig那样分类详细。很多字符类设备都在此选项。比如MMC、EEPROM、RTC、串口。Other modules  ---> kmod-serial-8250.............................................. 8250 UARTs   # 串口设备9.11、SPI支持SPI驱动,不使用。SPI Support  --->9.12、音频支持不使用音频驱动。Sound Support  --->9.13、USB支持USB接口输入设备,U盘支持,在此处选择。USB Support  --->   -*- kmod-usb-core............................................ Support for USB kmod-usb-ohci............................... Support for OHCI controllers kmod-usb-storage..................................... USB Storage support9.14、视频支持不使用视频功能。Video Support  --->9.15、无线驱动不使用无线驱动。Wireless Drivers  --->

10、编程语言如Java、Lua、PHP、Perl、Python、Ruby。由于openwrt使用Lua,建议选上。Languages  --->      

11、库常用库。如压缩为libbz2、SSL库、libexif,等,使用默认选项。SSL建议选择。Libraries  --->      

12、界面选项配置openwrt的界面使用LuCI,功能项、工具在此配置。内容较庞大、较重要。LuCI  --->1. Collections  --->2. Modules  --->    3. Applications  --->4. Themes  --->     5. Protocols  --->  6. Libraries  --->  9. Freifunk  --->12.1、综合1. Collections  --->    -*- luci   luci-ssl......................... Standard OpenWrt set with HTTPS support

12.2、模块LuCI基本模块在此配置。包含各种语言支持。2. Modules  --->-*- luci-base............................................ LuCI core librariesTranslations  ---> Chinese (zh-cn)        # 中文支持-*- luci-mod-admin-full. LuCI Administration - full-featured for full control

12.3、应用程序界面上的应用程序在此配置。如防火墙、QOS、NTP同步,等。3. Applications  --->   -*- luci-app-firewall................ Firewall and Portforwarding application luci-app-ntpc.............. NTP time synchronisation configuration module luci-app-qos..................... Quality of Service configuration module luci-app-vnstat.................................. LuCI Support for VnStat

12.4、主题默认主题为luci-theme-bootstrap。4. Themes  --->     -*- luci-theme-bootstrap........................... Bootstrap Theme (default) luci-theme-freifunk-bno.................... Freifunk Berlin Nordost Theme luci-theme-freifunk-generic....................... Freifunk Generic Theme luci-theme-openwrt................................ LuCI OpenWrt.org theme12.5、协议如3G、ipv6、PPP。5. Protocols  --->   luci-proto-3g............................................. Support for 3G-*- luci-proto-ipv6........... Support for DHCPv6/6in4/6to4/6rd/DS-Lite/aiccu-*- luci-proto-ppp.......................... Support for PPP/PPPoE/PPPoA/PPtP12.6、库6. Libraries  --->  -*- luci-lib-ip....... Lua library for IP calculation and routing information-*- luci-lib-nixio....................................... NIXIO POSIX library 

13、邮件邮件服务。不使用。Mail  --->           

14、多媒体多媒体配置,如ffmpeg、流媒体播放工具。不使用。Multimedia  --->     

15、网络本项为网络相关工具、模块的配置。内容较多,也较重要。Network  --->SSH  --->-*- openssh-client............................................ OpenSSH client-*- openssh-keygen............................................ OpenSSH keygen openssh-server............................................ OpenSSH serverTime Synchronization  ---> # 时间同步-*- ntpclient............................. NTP (Network Time Protocol) client ntpdate..................................................... ISC ntp dateVPN  ---> # VPN,不使用Web Servers/Proxies  --->-*- uhttpd........................ uHTTPd - tiny, single threaded HTTP server # 小型web服务器 ethtool......................... Display or change ethernet card settings iperf tcpdump..................... Network monitoring and data acquisition tool

16、音频不使用音频相关工具、库,不用配置。Sound  --->          

17、其它工具一些其它小工具在此。内容较杂。有的是boot loader,有的是压缩库,有的是编辑器(vim)。还有其它工具,如minicom、grep、tar、bash、file,等。根据实际选择。Utilities  --->   Editors  --->  # 编辑器 vim.............................. Vi IMproved - enhanced vi editor (Tiny)Terminal  --->  # 终端工具 minicom....................................... Terminal emulation programdatabase  --->  # 数据库,如mysql、sqlitezoneinfo  --->  # 时区信息 bash.......................................... The GNU Bourne Again Shell grep.................................. grep search utility - full version hwclock.................................. query or set the hardware clock tar.............................................................. GNU tar

 

 

OpenWrt 添加usb驱动及相关脚本

第一步,添加内核相关支持编译openwrt支持USB自动mount功能

make menuconfig1). 添加USB相关支持Kernel modules —> USB Support —> kmod-usb-core.Kernel modules —> USB Support —> kmod-usb-ohci.Kernel modules —> USB Support —> kmod-usb-uhci.Kernel modules —> USB Support —> kmod-usb-storage.Kernel modules —> USB Support —> kmod-usb-storage-extras.Kernel modules —> USB Support —> kmod-usb2.

2). 添加SCSI支持Kernel modules —> Block Devices —> kmod-scsi-core

3). 添加USB挂载Base system —> block-mount

4). 添加自动挂载工具Utilities —> Filesystem —> badblocks

5). 添加文件系统支持Kernel modules —> Filesystems —> kmod-fs-ext4Kernel modules —> Filesystems —> kmod-fs-vfatKernel modules —> Filesystems —> kmod-fs-ntfs

6). 添加UTF8编码,CP437编码,ISO8859-1编码Kernel modules —> Native Language Support —> kmod-nls-cp437Kernel modules —> Native Language Support —> kmod-nls-iso8859-1Kernel modules —> Native Language Support —> kmod-nls-utf8

Utilities ---> disc ---> fdisk.................................... manipulate disk partition tableUtilities ---> usbutils................................... USB devices listing utilities

第二步,开机自动挂载df一下应该就可以看到挂载的USB硬盘了

下面写一个启动脚本,让路由器在启动时自动挂载USB硬盘。

vim /etc/init.d/usbdisk

写入脚本:

#!/bin/sh /etc/rc.commonSTART=99STOP=10start() {mount -t ext3 -o noatime /dev/scsi/host0/bus0/target0/lun0/part1 /mnt}stop(){syncumount /dev/scsi/host0/bus0/target0/lun0/part1}restart(){stopstart}加入可执行权限

chmod 755 /etc/init.d/usbdisk

加入到启动目录

ln -s /etc/init.d/usbdisk /etc/rc.d/S99usbdisk

第三步,热插拔支持

自动挂载脚本,在/etc/hotplug.d/block/创建一个30-usbmount的文件,#!/bin/shcase "$ACTION" inadd)for i in $(ls /dev/ | grep 'sd[a-z][1-9]')domkdir -p /mnt/$imount -o io,rw /dev/$i /mnt/$iif [ "$?" -ne 0 ];thenmount -o rw /dev/$i /mnt/$ifidone;;remove)MOUNT=`mount | grep -o '/mnt/sd[a-z][1-9]'`

for i in $MOUNT

doumount $idone;;esac

 

================ End



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3