Build custom OpenWRT firmware

Not so long ago, I was puzzled by the rise of the OpenVPN server on my D-Link DIR-320 router. But after installing the OpenWRT firmware, it turned out that there was not enough space on the 4-megabyte USB flash drive of the router to install OpenVPN. The way out of the situation was to build our own version of the firmware using Image Generator , which allowed us to get smaller firmware with the same set of packages.

Before continuing with the assembly description, I will dwell a little on how the root OpenWRT file system is arranged. It is a mini_fo file system that transparently integrates two other file systems: immutable SquashFS mounted in / rom, and mutable JFFS2 mounted on / overlay. All files that are created or modified after installing the firmware are in / overlay. When deleting files that were originally in the firmware, mini_fo simply marks them as deleted, the files themselves remain in / rom and continue to take up space. Both SquashFS and JFFS2 use compression, but SquashFS gives a better compression ratio, so putting all the necessary packages at once in / rom gives a smaller firmware. The exclusion of unnecessary packages from the firmware also allows you to save such a precious place on the flash drive.

To build custom firmware, you must download the ImageBuilder archive. For my router there was only an i686 version of the archive, but there were no problems with the 64-bit Gentoo. For everything related to the firmware, I created the ~ / dir-320 directory, and in it the 10.03 directory corresponding to the version of the OpenWRT firmware used, where I unpacked the ImageBuilder archive. The minimal image is built using the make image command , along with which you can also specify three variables:
  • PROFILE is an assembly profile that defines a list of additional packages for inclusion in the firmware. A list of profiles can be obtained using make info . For some reason, the variable did not have any effect on me, and the list of packages necessary for my router had to be specified manually.
  • PACKAGES - A list of packages to add or remove. The names of packages that need to be removed are preceded by a minus. During assembly, dependencies of packages are calculated, which are also added to the firmware.
  • FILES - path to the directory with additional files for inclusion in the firmware.

For ease of reuse, I created the make_image.sh script, where I call make image with the variable values ​​I need:
#!/bin/bash
make image PROFILE="Broadcom-b43" PACKAGES="kmod-b43 kmod-b43legacy -wpad-mini wpad ip iptables-mod-conntrack-extra ntpclient miniupnpd openvpn ddns-scripts nano" FILES="files/"

The ability to add your files to the firmware image turned out to be very convenient. Taking advantage of the fact that all new and changed files are located in / overlay, I wrote a backup.sh script to extract such files from the device:
#!/bin/bash
DATE=`date +%Y-%m-%dT%H:%M:%S`
BACKUP="backup-$DATE.tar.gz"
ROUTER=172.22.222.1
BASEDIR=`dirname $0`
FILELIST="etc/"
ssh root@$ROUTER tar -czf /tmp/$BACKUP -C /overlay -X /etc/backup_exclude $FILELIST
scp root@$ROUTER:/tmp/$BACKUP $BASEDIR/backup/$BACKUP

The META_ * files in / overlay are utility files in the mini_fo file system and should be excluded. Since the tar archiver built into busybox to exclude files supports only the -X option, you need to create the / etc / backup_exclude file on the router with the following contents:
META_*

In the same directory with the script, create a backup subdirectory, where the archives will be saved.

We put the new and changed files in the directory referenced by the FILES variable, preserving the structure of the subdirectories. Run the make_image.sh script and get the pre-configured firmware in the bin directory with all the necessary software.

Also popular now: