Imagebuilder为树莓派4B构建OPENWRT系统

环境配置说明

下载工具并配置

1
2
3
4
5
6
7
8
9
10
11
12
# 安装依赖
apt-get install subversion build-essential libncurses5-dev \
zlib1g-dev gawk git ccache gettext libssl-dev xsltproc wget \
unzip python

# 下载Imagebuilder工具包
wget https://downloads.openwrt.org/snapshots/targets/bcm27xx/bcm2711/openwrt-imagebuilder-bcm27xx-bcm2711.Linux-x86_64.tar.xz

# 解压并进入目录
xz -d openwrt-imagebuilder-bcm27xx-bcm2711.Linux-x86_64.tar.xz
tar -xvf openwrt-imagebuilder-bcm27xx-bcm2711.Linux-x86_64.tar
cd openwrt-imagebuilder-bcm27xx-bcm2711.Linux-x86_64

设置仓库地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# vi repositories.conf,可将下面的源修改为国内源
src/gz openwrt_core http://downloads.openwrt.org/snapshots/targets/bcm27xx/bcm2711/packages
src/gz openwrt_base http://downloads.openwrt.org/snapshots/packages/aarch64_cortex-a72/base
src/gz openwrt_luci http://downloads.openwrt.org/snapshots/packages/aarch64_cortex-a72/luci
src/gz openwrt_packages http://downloads.openwrt.org/snapshots/packages/aarch64_cortex-a72/packages
src/gz openwrt_routing http://downloads.openwrt.org/snapshots/packages/aarch64_cortex-a72/routing
src/gz openwrt_telephony http://downloads.openwrt.org/snapshots/packages/aarch64_cortex-a72/telephony
src imagebuilder file:packages

# 例如这样来使用腾讯云源,与上面二选一
src/gz openwrt_core http://mirrors.cloud.tencent.com/openwrt/snapshots/targets/bcm27xx/bcm2711/packages
src/gz openwrt_base http://mirrors.cloud.tencent.com/openwrt/snapshots/packages/aarch64_cortex-a72/base
src/gz openwrt_luci http://mirrors.cloud.tencent.com/openwrt/snapshots/packages/aarch64_cortex-a72/luci
src/gz openwrt_packages http://mirrors.cloud.tencent.com/openwrt/snapshots/packages/aarch64_cortex-a72/packages
src/gz openwrt_routing http://mirrors.cloud.tencent.com/openwrt/snapshots/packages/aarch64_cortex-a72/routing
src/gz openwrt_telephony http://mirrors.cloud.tencent.com/openwrt/snapshots/packages/aarch64_cortex-a72/telephony
src imagebuilder file:packages

查看默认软件包和硬件支持列表

1
2
3
4
5
6
7
8
9
10
11
12
leux@K680:~/imagebuilder$ make info
Current Target: "bcm27xx/bcm2711"
Current Revision: "r13150-47f17b0662"
Default Packages: base-files libc libgcc busybox dropbear mtd uci opkg netifd fstools uclient-fetch logd urandom-seed urngd bcm27xx-gpu-fw kmod-usb-hid kmod-sound-core kmod-sound-arm-bcm2835 kmod-fs-vfat kmod-nls-cp437 kmod-nls-iso8859-1 partx-utils mkf2fs e2fsprogs dnsmasq iptables ip6tables ppp ppp-mod-pppoe firewall odhcpd-ipv6only odhcp6c kmod-ipt-offload
Available Profiles:

rpi-4:
Raspberry Pi 4B
Packages: cypress-firmware-43455-sdio cypress-nvram-43455-sdio-rpi-4b kmod-brcmfmac wpad-basic iwinfo
hasImageMetadata: 1
SupportedDevices: raspberrypi,4-model-b
leux@K680:~/imagebuilder$

调整固件的分区大小

1
2
3
# vim .config
CONFIG_TARGET_KERNEL_PARTSIZE=64
CONFIG_TARGET_ROOTFS_PARTSIZE=104

自动配置网卡

在imagebuilder目录下创建目录 files/ 后面通过参数 FILES=files/ 来指定在构建时把files目录里的文件复制到固件根目录的对应位置
imagebuilder/files/etc/config/ 中新建两个文件:network,wireless内容如下用来分别配置接口和无线

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# files/etc/config/network

config interface 'loopback'
option ifname 'lo'
option proto 'static'
option ipaddr '127.0.0.1'
option netmask '255.0.0.0'

config globals 'globals'
option ula_prefix 'fd38:13ae:ceeb::/48'

config interface 'lan'
option type 'bridge'
option proto 'static'
option ipaddr '192.168.1.1'
option netmask '255.255.255.0'
option ip6assign '60'
option ifname 'eth0.1'
option _orig_ifname 'eth0 wlan0'
option _orig_bridge 'true'

config interface 'wan'
option ifname 'eth0'
option proto 'dhcp'
option _orig_ifname 'erspan0'
option _orig_bridge 'false'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# files/etc/config/wireless

