📜 ⬆️ ⬇️

Build custom firmware OpenWRT

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

Before proceeding with the description of the assembly, I’ll dwell on how the OpenWRT root file system works. It is a mini_fo file system that transparently merges two other file systems: an immutable SquashFS mounted to / rom, and a variable JFFS2 mounted to / 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 into / rom gives smaller firmware. The exclusion of unnecessary packages from the firmware also allows you to save such a valuable place on a flash drive.

To build custom firmware you need to 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 was the 10.03 directory corresponding to the version of the OpenWRT firmware being used, where I unpacked the ImageBuilder archive. The minimum image is assembled using the make image command , with which you can also specify three variables:

For ease of reuse, I created the make_image.sh script, where I invoke the 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/" 

It was very convenient to be able to add your files to the firmware image. Using the fact that all new and modified files are 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 deleted. Since the tar archiver built into busybox only supports the -X option to exclude files, you need to create the file / etc / backup_exclude on the router with the following contents:
 META_* 

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

New and modified files are placed in the directory referenced by the FILES variable, preserving the structure of subdirectories. Run the script make_image.sh and get in the bin directory pre-configured firmware with all the necessary software.

')

Source: https://habr.com/ru/post/127552/


All Articles