Build Script to Compile Custom Firmware for Devices with Small Memory

Script Features

  • Download and install necessary dependencies for compiling OpenWrt.
  • Download OpenWrt 18.06.4 tar.gz from the OpenWrt Releases page. This can be changed by editing the script.
  • Download and install OpenWrt Feeds.
  • Minify CSS, JS, and Lua files from the OpenWrt Feeds.

View the script here.


Instructions to Compile Firmware

1. Download and execute script

Note:This script follows the Quick Image Building Guide. Refer to this guide to understand how this script works.

  • Download script

wget -qO optimize-openwrt.sh https://gist.githubusercontent.com/sanchezpe/0d58591d128b31b7050e28b376f21981/raw

  • Optional: Edit the script to download a different OpenWRT release. For example, change line 15 in the script from:
    wget -qO openwrt.tar.gz https://github.com/openwrt/openwrt/archive/v18.06.4.tar.gz
    to:
    wget -qO openwrt.tar.gz https://github.com/openwrt/openwrt/archive/v17.01.7.tar.gz

  • Run script

chmod +x optimize-openwrt.sh
./optimize-openwrt.sh

  • Wait for a few minutes until the script finishes the execution.

  • Go to the directory where OpenWrt tar.gz was extracted.

cd openwrt

2. Free space

Note: Refer to the Saving firmware space guide for more information.

  • Open the make menuconfig window.

make menuconfig

  • Free space by changing the following settings in the make menuconfig window:
Target System
	(X) Atheros AR7xxx/AR9xxx
	
Subtarget (Generic)
	(X) Devices with small flash
	
Target Profile (Default Profile (all drivers))
	(X) Multiple devices 

Target Devices
	[*]{Select your router}

Target Images
	[*] squashfs (NEW)
		(256) Block size (in KiB)
			1024

Kernel modules
	Wireless Drivers
		kmod-mac80211
			[ ]   Export mac80211 internals in DebugFS
			[ ]   Enable 802.11s mesh support
	USB Support
		[ ]{Disable all options}

Global build settings
	Kernel build options
		[ ] Enable support for printk
		[ ] Crash logging
		[ ] Compile the kernel with debug filesystem enabled
		[ ] Support for paging of anonymous memory (swap)
		[ ] Compile the kernel with symbol table information
		[ ] Compile the kernel with debug information
		[ ] Compile the kernel with SysRq support
		[ ] Enable process core dump support
		[ ] Enable printk timestamps
	
	[ ] Enable IPv6 support in packages *OPTIONAL
	[*] Strip unnecessary exports from the kernel image
	[*] Strip unnecessary functions from libraries

3. Add Packages

  • Add desired packages. For example, to add LuCI, Adblock, DDNS, OpenVPN, QoS, UPnP, Wake-on-LAN packages for LuCI, mbed TLS and mbed TLS for OpenVPN, change the following settings in the make menuconfig window:
LuCI
	1. Collections
		<*> luci
	2. Modules
		[*] Minify Lua sources
	3. Applications
		<*> luci-app-adblock
		<*> luci-app-ddns
		<*> luci-app-openvpn
		<*> luci-app-qos OR <*> luci-app-sqm
		<*> luci-app-upnp
		<*> luci-app-wol
		
Libraries
	<*> libustream-mbedtls

Network
	VPN
		<*> openvpn-mbedtls

Note: Keep in mind that routers with 4MB flash will not be able to store many packages. Refer to OpenWrt Packages Table for more information about packages size.

  • Save your changes and exit.

3. Optimize kernel configuration

  • Open the make kernel_menuconfig window.

make kernel_menuconfig CONFIG_TARGET=ar71xx

  • Wait for a few minutes until the kernel compiles and the make kernel_menuconfig window appears.

  • Change the following settings in the make kernel_menuconfig window:

Machine selection
	Atheros AR71XX/AR724X/AR913X machine selection
		[*]{Select your router, if available}
	
Device Drivers
	Character devices
		Serial drivers 
			< > 8250/16550 and compatible serial support
	[ ] USB support

File systems
	Miscellaneous filesystems
		[*]     Additional option for memory-constrained systems
		(3)       Number of fragments cached
			1

Kernel hacking
	[ ] Early printk
  • Save changes and exit.

4. Compile firmware

  • Compile firmware using make.

make -i -j1 V=s 2>&1 | tee build.log | grep -i error

  • Wait until compilation finishes. This could take a long time.

  • Navigate to the directory that contains the compiled firmware images

cd /bin/targets/ar71xx/

6 Likes

suggest removing/commenting out apt upgrade...

nice writeup!

curious how much space this, or any of the tweaks/tradeofs is saving.

LuCI's images are already precompressed AFAIK. There's not a lot to be gained there but every bit helps on 4 MB.

this looks amazing, but im so afraid of making a mistake and letting me out of my only device

where i can get more info about de default configurations?
like setting up the default ip and enabling ssh access for example

i have an ubiquiti ac mesh/lite and i need as much free space as it can be
like a clean linux machine if possible

can anyone give me a hint about how to do this if is possible?

Your device's defaults are in /rom/etc/uci-defaults/. You can see what and how everything gets set there.

1 Like

Hi! Thanks for your job, description of building process with reducing image size is very good!
I tried your script on 19.07.3, and i can't see any difference in ready image size regardless of using your script's minimizers or not - ready image files have exactly the same size (in bytes).
But i noticed difference in size of "luci" folder in "bin/packages/mips_24kc" - the difference is about 1.7 kb..
Also I tried not to switch on config option "[*] Minify Lua sources" - this have much more impact to size of "luci" folder, but still ready image file have the same size.
After running of the script, the size of "feeds" folder reduces by about 700 kb.
I tried several times with different set of Luci applications selected - one without any applications and another with "luci-app-openvpn" (+libustream-mbedtls +openvpn-mbedtls) - the result was the same (of course, with applications the size was bigger, but not depends on using of script's minimizers).
What I did wrong?