Openwrt we collect the firmware. Brief instructions for assembling OpenWRT

Not long ago I was puzzled by setting up an OpenVPN server on my D-Link DIR-320 router. But after installing the OpenWRT firmware, it turned out that there was not enough space left on the router’s 4 MB flash drive to install OpenVPN. The way out of the situation was to build our own version of the firmware using Image Generator, which made it possible to obtain smaller firmware with the same set of packages.

Before continuing with the description of the assembly, I’ll dwell a little on how the OpenWRT root file system is structured. It is a mini_fo filesystem that transparently combines two other filesystems: the immutable SquashFS mounted on /rom and the mutable JFFS2 mounted on /overlay. All files that are created or modified after installing the firmware are located in /overlay. When deleting files that were originally in the firmware, mini_fo simply marks them as deleted, while the files themselves remain in /rom and continue to take up space. Both SquashFS and JFFS2 use compression, but SquashFS gives better compression, so putting all the required packages in /rom at once results in smaller firmware. Excluding unnecessary packages from the firmware also saves valuable space on the 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 it on 64-bit Gentoo. For everything related to the firmware, I created a directory ~/dir-320, 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 command make image, along with which you can also specify three variables:

  • PROFILE - build profile, which defines a list of additional packages to be included in the firmware. The list of profiles can be obtained using make info. For some reason, the variable did not have any effect, and the list of packages required for my router had to be specified manually.
  • PACKAGES - list of packages to add or remove. The names of packages to be removed are preceded by a minus sign. During assembly, package dependencies are calculated and also added to the firmware.
  • FILES - path to a directory with additional files to be included in the firmware.
For ease of reuse, I created a script make_image.sh, 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 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
Files META_* in /overlay are service files in the mini_fo file system and must be excluded. Since the built-in tar archiver in busybox for excluding files only supports the -X option, you need to create a /etc/backup_exclude file on the router with the following contents:
META_*
In the same directory as the script, create a backup subdirectory where the archives will be saved.

New and changed files are placed in the directory referenced by the FILES variable, preserving the subdirectory structure. We run the script make_image.sh and get pre-configured firmware with all the necessary software in the bin directory.

We remind you that attempts to repeat the author’s actions may lead to loss of warranty on the equipment and even to its failure. The material is provided for informational purposes only. If you are going to reproduce the steps described below, we strongly advise you to carefully read the article to the end at least once. The editors of 3DNews do not bear any responsibility for any possible consequences.

Creating your own version of alternative firmware is useful only when you are not satisfied with the functionality of the existing assemblies - the necessary components are missing, and instead of them there is something completely unnecessary. At the same time, the amount of memory in the router is relatively small, and installing additional software on an external drive is not always possible. In principle, there is nothing complicated in creating your own firmware. For OpenWRT there is a utility Image Generator, which can remove or add software packages to an existing assembly. You can use it, or you can go the other way - compile your firmware with blackjack and... well, you get the idea. A general description of the process can be found on the project wiki.

