Home Router as a Mini-Server

By

Bernhard Bablok

Many devices now run Linux – but mostly as closed source firmware that you cannot access. We show how to use OpenWrt to free the TL-MR3020 router from its proprietary firmware and convert it into an all-around server for your home network.

TP-Link manufactures routers in various performance classes. The TL-MR3020 (Figure 1) is marketed by the manufacturer as a mobile router because it is small and light. However, it does not support 3G (see the “Hardware” box), although you can upgrade via the USB port.

Figure 1: The TL-MR3020 TP-Link (left) is no larger than the palm of your hand. The mini-USB hub by Pearl (right) acts as an expander.

Thus, the router is aimed at people who do not need 3G or have a UMTS stick and therefore do not want to invest in an expensive mobile UMTS router. However, the “normal” use of the router is not the focus of this article; instead, I will describe how to convert it into a mini-server.

To do this, you’ll replace the existing firmware with OpenWrt, a special Linux distribution for microdevices. OpenWrt has a package management system that is in no way inferior to that of classical distributions; thus, only the limitations of the hardware can curb your creativity.

Prelude

The TL-MR3020 is well-suited to this purpose for two reasons: On one hand, it costs EUR 30 (US$ 40) – which is not too much, if you happen to “brick” the device. On the other hand, the manufacturer does not put any obstacles in the way of installing third-party firmware.

To begin, download the correct version of OpenWrt and follow the brief, but perfectly sufficient, instructions that come with the router to connect to the device’s web interface. When you get there, upload the new firmware to the TL-MR3020 by selecting Upgrade in the new web form (Figure 2).

Figure 2: OpenWrt is uploaded as a firmware update via the original web interface on the TL-MR3020.

Uploading the firmware is the only really critical step in the whole process. A defective firmware image will send the router off to the happy hunting grounds – your only chance is a soldering iron. Thus, before loading the image, it is essential to check out the postings on the OpenWrt wiki and follow the appropriate instructions.

After installing OpenWrt, the next step is the basic configuration, particularly for the network. Then, you need to set up a number of software packages that upgrade USB support for data media (USB sticks or hard disks). This step lets you boot the router from an external disk. On the now-extended root filesystem, you then add more software packages from the OpenWrt repository for the actual application.

First Contact

After the firmware upgrade and device reboot, you can connect your PC or laptop to the router with the use of a cable. Configure the network (you can normally use the network manager for this) and login via Telnet on 192.168.1.1 (Figure 3). The router runs a DHCP server that supplies a suitable address to your PC or laptop.

Figure 3: Done: The first login on OpenWrt.

At this point, you should set a password for root. OpenWrt then switches off the Telnet server for safety reasons and starts the SSH server. Next, adjust the network configuration so that the router is on your home network; then, you can move on and add more software. For details of the OpenWrt configuration system, see the “Configuring OpenWrt” box.

Figure 4: Using the web interface on OpenWrt to retrieve the system state.

To use the router as a server on your home network, the network configuration should look like Listing 1 (/etc/config/network) and Listing 2 (/etc/config/wireless).

In Listing 2, line 19 ensures that the server is integrated into the network as a WLAN client rather than as an access point or router.

After a reboot, the device should be found at the configured network address. If not, emergency mode can help: During the boot process, press the WPS button once it starts flashing. If the LED light flashes quickly, then release it again. Now the device is in Failsafe mode with the default address 192.168.1.1 and running the Telnet daemon. You can connect to Telnet and make the necessary corrections. However, no DHCP server is available on the router in this mode. You must assign a matching IP address to your own computer manually.

More Software

The original firmware image does not contain all the components that are necessary for running the root filesystem on a USB stick, so you will need to add them now. After successfully completing the network configuration, the mini-router can access the Internet and use the convenient package management system to install additional software retroactively.

The pivotal point is the opkg command, which you need to run as root. To do so, first call opkg update to upgrade the package list. Then, install the specified packages using the following command:

# opkg install <package>

If you are looking for a particular package, the list subcommand outputs a package list with brief descriptions. For the USB installation, you primarily need a set of kernel packages (Listing 3).

It’s best to partition and format the USB stick (/dev/sdb in the listing – you might need to adjust this) on your PC. Besides the root partition, you’ll want a home partition and, above all, swap space.

Then, you can simply plug the stick into the router and change the configuration of the mountpoints in the /etc/config/fstab file. Lines 10-16 in Listing 4 are important.

The last line of Listing 3 copies the entire root filesystem to the stick. Creating a backup now is advisable. The system will still boot, even without the stick inserted: In this case, it simply ignores the nonexisting devices.

After a reboot with a stick attached, you finally have more disk space. Any packages you now add can be stored there. All told, having the root filesystem on a USB device is handy. You can modify the configuration with the usual editors on a PC and easily copy data between locations (e.g., for backups).