config wifi-device 'radio0'
option type 'mac80211'
option channel '36'
option hwmode '11a'
option path 'platform/soc/fe300000.mmcnr/mmc_host/mmc1/mmc1:0001/mmc1:0001:1'
option htmode 'VHT40'

config wifi-iface 'default_radio0'
option device 'radio0'
option mode 'ap'
option encryption 'none'
option ssid 'RPI4'
option network 'lan'

开始构建固件

完成后把bin/targets/brcm27xx/bcm2711/openwrt-bcm27xx-bcm2711-rpi-4-ext4-factory.img.gz解压后的img文件刷入SD卡中即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# make image命令会创建一个只包含必不可少包文件的最小化镜像
# 这个命令可以通过下面三个传递过去的参数控制:
# PROFILE 指定要编译的目标镜像的配置
# PACKAGES 要嵌入镜像的包文件的列表
# FILES 要包含进去的自定义文件的目录

make image PROFILE=rpi-4 FILES=files/ PACKAGES=" \
luci luci-i18n-base-zh-cn luci-theme-material luci-app-qos \
luci-i18n-qos-zh-cn luci-app-upnp luci-i18n-upnp-zh-cn \
luci-app-firewall luci-i18n-firewall-zh-cn luci-app-opkg \
luci-i18n-opkg-zh-cn ariang luci-app-aria2 luci-i18n-aria2-zh-cn \
luci-app-samba luci-i18n-samba-zh-cn luci-app-frpc luci-i18n-frpc-zh-cn \
block-mount blockd fdisk lsblk curl wget htop kmod-rt2800-usb \
kmod-usb-net-sr9700 kmod-usb-net-asix kmod-usb-net-asix-ax88179 \
kmod-usb-net-rtl8152 kmod-fs-exfat kmod-fs-ext4 kmod-fuse kmod-fs-ntfs \
ntfs-3g kmod-fs-squashfs kmod-usb-core kmod-usb-ehci kmod-usb-ohci \
kmod-usb-storage kmod-usb-storage-extras kmod-usb-uhci kmod-usb2 kmod-usb3"

配置离线源(可选)

  1. 设置仓库地址
1
2
3
4
5
6
7
8
# 其实KMODS和CORE是同一个库,core是kmod库的软连接,这样便于阅读
KMODS_URL=https://downloads.openwrt.org/snapshots/targets/bcm27xx/bcm2711/kmods/4.19.108-1-24e1d7b0eb4a2f4572b17ce895fcd3ec/
CORE_URL=https://downloads.openwrt.org/snapshots/targets/bcm27xx/bcm2711/packages/
BASE_URL=https://downloads.openwrt.org/snapshots/packages/aarch64_cortex-a72/base/
LUCI_URL=https://downloads.openwrt.org/snapshots/packages/aarch64_cortex-a72/luci/
PKGS_URL=https://downloads.openwrt.org/snapshots/packages/aarch64_cortex-a72/packages/
ROUT_URL=https://downloads.openwrt.org/snapshots/packages/aarch64_cortex-a72/routing/
TELE_URL=https://downloads.openwrt.org/snapshots/packages/aarch64_cortex-a72/telephony/
  1. 创建对应目录
    mkdir core base luci packages routing telephony

  2. 进入对应目录并定义链接变量

1
2
cd core
URL=$CORE_URL
  1. 通过验证文件获取所有软件包的地址并下载它们
1
2
3
wget $URL/Packages $URL/Packages.asc $URL/Packages.gz $URL/Packages.manifest $URL/Packages.sig
cat Packages | grep Filename: | sed "s|Filename: |$URL|g" > url.txt
wget -c -i url.txt
  1. 然后重复步骤3和4,把步骤3中的目录kmods和链接KMODS_URL换为下一个仓库地址

  2. 配置离线源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 将SD卡的剩余空间格式化并挂载到/mnt/mmcblk0p3/下
# 再将下载的离线源拷贝到/mnt/mmcblk0p3/下,最后使用软链接到web文件夹中
ln -s /mnt/mmcblk0p3/mirrors /www/mirrors

# 将/etc/opkg/distfeeds.conf中默认的源全部注释掉,在/etc/opkg/customfeeds.conf中添加如下源:
src/gz openwrt_core http://127.0.0.1/mirrors/core
src/gz openwrt_base http://127.0.0.1/mirrors/base
src/gz openwrt_luci http://127.0.0.1/mirrors/luci
src/gz openwrt_packages http://127.0.0.1/mirrors/packages
src/gz openwrt_routing http://127.0.0.1/mirrors/routing
src/gz openwrt_telephony http://127.0.0.1/mirrors/telephony

# 也可这样设置为本地源,类似openwrt_core只能唯一,如相同需要重命名,否则会冲突报错
src/gz mirrors_core file:///www/mirrors/core
src/gz mirrors_base file:///www/mirrors/base
src/gz mirrors_luci file:///www/mirrors/luci
src/gz mirrors_packages file:///www/mirrors/packages
src/gz mirrors_routing file:///www/mirrors/routing
src/gz mirrors_telephony file:///www/mirrors/telephony