From charlesreid1

Widy.jpg

Building my own Widy the Hacker Gadget.

The Background

What we'll be doing is installing OpenWrt, an open-source Linux distribution that can be installed onto router firmware, onto a TP Link 3040 battery-powered router.

This router was selected becase it has a USB port, which will enable us to extend the filesystem of the OpenWrt device.

The Procedure

Our steps will be as follows:

Install OpenWrt on Router

The summary: Use the default TP-LINK interface at 192.168.0.1 to load and install OpenWRT firmware onto the router. Then use the OpenWRT interface at 192.168.1.1 to prepare to pivot the operating system's file system to a USB thumb drive.

Start Up the Router

Turn on the router. Connect your computer's ethernet port to the Widy device's ethernet port. Make sure the device is in 3G or WISP mode (where it expects clients to connect via ethernet), not in AP mode (where it expects to be able to connect to a modem via ethernet).

Navigate to the built-in router control page at 192.168.0.1. From here you can change the settings of the router, but you can also install firmware updates. This is how we'll install OpenWrt on our router.

Go to System and Firmware Update. We'll download an OpenWrt image and point the router to this image as the firmware update.

Download OpenWrt

You'll want version 2 firmware for the router, available here: https://wiki.openwrt.org/toh/tp-link/tl-mr3040#version_2x

  _______                     ________        __
 |       |.-----.-----.-----.|  |  |  |.----.|  |_
 |   -   ||  _  |  -__|     ||  |  |  ||   _||   _|
 |_______||   __|_____|__|__||________||__|  |____|
          |__| W I R E L E S S   F R E E D O M
 -----------------------------------------------------
 BARRIER BREAKER (14.07, r42625)
 -----------------------------------------------------
  * 1/2 oz Galliano         Pour all ingredients into
  * 4 oz cold Coffee        an irish coffee mug filled
  * 1 1/2 oz Dark Rum       with crushed ice. Stir.
  * 2 tsp. Creme de Cacao
 -----------------------------------------------------

Note that version 1 (v1) will not work, you must use version 2 (v2).

Firmware Update

Install the image by updating your firmware in the router control panel at 192.168.0.1

Go to System Tools > Firmware Upgrade.

Find the OpenWrt .bin image file you downloaded, and hit go.

IMPORTANT: You are flashing the hardware of the router. Anytime you flash the hardware, you have the potential of bricking the device. Make sure you're ready, you don't have a low battery, you don't accidentally close the browser, etc etc.

Wait for the progress bar to reach 100%. Eventually the router will turn off and turn back on, and your browser won't be able to reload the page. That's because your router is now running OpenWrt. Congratulations!!!

OpenWrt on the Router

Assuming OpenWRT is now on the router, and that you are connected to the router's ethernet port, navigate to 192.168.1.1 in the browser and you should see the OpenWrt web interface.The first thing you'll be asked to do is set up a password for the router. Once you do that, you'll have a web login to a tiny Linux box. You can control all sorts of stuff from the web interface - things you can control when you're running Linux. It's become a very souped-up router! You can also SSH to the router.

Extend OpenWrt Filesystem

The stock router has only 640 KB of free space, but we can stretch out by using a 64 GB jump drive to store the file system.

However, to get OpenWrt to fit into 640 KB of space, no software is included that tells OpenWrt how to use a USB drive, so we'll have to install some modules.

But before we can install modules, we have to connect OpenWrt to the internet.

Connect OpenWrt to the Internet

You can turn this tiny router from an access point (Master mode) into a wireless client (Client mode), a really cool feature that makes this doubly valuable as a gadget.

Go to Wifi > Network.

Turn on scan, and wait for it to present a list of wifi networks.

Connect to your desired wifi network.

Boom. If you ssh into the box, you should be able to reach the outside world:

$ ifconfig
$ ping microsoft.com

Install USB Kernel Modules

Let's install some modules, so OpenWrt knows how to talk to an external mounted drive.

We'll use opkg, the package manager for OpenWrt.

Start by updating the list of software available:

$ opkg update

Then take a look at all the installed software:

$ opkg list-installed