So, we need a computer with Linux/*BSD/Mac OS on board, about five gigabytes of free hard disk space and a little patience. It is highly not recommended to compile the firmware in a virtual machine with a suitable OS, since the compilation process in this case may be unnecessarily delayed. As an example, let's look at working in Ubuntu 11.10. The first step is to install additional utilities and libraries that will be required during the build process. For other operating systems and distributions, the list of required software can be viewed. The minimum required set in our case is set by such a command.

Sudo apt-get install subversion build-essential libncurses5-dev zlib1g-dev gawk flex quilt git-core mkisofs

Now you need to create a working folder where the source code of the firmware, its settings, ready-made images, and so on will be saved. There should be no spaces in the path to this directory. After creating the directory, download the OpenWRT source codes into it.

Mkdir ~/OpenWRT cd OpenWRT

There are two options here - either take the stable version, which is well tested and reliable to a certain extent, or use the version for developers, which has many new features, but at the same time get a non-zero probability of firmware crashes out of the blue. The current release is called Backfire, and the test version is always in the trunk. Execute one of the commands below to download sources from the SVN repository and go to the appropriate folder.

Svn co svn://svn.openwrt.org/openwrt/trunk/ cd trunk

Svn co svn://svn.openwrt.org/openwrt/branches/backfire cd backfire

The feeds.conf.default file contains a list of repositories with additional software packages. If necessary, edit it. Uncomment (remove the # at the beginning of the line) items in the list or add third-party feed addresses for some specific software. At a minimum, we will need the standard set of packages and the LuCI web interface (you can choose another one or leave the default settings), so you don’t have to go into this file at all, leaving everything by default, and immediately update the package lists.

Nano ./feeds.conf.default ./scripts/feeds update -a

Let's get to the fun part - filling the firmware with components. First of all, for the test build, we will install the LuCI web interface, so as not to once again configure the router from the console. The remaining programs are searched by the same script with the search parameter and the keyword after it. The output will include package names with a brief description. To install, use the same utility with the install parameter. All dependencies will be automatically resolved and missing packages, if required by the user-selected component, will also be installed.

./scripts/feeds install -a -p luci ./scripts/feeds search keyword./scripts/feeds install package_name

For some programs, you may want or need to create configuration files. To do this, place them in the /files/etc/config directory, which must be created in the source folder. In general, any files from the /files directory will be included in the firmware. After this, you can proceed directly to the assembly. Let's run the following command in the console.

Make menuconfig

Here we go to the Target System (Enter) item and select the platform for which the firmware will be compiled. Find your model in the list of compatible devices and see what kind of hardware it has. To begin with, it is recommended to “practice on cats,” that is, make an x86 build with the required set of packages (note that this will not be possible for all packages), and then run it in a virtual machine. In the meantime, go to the main menu (Esc) and press Exit, not forgetting to save the new configuration. To get the basic settings for the firmware, run one more command.

Make defconfig make menuconfig

We return to the main menu and look for your router in Target Profile. Please note that in some models the hardware may differ noticeably from version to version. Try not to mix anything up, otherwise in the worst case you will get a “brick” instead of a device. If you decide to test the x86 assembly, then check the ramdisk and iso boxes in Target Images - the output will be a bootable iso image.



Now comes the most tedious part - manually selecting the necessary components. Previously installed packages must be included in the assembly itself, for which you will have to go through almost all sections of the settings. To install a component, navigate to it and press Spacebar to select it. * means that it will be “built into” the kernel, and M indicates the creation of a separate loadable module. In some cases, activating a component provides access to additional settings associated with it.


The Base system and Libraries sections contain basic programs and libraries. Network and IPv6 contain everything related to working on the network - Bittorrent and VPN clients, various servers, auxiliary utilities and much more.


In the LuCI section, the web interface parameters are configured, since it was chosen as an example for the x86 build. Firstly, it must be included in Collections, and in Modules select admin-core or admin-full. The Applications subsection contains modules for configuring certain router parameters. Additionally, you need to install localizations (Translations), support for network interface settings (Protocols) and basic libraries (Libraries).


The Kernel modules section mainly contains drivers for hardware, file systems, various subsystems, and so on. Here it is also useful to go through the subsections and enable, for example, NTFS support. Unfortunately, there is no universal guide to action when configuring firmware components. You can install additional utilities and libraries, but do not forget about the small storage capacity in the router. But you need to be careful when deleting something that was enabled by default. Check out at least a brief reference on the component by pressing H, or better yet, Google it.

After selecting all the required modules and components, you need to save the configuration. Just before building, it is useful to run another command that will show the missing utilities or libraries. You will need to install them using apt-get install or look for the packages that contain them and install them too.

Make prereq

The build process is started with a very simple command:

Building the firmware takes quite a long time. The more you “stuffed” into it, the longer you will have to wait. Owners of multi-core processors can make their plight a little easier by adding the -j parameter to the make command x, Where x=number of cores+1. On a test machine with an AMD Phenom II X6 CPU, the basic x86 firmware with minor additions was compiled into several threads in about 10-15 minutes. However, sometimes the use of several processor cores can lead to an error during assembly.

Make -j 7

The finished firmware files (trx, bin, iso and others) will be located in the bin folder. The process of uploading the firmware depends on the router model - look for information in the compatibility list and the wiki. For many models, two files are generated at once: factory and upgrade. The first one is needed to replace the standard software, and the second one is needed to update an already installed OpenWRT. If you have created a test x86 assembly, then its functionality can be checked in a virtual machine. VirtualBox is quite suitable for this. Create a new VM, select OS type Linux 2.6, do not create a HDD. As a bootable CD, use the openwrt-x86-generic-rootfs-iso.img image, whose img extension must be changed to iso. Switch the first network adapter to Internal Network mode, and the second to NAT or Bridge mode.

You can run some Live distribution inside the second VM by first switching the virtual network interface to the “Internal Network” mode. Ubuntu, for example, or one of the many WinPE variants. This machine should automatically obtain an IP address via DHCP, and you can connect to the pseudo-router via the web interface or telnet. If everything worked as it should, then you can start assembling the firmware for a specific router model.

Make clean svn update ./scripts/feeds update -a

In general, the devil, sorry, penguin, is not as scary as he is painted. Although, I must admit, for the average user this is not so simple. Unfortunately, as mentioned above, there is no universal and simple guide for assembling your own firmware. We tried to describe the most general steps, but it is simply impossible to cover all the nuances and details. Once again we urge our readers to be more careful during assembly and warn that incorrect actions can lead to the “death” of the device and sometimes difficult restoration of its functionality. Good luck!

Recently I needed to install my program written in C (C) on an access point running OpenWRT. I have already written how to build this program for Windows using GCC from Cygwin. To build OpenWRT and all supplied programs, the Buildroot build system and the ipkg package system are used. After much research on the OpenWrt Wiki and other sites, I was unable to find up-to-date documentation. As a result, after collecting information bit by bit, I finally assembled the ipkg package with my program. In order not to lose the acquired knowledge, I decided to write this article.

Obtaining source codes

I'm building OpenWrt from the trunk branch. You can get it either using git:

git clone git://nbd.name/openwrt.git

Or using svn:

svn co svn://svn.openwrt.org/openwrt/trunk/openwrt/

How to get other branches, including those with packages, is written.

Preparing to add your package

Go to the resulting directory



Did you like the article? Share with your friends!