Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Build OpenWrt Image (Change Your Router to Linux PC)
#1
Recently I was tasked with reflashing TP-LINK WR841N with OpenWrt firmware. It is very minimal Linux PC with 650MHz single core SoC MIPS 24k 32-bit RISC CPU, 32MB RAM and 4MB built-in flash for under CAD $30 brand new and runs of 9V/0.6A power adapter. It apparently sports 433 BogoMIPS (Raspberry 3 has 77 per core). It has a serial port and if you really wanted to push it, you could use the LED GPIO for other purposes than intended.

Received V11 hardware, which is currently not officially supported (it just means there is no official image release), but if you look closely, it is exactly the same as V10 except FLASH chip model number. Even if there was a pre-built official firmware for this router version, I would not be able to use it because of very specific functionality that was required and not included in the prebuilt image and not enough space to install them afterward.

The task required these services: openvpn, snmpd, siproxd and user interface called LuCI. There is no way all these could fit on 4MB, but I ended up excluding hefty packages and including NFS client, then mounting space on local Raspberry Pi we already had there anyway, redirected /usr directory and installed additional packages including openvpn, snmpd, siproxd and even mc (Midnight Commander), Python and gcc. Also compiled Hello World program right on that little router. Mounted network disk transfer speed was benchmarked at 10-12MB/s - almost at theoretical maximum of FastEthernet.

But lets start from the beginning.

Build Environment

There is several ways to build OpenWrt. Rebuilding from sources takes long, also most common packages are already precompiled so we use Image Generator development toolkit.

But before that we need a build host system. If you have Linux machine, you good to go, I used VMWare Player with small Virtual Machine with 1G RAM 8G HDD running Ubuntu 16.04 LTS 64-bit Server. Installing Ubuntu is not part of this article, so I assume you already have functional build host machine.

Now let us go to the good old command line:

Code:
cd ~
mkdir openwrt && cd openwrt
wget https://downloads.openwrt.org/chaos_calmer/15.05.1/ar71xx/generic/OpenWrt-ImageBuilder-15.05.1-ar71xx-generic.Linux-x86_64.tar.bz2
tar -xvjf OpenWrt-ImageBuilder-15.05.1-ar71xx-generic.Linux-x86_64.tar.bz2
export BuildRoot=OpenWrt-ImageBuilder-15.05.1-ar71xx-generic.Linux-x86_64
cd $BuildRoot

Make sure you add the export BuildRoot line from above to your to ~/.bash_profile or ~/.profile or /etc/profile so it survives the reboot.

If you do not have make command installed, do it now:

Code:
OpenWrt-ImageBuilder-15.05.1-ar71xx-generic.Linux-x86_64
sudo apt-get install make

Because WR841N V11 is not supported and even though V10 is compatible, the bootloader would reject V10 image due to incorrect HW ID, we need to configure toolkit to build the proper V11 image.

  1. In directory $BuildRoot/build_dir/target-mips_34kc_uClibc-0.9.33.2/linux-ar71xx_generic copy tl-wr841n-v10-kernel.bin to tl-wr841n-v11-kernel.bin

  2. In $BuildRoot/target/linux/ar71xx/image/Makefile
    1. copy section "define Device/tl-wr841n-v10 ... endef" into tl-wr841n-v11
    2. make sure the HW ID is correct for your selected model (0x08410011 for V11)
    3. add tl-wr841n-v11 to TARGET_DEVICES += 
So the Makefile will have following items for V11, notice the added section between 1st and 2nd ... then added tl-wr841n-v11 in the list of TARGET_DEVICES

Code:
define Device/tl-wr841n-v10
...
define Device/tl-wr841n-v11
   $(Device/tplink-4mlzma)
   BOARDNAME := TL-WR841N-v9
   DEVICE_PROFILE := TLWR841
   TPLINK_HWID := 0x08410011
endef
...
define Device/tl-wr842n-v2
...
TARGET_DEVICES += tl-wr841n-v8 tl-wr841n-v9 tl-wr841n-v10 tl-wr841n-v11 tl-wr842n-v2 tl-wr843nd-v1 tl-wr847n-v8

Building OpenWrt

At this point we are ready to rebuild WR841N V11 image. In this example I only include nano editor and LuCI:

Code:
cd $BuildRoot
make image PROFILE=TLWR841 PACKAGES="nano luci" FILES=files/

Everything you specify in the PACKAGES= clause will get included in the image. If you specify too much, the build will fail. Check your build output:

Code:
...
Exportable Squashfs 4.0 filesystem, xz compressed, data block size 262144
       compressed data, compressed metadata, compressed fragments, no xattrs
       duplicates are removed
Filesystem size 2269.95 Kbytes (2.22 Mbytes)
       34.05% of uncompressed filesystem size (6667.14 Kbytes)
...

The Squashfs filesystem can go up to approx 2600 to be able to fit, so we do not have much space left even though we did not even include much of functionality.

After rebuild, resulting images are in $BuildRoot/bin/ar71xx

Uploading Image to Router

You can upload image via web interface, serial port or my favorite on this router - via TFTP.

TBD - will update

Image Layout

You can skip this section, it only contains some details about generated image.

The generated image is 3840kB, i.e. 256kB out of 4MB flash space is reserved for bootloader and router's BIOS. Compressed kernel is 1147kB and /rom is very tightly (LZMA/7-zip) compressed pre-built packages in read-only filesystem called SquashFS and /overlay part is writable compressed filesystem with journalling and wear leveling. The two are combined into one using OverlayFS. Any changes on the filesystem happen on the /overlay so when you boot, you will find that writing config files in /etc directory will be mirrored in /overlay/upper/etc.

In the example above where only nano and Luci were included the kernel is 1.12MB (~1148kB) and /rom portion is ~2270kB so the remainder for /overlay is 422kB.

Actual NFS Client Configuration

The goal was to build minimal functional router that performs the connectivity function and adds additional services from external storage. Should external storage break or disappear, the router must continue to provide connectivity, but may stop services like snmp, telephony, user interface, etc.

TBD - will update
Reply
#2
Hi Roman,
Very impressive ; way over my head though.
Bob D
Reply
#3
Bob,

OpenWrt is Linux based solution to reprogram many common routers. It is a toolkit to easily build your own brand of networking device. There is other similar systems: DD-WRT and Tomato. I did not select OpenWrt, it was selected for me, so I am learning it.

Btw. OpenWrt is based on BusyBox, the kernel is usually around 1.2MB depending on the target CPU. BusyBox has been around since 1999, so it's a time and battle hardened veteran.

Bob, you mentioned you would like to start looking at Linux did you get a chance?

Roman
Reply
#4
Hi Roman,
I am working on a Kodi ( OpenElec) media center on a raspberry Pi.
This is my 1st attempt with the Pi, so even writing an image to an SD card was a challenge.
The project was in this month's Nuts and Volts magazine, and looks simple enough Wink
I still have to get the Kore app working as the built in wireless for the pi is not working thus far.
Once i get this functional ( mainly for access to my audio library) I will start looking for a CRON based project.
The trouble is I will be starting a kitchen remodel soon and i am sure the wife will put that ahead of my electronics.
Oh well, as long as i can find some time to play, I eventually will learn more about using Linux
Bob D
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)