This translation is older than the original page and might be outdated. See what has changed.

使用Image Builder

另请参阅: Image Builder frontends, Using the toolchain, Quick image building guide

Image Builder (以前称为 Image Generator) 是一个预编译环境, 适用于创建自定义映像, 而无需从源代码编译它们. 它下载预编译的软件包并将它们集成到单个可闪存映像中.

在以下情况下, 这样做很有用:

  • 您想在较小的闪存中容纳更多的封装
  • 你想关注开发快照
  • 您的设备具有 32MB 或更少的 RAM 并且 opkg 无法正常工作
  • 你想批量刷新几十个设备,你需要一个特定的映像设置

Image Builder 映像与官方映像不同, 因为它们获取预先生成的包. 在进行 recent/important 的更改时, 这些包的传播可能会有一些延迟.最好检查包是否在 imagebuilder/change 日期之后上载.

  • Image Builder 仅在 64 位 Linux 中运行. 但您可以在 PC 或 VM(例如 VirtualBox)中运行 64 位 Linux, 甚至可以从 32 位 Windows 运行.
  • Image Builder 具有与 Build system 类似的先决条件.

最常见发行版中的示例依赖项:

sudo pacman -S --needed base-devel ncurses zlib gawk git gettext \
openssl libxslt wget unzip python
sudo dnf install git gawk gettext ncurses-devel zlib-devel \
openssl-devel libxslt wget which @c-development @development-tools \
@development-libs zlib-static which python3
sudo apt install build-essential libncurses5-dev libncursesw5-dev \
zlib1g-dev gawk git gettext libssl-dev xsltproc rsync wget unzip python

您可以下载包含 Image Builder 的存档, 它通常位于您找到设备固件映像的同一下载页面中.

例如, 这是您可以下载 ath79/generic 设备的所有固件映像的页面: https://downloads.openwrt.org/snapshots/targets/ath79/generic/ 您会找到一个 openwrt-imagebuilder-ath79-generic.Linux-x86_64.tar.xz 包含映像生成器的存档. 此外, 它始终由构建系统创建, 因为创建映像文件需要它. 如果启用了 “Build the OpenWrt Image Builder” 选项, 则映像构建器将在您找到固件映像的文件夹中生成 (source/bin/targets/xxx) , 您可以使用它从编译期间获得的包中创建更多映像.

所有操作都应使用非 root 用户帐户执行.

解压存档并更改工作目录:

tar -J -x -f openwrt-imagebuilder-*.tar.xz
cd openwrt-imagebuilder-*/

可以使用以下变量自定义映像构建:

变量 描述
PROFILE 指定要构建的目标映像
PACKAGES 要嵌入映像的软件包列表
FILES 包含自定义文件的目录
BIN_DIR 映像的替代输出目录
EXTRA_IMAGE_NAME 将其添加到输出映像文件名 (sanitized)
DISABLED_SERVICES 要禁用的 /etc/init.d 中服务的名称, 例如. 用于 dnsmasq 的 dhcp

运行 make help 以获取 详细帮助.

PROFILE 变量指定要构建的目标映像.

PROFILE="profile-名称"

运行 make info 以获取 可用配置文件的列表.

PACKAGES 变量允许在固件映像中含 和安装/或排除 插件. 默认情况下( PACKAGES 变量值为空), Image Builder 将创建一个具有特定于设备的内核和驱动程序, uci, ssh, switch, 防火墙, ppp 及 ipv6 支持的最小映像.

PACKAGES="pkg1 pkg2 pkg3 -pkg4 -pkg5 -pkg6"

上面的例子将插件含 pkg1, pkg2, pkg3, 并排除 pkg4, pkg5, pkg6, 注意每个排除插件前的 “-” 字符.

您不需要在此列表中列出您需要的插件的所有依赖项, Image Builder 使用 opkg 自动解析插件依赖项并安装其他所需的插件.

可以使用以下命令获取设备上当前安装的插件列表:

echo $(opkg list-installed | sed -e "s/\s.*$//")