At this point, the basic configuration is complete except for a few things, such as the hostname. The remainder of the procedure depends on how you will be using the router, and you can use the package manager to retrieve and configure the required software. The procedure on OpenWrt is no different from that of any other distribution.

Buttons and LEDs

By way of the web interface or SSH, you can completely control the TL-MR3020 as a mini-server. However, it is easier to use the existing buttons. The original WPS button is suitable for triggering individual actions, and the slide switch on the side (with its three positions) does the switching between different operating conditions (more about this later).

The LEDs serve as a kind of output device. Three different states are possible: on, off, or flashing. In combination with the buttons, you can build some nice features. From a technical point of view, you need to control the LEDs on files below /sys/class/leds (e.g., for the WPS LED, this is the /sys/class/leds/tp-link:green:wps directory). The makers of OpenWrt hide the complexity, and you normally only need to edit the configuration in the /etc/config/system file (Listing 5, lines 13-35).

Typically, OpenWrt couples LED activity with the operating states of individual components, such as the network or USB interface. However, you can adjust the defaults to suit your needs.

Configuring the switch is also easy. You need to configure the event system kernel so that it triggers on the appropriate button events. To do this, edit the /etc/hotplug2.rules file and remove the caret before the ^Buttons string. Because this is an exception list, you enable events for buttons by removing the comment sign. From now on, the system runs all the scripts in the directory /etc/hotplug/button for each button event. The variables BUTTON and ACTION are set.

An example that demonstrates the interaction with the LEDs is shown in Listing 6.

This script is only for the WPS button (line 22), and it causes the WPS LED to flash for three seconds (lines 6-8 and 10) then go off for another three seconds. When you release the button, the script turns on the LED again (line 19).

Instead of storing each script individually in the Button directory and querying the status of the switch there, as in Listing 5, it makes sense to download a generic button script from the OpenWrt treasure trove. Then, you can configure the switch similar to the LEDs in /etc/config/system.

The WPS button is configured as an off switch here: If you hold down the button for at least three, and a maximum of six, seconds and then let go, the system shuts down (lines 37-42 in Listing 5).

This step also reveals the meaning of the program in Listing 6; the button LED flashes for three seconds and then switches off for three seconds – so you do not have to count to shut down the system. Likewise, you could also use the WPS button to start another operation with short clicks. Listing 5 (lines 44-49) configures a log entry for demonstration purposes.

The Slide Switch

The slide switch on the TL-MR3020 is slightly trickier for two reasons. First, the kernel does not see the slide switch as one button but as two; mapping the three possible states takes 2 bits. Each change of the switch thus triggers two events, which reach the processing script sequentially but must be evaluated together. Second, the event system only sees the changes.

Now you can try a little script that checks the status of the slide switch while booting, for example, to configure the network environment. This aspect, however, is only hinted at in the OpenWrt wiki documentation. Listing 7 shows you how.

It updates the Status button in /var/run. Additionally, it starts a processing script. The extra magic in lines 10-12 ensures that only one processing script starts. The wait time before processing (line 21) ensures that all relevant button events are also received and processed. Lines 16 and 17 retrieve the processing script from the configuration file in /etc/config/system. The processing script then starts at line 22.

It reads the status of the slide switch and reconfigures the network. The actual network files are only symbolic links; thus, the reconfiguration consists of pointing the symlinks at the new configuration files. For example, /etc/config/network.AP contains the network configuration when the slider is in the AP position. It makes sense to start the processing script once at startup time (in /etc/rc.local) after reading the slider status.

Conclusions

In this article, I’ve explained the hardware, so you “only” need to combine the individual components. For more detailed information about getting a UMTS stick to run, for example, or implementing specific network scenarios, please see the OpenWrt wiki.

The little TL-MR3020 router cannot cover every application (see the “More Application Examples” box). However, the mechanisms described here are at least similar for other devices, including those by other manufacturers. At the upper end of the power spectrum are the QNAP NAS devices that are supplied with OpenWrt in place. However, the range of preconfigured services makes customization more difficult.

Bernhard Bablok works at Allianz Managed Operations & Services SE as a SAP HR developer. When he’s not listening to music, riding his bike, or walking, he focuses on topics relating to Linux and object orientation. You can contact him at mail@bablokb.de.

Related content

Many devices now run Linux – but mostly as a closed source firmware that you cannot access. We show how to use OpenWrt to free the TL-MR3020 router from its proprietary firmware and convert it into an all-around server for your home network.

OpenWRT puts Linux on WLAN routers and helps users set up large-scale WLANs at home, and the FreeWRT derivative adds a professional touch. If you don’t have your own compile farm, the OpenSUSE build service may be just what you need. And we investigate the obstacles to new packages for Debian.