Now we'll need to install some kernel modules:

$ opkg install kmod-usb-storage kmod-fs-ext4 block-mount kmod-scsi-core

Let's break that down:

kmod-scsi-core installs kernel modules required for communicating with external USB devices.

kmod-usb-storage installs kernel modules required to use USB storage devices. This is a hardware thing.

kmod-fs-ext4 installs kernel modules required to use ext4 filesystems. This is a software thing.

block-mount installs kernel modules required to mount block devices, like file systems. This is a hardware+software thing.

Got it? Good.

The space on the device is limited, so it's gonna be tight, but I had no problem with the above packages. If you do get into a jam, you may be able to uninstall some packages with opkg remove. There are other options, too. One less than optimal solution is to use a USB device mounted on the router over a network connection (like plugging a thumb drive into a desktop, and being able to use it from the router).

Hopefully everything works out. Remember, first things first! Don't install too much stuff!

Verify Support for Device

Now you should be able to plug the USB device into the router, and use the dmesg command to see that it was recognized as a USB storage device. You should see the manufacturer and size show up, if everything goes correctly.

Now you need to format the flash drive, but you'll need to do that somewhere else - there isn't enough space to install anything, and Gparted is a nice tool to use on the desktop anyway.

Format USB Thumb Drive

Now format the USB thumb drive for onboard use with the Widy gadget.

Open gparted on a Linux machine, plug the USB drive into that Linux machine, and create a new partition table of type msdos. Then create a new partition of type ext3 that is a primary partition the size of the jump drive.

EXTREMELY IMPORTANT: Follow these instructions down to the word.

Plug the USB drive into any Linux computer, and use parted or gparted to wipe it.

Create a new partition table of type "msdos".

Create a new partition of type ext3.

If the partition is around 3 GB or less, everything is ok.

Mount USB Thumb Drive on Router

Plug your thumb drive into your router. SSH into the router. Now look for activity with the thumb drive on the USB port by running the dmesg command:

$ dmesg
[ 1066.400000] usb 1-1: new high-speed USB device number 3 using ehci-platform
[ 1066.560000] usb-storage 1-1:1.0: USB Mass Storage device detected
[ 1066.560000] scsi1 : usb-storage 1-1:1.0
[ 1067.600000] scsi 1:0:0:0: Direct-Access     Kingston DataTraveler G3  PMAP PQ: 0 ANSI: 0 CCS
[ 1069.170000] sd 1:0:0:0: [sda] 7827456 512-byte logical blocks: (4.00 GB/3.73 GiB)
[ 1069.180000] sd 1:0:0:0: [sda] Write Protect is off
[ 1069.180000] sd 1:0:0:0: [sda] Mode Sense: 23 00 00 00
[ 1069.180000] sd 1:0:0:0: [sda] No Caching mode page found
[ 1069.190000] sd 1:0:0:0: [sda] Assuming drive cache: write through
[ 1069.200000] sd 1:0:0:0: [sda] No Caching mode page found
[ 1069.200000] sd 1:0:0:0: [sda] Assuming drive cache: write through
[ 1069.230000]  sda: sda1
[ 1069.240000] sd 1:0:0:0: [sda] No Caching mode page found
[ 1069.240000] sd 1:0:0:0: [sda] Assuming drive cache: write through
[ 1069.250000] sd 1:0:0:0: [sda] Attached SCSI removable disk

Woo hoo!

Now mount it:

$ mkdir usb/
$ mount /dev/sda1 usb/
$ 

Woo hoo!

Pivot to USB Drive

This will pivot by copying the entire drive to the router, and updating the fstab table on that jump drive so that the Widy will boot from it.

Now that we've successfully gotten a USB drive mounted on the router, we can pivot the entire filesystem onto the USB drive.

First, we'll copy everything in the root filesystem / to the USB drive.

Copy Everything to USB Drive

$ mkdir -p usb
$ mount /dev/sda1 usb/

$ mkdir -p rootcopy/
$ mount --bind / rootcopy/

Now connect a tar compression stream (cvf) with a tar expansion stream (xf) to copy everything onto the USB drive:

$ tar -C rootcopy/ -cvf - . | tar -C usb/ -xf -

Now you can unmount the filesystems:

umount rootcopy/
umount usb/

Edit Fstab

Now we can edit the fstab file to tell the operating system which partitions to mount where.

NOTE: You should edit the fstab config file on the jump drive, or edit it before copying the contents of the root file system onto the jump drive.

NOTE: Do not edit /etc/fstab, as this file is automatically generated and is overwritten at boot time. Instead, edit /etc/config/fstab.

Change the fstab configuration entry for /dev/sda1 so that it mounts as the root filesystem, and turn on the boot flag by adding some stuff to the fstab config file.

Need to add the "config mount" block. The "config global" block was already there.

config 'global'
        option  anon_swap       '0'
        option  anon_mount      '0'
        option  auto_swap       '1'
        option  auto_mount      '1'
        option  delay_root      '5'
        option  check_fs        '0'

config mount
        option target /    
        option device /dev/sda1
        option fstype ext3
        option options rw,sync
        option enabled 1
        option enabled_fsck 0
 

Note: pre-existing configuration file contained:

config 'global'
        option  anon_swap       '0'
        option  anon_mount      '0'
        option  auto_swap       '1'
        option  auto_mount      '1'
        option  delay_root      '5'
        option  check_fs        '0'

config 'mount'
        option  target  '/mnt/sda1'
        option  uuid    'e34ae397-030d-47bb-a3c4-08d030c10baf'
        option  enabled '0'

Reboot

Now reboot the device.

Confirm You Have A Metric Ton Of Storage Space

Once you log in, you can check the size of all your partitions, and that should show you how big your / root filesystem is:

root@OpenWrt:~# df -h
Filesystem                Size      Used Available Use% Mounted on
rootfs                   58.6G     61.3M     55.5G   0% /
/dev/root                 2.3M      2.3M         0 100% /rom
tmpfs                    14.1M     64.0K     14.0M   0% /tmp
/dev/sda1                58.6G     61.3M     55.5G   0% /
tmpfs                   512.0K         0    512.0K   0% /dev

Baziiiiiiinggg!!! Give yourself a raise. You just upgraded from a dozen MB to 3.5 GBs.

A Note on Disk Encryption

Encrypting the USB drive shouldn't be too hard:

http://wiki.openwrt.org/doc/howto/disk.encryption

Errors

Failed to insert mod .ko

2018 revisiting instructions: am now seeing several of the packages fail with this "failed to insert X.ko" message.

Start with an update:

root@dropbear:~# opkg update

kmod-usb-storage package fails due to kmod-scsi-core failing:

root@dropbear:~# opkg install kmod-usb-storage
Installing kmod-usb-storage (3.10.49-1) to root...
Downloading http://.../kmod-usb-storage_3.10.49-1_ar71xx.ipk.
Installing kmod-scsi-core (3.10.49-1) to root...
Downloading http://../kmod-scsi-core_3.10.49-1_ar71xx.ipk.
Configuring kmod-scsi-core.
kmod: failed to insert /lib/modules/3.10.49/sd_mod.ko
Configuring kmod-usb-storage.

then kmod-fs-ext4 fails:

root@dropbear:~# opkg install kmod-fs-ext4
Installing kmod-fs-ext4 (3.10.49-1) to root...
Downloading http://../kmod-fs-ext4_3.10.49-1_ar71xx.ipk.
Installing kmod-lib-crc16 (3.10.49-1) to root...
Downloading http://../kmod-lib-crc16_3.10.49-1_ar71xx.ipk.
Installing kmod-crypto-hash (3.10.49-1) to root...
Downloading http://../kmod-crypto-hash_3.10.49-1_ar71xx.ipk.
Configuring kmod-crypto-hash.
Configuring kmod-lib-crc16.
Configuring kmod-fs-ext4.
kmod: failed to insert /lib/modules/3.10.49/ext4.ko

Also see this thread on the openwrt forums: https://forum.openwrt.org/viewtopic.php?id=56640

The problem appears to be with mis-matching kernel versions. I have no idea how this got messed up, as it formerly worked fine.

Flags