许多设备的存储容量有限, 并且无法保证构建系统会检测到您添加了太多包以适应设备存储空间, 这可能会导致设备无法启动(如果安装). 如果有疑问, 请不要过度. 使用您最后安装在设备上的内容作为指导或先创建一个最小映像, 将其安装到设备上并测试您要先添加的内容, 考虑删除不必要的插件以 节省固件空间.

此外,诸如 libubus20191227 或类似的 ABI 版本控制包可能会导致映像生成器出现问题. 当这些作为插件提供时, 您可能会遇到编译错误. 为避免出现问题, 您应该从映像生成器中省略它们, 并通过插件依赖项安装正确的版本 . 该 --strip-abi 参数可用于导出规范化的插件列表.

如果有自定义插件或ipk, 您更愿意使用创建一个 packages 目录(如果不存在), 并将您的自定义ipk放置在此目录中.

FILES 变量允许将自定义配置文件包含在使用 Image Builder 构建的映像中. 如果您需要在刷机前更改默认网络配置, 或者如果您正在准备大量刷机许多设备的映像, 这将特别有用.

FILES="files"

files 目录应放置在发出 make 命令的 Image Builder 根目录中,否则指定绝对/完整路径.

强烈建议使用 uci-defaults 仅增量集成所需的自定义. 这有助于最大限度地减少与自动生成的设置的冲突,这些设置可以在版本之间更改.

参考: uci-default_example

选择适当的配置文件, 插件和自定义文件夹, 将其传递给 make image 命令.

make image PROFILE="profile-name" PACKAGES="pkg1 pkg2 pkg3 -pkg4 -pkg5 -pkg6" FILES="files"

make 命令完成后, 生成的镜像存放在 bin/设备架构名 目录下, 就像编译一样.

构建的固件将在子目录下找到 ./bin/targets/<target>/generic 或查看里面 ./build_dir/ 对于文件 *-squashfs-sysupgrade.bin*-squashfs-factory.bin (例如. /build_dir/target-mips_24kc_musl/linux-ar71xx_tiny/tmp/openwrt-18.06.2-ar71xx-tiny-tl-wr740n-v6-squashfs-factory.bin)

要清理临时构建文件和生成的固件,请使用该 make clean 命令.

以下示例显示:

  • 为配置文件创建目录.
  • 使用 scp 发送 uci 配置文件到 WL500GP 路由器里的 files/etc/config 目录中.
  • 使用自定义插件和 uci 配置文件为 WL500GP 生成固件映像 .
mkdir -p files/etc/config
scp root@192.168.1.1:/etc/config/network files/etc/config/
scp root@192.168.1.1:/etc/config/wireless files/etc/config/
scp root@192.168.1.1:/etc/config/firewall files/etc/config/
make image PROFILE="wl500gp" PACKAGES="nano openvpn -ppp -ppp-mod-pppoe" FILES="files"
  1. 您是否以非 root 用户身份运行所有内容?
  2. 检查日志输出, 是否存在插件问题 (冲突, 导入名是否正确)?
  3. 检查日志输出, 是否超出了最大空间?
  4. 每天等待几个小时上游 packages 可能处于不一致状态, 尤其是在 master/snapshot 上
  5. 验证您拥有受支持的操作系统, 先决条件, 文件系统和路径命名

T以下主题不仅限于简单的使用,而且面向开发人员和高级用户.

另请参阅 ImageBuilder makefile

获取详细帮助:

# make help

可用命令参数:
	help:	此帮助文本
	info:	显示可用目标配置文件的列表
	clean:	删除映像和临时构建文件
	image:	构建映像 (请参见下文有关更多信息).

构建镜像:
	默认情况下 'make image' 将使用默认的目标配置文件和包集创建一个镜像. 
	您可以使用以下参数进行更改:

	make image PROFILE="<profilename>" # 覆盖默认目标配置文件
	make image PACKAGES="<pkg1> [<pkg2> [<pkg3> ...]]" # 包括额外的插件
	make image FILES="<path>" # 包含来自 <path> 的额外文件
	make image BIN_DIR="<path>" # 固件生成的可选输出目录
	make image EXTRA_IMAGE_NAME="<string>" # 将其添加到输出固件文件名 (sanitized)
	make image DISABLED_SERVICES="<svc1> [<svc2> [<svc3> ..]]" # 应禁用 /etc/init.d/ 中的哪些服务
	make image ADD_LOCAL_KEY=1 # 在构建的映像中存储本地生成的签名密钥

