Getting started with OpenWrt

Frederic Cambus January 10, 2014 [Networking]

My last encounter with embedded Linux distributions dates back to August 2005, leveraging Familiar Linux on an iPaq in order to port an Apache + PHP + MySQL application to a thttpd + PHP (using CGI) one to be usable directly on what was a widely available PDA at the time.

Clearly, it had been a while and I'm now doing some experiments with OpenWrt (an embedded Linux distribution targeted at residential gateways and routers) on a TP-LINK TL-MR3020 pocket router. The main reason behind this is that I've been focusing a lot on DNS during the last few years and have been wanting to do DNSSEC validation at the router level, which I will document in another article.

OpenWrt 12.09 MOTD

The TL-MR3020 embeds an Atheros AR7240 CPU (MIPS architecture) running at 400MHz, 32MiB of RAM, 4MiB of Flash, a 100 MBit Ethernet port, and most importantly an USB 2.0 port allowing to plug a flash drive and extend storage space.

Here is the result of running cat /proc/cpuinfo on this device:

root@OpenWrt:~# cat /proc/cpuinfo

system type             : Atheros AR9330 rev 1
machine                 : TP-LINK TL-MR3020
processor               : 0
cpu model               : MIPS 24Kc V7.4
BogoMIPS                : 265.42
wait instruction        : yes
microsecond timers      : yes
tlb_entries             : 16
extra interrupt vector  : yes
hardware watchpoint     : yes, count: 4, address/irw mask: [0x0000, 0x0008, 0x0008, 0x0730]
ASEs implemented        : mips16
shadow register sets    : 1
kscratch registers      : 0
core                    : 0
VCED exceptions         : not available
VCEI exceptions         : not available

LuCI web interface

One of the best part of using OpenWrt instead of stock firmware is the gorgeous interface, which is uncluttered and responsive.

Here are some captures:

OpenWrt - Overview

OpenWrt - Realtime Traffic

Installing packages

OpenWrt uses the opkg package manager (originally forked from ipkg) which makes it really easy to install software packages.

opkg must have one sub-command argument
usage: opkg [options...] sub-command [arguments...]
where sub-command is one of:

Package Manipulation:
    update                  Update list of available packages
    upgrade <pkgs>          Upgrade packages
    install <pkgs>          Install package(s)
    configure <pkgs>        Configure unpacked package(s)
    remove <pkgs|regexp>    Remove package(s)
    flag <flag> <pkgs>      Flag package(s)
    <flag>=hold|noprune|user|ok|installed|unpacked (one per invocation)

Using an USB flash drive to extend storage space

First, we need to install required packages and kernel modules in order to be able to use and mount USB external storage devices.

For ext4 support:

opkg install block-mount kmod-usb-storage kmod-fs-ext4

Then we can configure OpenWrt to use a root filesystem on external storage (extroot) by using one of the following methods: external root (pivot root), or external overlay (pivot overlay).

Swap

This device has 32Mb of RAM which can easily be exhausted, so we are going to set the swap space to double that amount:

dd if=/dev/zero of=/swapfile count=65536 bs=1k
mkswap /swapfile
swapon /swapfile

We can check that swap is enabled using the free command:

             total         used         free       shared      buffers
Mem:         29212        23548         5664            0         2032
-/+ buffers:              21516         7696
Swap:        65532            0        65532

To activate the swap file at boot time, we need to add the following directives in /etc/config/fstab:

config swap
        option device	/swapfile

And enable fstab autostart at boot time:

/etc/init.d/fstab enable

Things to do on OpenWrt

IRCing from the console is something I have fond memories of, and I vividly remember using IrcII and especially BitchX (which came packed with some really nice ASCii and ANSi art logos). Nowadays I still use IRC from time to time to get my dose of nostalgia and I'm using irsii for this purpose.

Irssi on OpenWrt

There are some other IRC related packages available such as psyBNC (a permanent IRC-Bouncer) and bitlbee (an IRC instant messaging gateway).

Download service

OpenWrt comes with several packages for downloading torrents directly from command line: ctorrent (which for some reasons keeps crashing my router), rtorrent, or transmission-cli.

rTorrent on OpenWrt

Media server

Thanks to MiniDLNA, it is possible to stream audio, pictures and video content to DLNA/UPnP AV compatible devices such as Smart TVs.

Here is a minimal configuration file: /etc/minidlna.conf

port=8200
network_interface=br-lan

friendly_name=OpenWrt

db_dir=/mnt/sda1/minidlna/db

media_dir=A,/mnt/sda1/audio
media_dir=P,/mnt/sda1/pictures
media_dir=V,/mnt/sda1/videos

Serving static content

Nginx, my favorite web server, is of course available as a package on OpenWrt and can be leveraged to serve static content. As the LuCI web interface is running on port 80, we need to change nginx configuration in order to use a different port, for example 8080.

Here is a minimal configuration file: /etc/nginx/nginx.conf

user nobody nogroup;
worker_processes 1;

events {
    worker_connections 1024;
}

http {
    include mime.types;

    server {
        listen 8080;
        server_name localhost;

        location / {
            root /etc/nginx/html;
            index index.html;
        }
    }
}

Networking stuff

Being a router targeted Linux distribution, there are of course lots of interesting network related packages available such as DNS servers, IPv6 tunnels, VPN software, and so far and so on. I will post some detailed articles about using OpenWrt for such purposes in the following weeks, so stay tuned!

Back to top