打印清单:
	列出安装到固件映像中的 "all" 所有插件.
	您可以使用以下参数:

	make manifest PROFILE="<profilename>" # 覆盖默认的目标配置文件
	make manifest PACKAGES="<pkg1> [<pkg2> [<pkg3> ...]]" # 包含额外的插件
	make manifest STRIP_ABI=1 # 从打印的插件名中删除 ABI 版本

列出可用的配置文件:

# make info

Available Profiles:

Default:
    Default Profile
    Packages: kmod-usb-core kmod-usb2 kmod-usb-ohci kmod-usb-ledtrig-usbport
ai-br100:
    Aigale Ai-BR100
    Packages: kmod-usb2 kmod-usb-ohci
rp-n53:
    Asus RP-N53
    Packages:
rt-n14u:
    Asus RT-N14u
    Packages:
whr-1166d:
    Buffalo WHR-1166D
    Packages:
whr-300hp2:
    Buffalo WHR-300HP2
    Packages:
...

可以使用 buildroot 来创建您自己的 Image Builder 并将所有插件集成到其中, 这样它就可以在不下载包的情况下生成固件映像.

在图形配置中, 选择 “Build the OpenWrt Image Builder” 来编译镜像生成, 然后选择 Global Build Settings → Select all packages by default, 保存(save)和退出(exit). 如果遇到无法编译的未维护插件, 您可以忽略构建错误 , 假设这不会影响内核和核心依赖项.

不要在路径中调用 make defconfig 或保留旧 .config 文件, 因为 Select all packages by default只会将插件选择设置 [m] 为尚未配置的插件! make defconfig 将大多数包设置为 [n]不编译.

The Image Builder you download from the OpenWrt pages is already configured to download any non-default packages from official repositories. The package sources are configured in the repositories.conf file in the extracted directory. Sources are specified in opkg native config format. This can be either the official package repositories or custom generated repositories.

An example of the contents of the repositories.conf from the openwrt-imagebuilder-18.06.0-rc2-ramips-mt7621.Linux-x86_64.tar.xz:

## Place your custom repositories here, they must match the architecture and version.
# src/gz %n http://downloads.openwrt.org/releases/18.06.0-rc2
# src custom file:///usr/src/openwrt/bin/ramips/packages
 
## Remote package repositories
src/gz openwrt_core http://downloads.openwrt.org/releases/18.06.0-rc2/targets/ramips/mt7621/packages
src/gz openwrt_base http://downloads.openwrt.org/releases/18.06.0-rc2/packages/mipsel_24kc/base
src/gz openwrt_luci http://downloads.openwrt.org/releases/18.06.0-rc2/packages/mipsel_24kc/luci
src/gz openwrt_packages http://downloads.openwrt.org/releases/18.06.0-rc2/packages/mipsel_24kc/packages
src/gz openwrt_routing http://downloads.openwrt.org/releases/18.06.0-rc2/packages/mipsel_24kc/routing
src/gz openwrt_telephony http://downloads.openwrt.org/releases/18.06.0-rc2/packages/mipsel_24kc/telephony
 
## This is the local package repository, do not remove!
src imagebuilder file:packages

The repositories.conf in an imagebuilder you compile from source will lack the “Remote package repositories” links.

If you want to add a custom local repository, copy the src custom file:///usr/src/openwrt/bin/ramips/packages line and modify it to point to the local folder you have your packages and package lists in. If you have problems with using you local repository because the “Signature check failed” then remove the line option check_signature from repositories.conf

If you have custom repositories online, copy and modify the src/gz reboot http://downloads.openwrt.org/snapshots line instead.

NOTE: if you want to override packages coming from an existing feed, you must write your custom feed ABOVE the line of the package feed containing the packages you want to override, as shown in the examples above.

Create a non-privileged admin user and lock root password. Configure privilege elevation with sudo. Set up key-based authentication and disable password authentication for Dropbear.

mkdir -p files/etc/uci-defaults
cat << "EOF" > files/etc/uci-defaults/99-custom
USER_NAME="admin"
USER_GROUP="admin"
USER_SSHPUB="SSH_PUBLIC_KEY"
USER_SHELL="/bin/ash"
SUDO_USER="root"
SUDO_GROUP="sudo"
SUDO_GID="27"
uci set dropbear.@dropbear[0].PasswordAuth="0"
uci set dropbear.@dropbear[0].RootPasswordAuth="0"
uci commit dropbear
/etc/init.d/dropbear restart
passwd -l "${SUDO_USER}"
groupadd -r -g "${SUDO_GID}" "${SUDO_GROUP}"
touch /etc/environment
cat << EOI > /etc/sudoers.d/00-custom
%${SUDO_GROUP} ALL=(ALL) ALL
EOI
groupadd "${USER_GROUP}"
useradd -m -g "${USER_GROUP}" -G "${SUDO_GROUP}" \
-s "${USER_SHELL}" "${USER_NAME}"
passwd -d "${USER_NAME}"
USER_HOME="$(eval echo ~"${USER_NAME}")"
mkdir -p "${USER_HOME}"/.ssh
cat << EOI > "${USER_HOME}"/.ssh/authorized_keys
${USER_SSHPUB}
EOI
chown -R "${USER_NAME}:${USER_GROUP}" "${USER_HOME}"
chmod -R go= "${USER_HOME}"
EOF
make image FILES="files" PACKAGES="nano shadow sudo"

Examples below may contain version dependent / legacy information and are for informational purposes. They are very low level so expect to have a good level of skill and familiarity with the ImageBuilder / OpenWrt in general.

The image building is tied to the profile names. If you add a new profile without also adding an appropriate macro to the image-generation Makefile, no suitable firmware file will get generated when using the custom profile. Remove the /tmp directory to properly apply the modified package selection from profiles.

The location of the profiles for the pre-compiled package for brcm47xx-for-Linux-i686 was target/linux/brcm47xx/profiles/

Remarkably, all that needs to be done to add a new profile, is to add a new file to the profiles directory. While this may have been the case in earlier releases, for 17.01, it appears that manual editing of .targetinfo is also required.

Here is what the profiles/100-Broadcom-b43.mk profile file looks like:

define Profile/Broadcom-b43
	NAME:=Broadcom BCM43xx WiFi (default)
	PACKAGES:=kmod-b43 kmod-b43legacy
endef
 
define Profile/Broadcom-b43/Description
	Package set compatible with hardware using Broadcom BCM43xx cards
endef
$(eval $(call Profile,Broadcom-b43))

Alternately edit the hidden .profile.mk file at the top level directory of the image builder and manually add the names of the desired packages to be added to the output image. An “ls -a” will reveal the files hidden in the various directories.

This is not a standard feature of the Image Builder.

It is highly recommended that you test file removal prior to incorporating such changes at the image builder level or that you have low level means to recover a device before attempting this type of mod, as bricking / non booting may result.

Note that it requires patching of the Makefile

It is based on older Chaos Calmer era code… and not applicable to modern ImageBuilders but useful as a reference…

Create file files_remove with full filenames:

/lib/modules/3.10.49/ts_bm.ko
/lib/modules/3.10.49/nf_nat_ftp.ko
/lib/modules/3.10.49/nf_nat_irc.ko
/lib/modules/3.10.49/nf_nat_tftp.ko

Patch Makefile:

 ifneq ($(USER_FILES),)
 	$(MAKE) copy_files
 endif
+
+ifneq ($(FILES_REMOVE),)
+	@echo
+	@echo Remove useless files
+
+	while read filename; do \
+	    rm -rfv "$(TARGET_DIR)$$filename"; \
+	done < $(FILES_REMOVE);
+endif
+
 	$(MAKE) package_postinst
 	$(MAKE) build_image

Rebuild firmware:

make image \
PROFILE="tlwr841" \
PACKAGES="igmpproxy ip iptraf kmod-ipt-nathelper-extra openvpn-polarssl tcpdump-mini -firewall -ip6tables -kmod-ip6tables -kmod-ipv6 -odhcp6c -ppp -ppp-mod-pppoe" \
FILES_REMOVE="files_remove"
This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
  • Last modified: 2021/12/06 19:30
  • by guyezi