A better pivotroot for OpenWRT

Last time I introduced pivotroot scripts for OpenWRT. This time I’ll discuss my improvements.

The standard OpenWRT pivotroot is pretty basic. It needs to know where your USB disk will show up, and it assumes that the filesystem is in good shape. But I have OpenWRT systems with multiple USB ports, plus I’d like to be able to use USB hubs. And my OpenWRT systems tend to run until the power goes off – so I want to make sure the filesystem is clean before I boot off it.

In order to make things safer, I choose to run an ext3 filesystem. This is harder on the flash, because of the journal writes, but safer recovery for unclean shutdowns – which are the norm. A corrupted filesystem only gets worse, though, so I want to run fsck before I use the filesystem. (That means you’ll need to install e2fsprogs in the flash-based root filesystem.)

Since my USB sticks can move around, I search through all the available disk partitions to find one that looks like an OpenWRT system. That means I may try to mount filesystems and have them fail, but this is pretty safe. Eventually I’ll find the right filesystem, and if not I’ll boot off flash. I do assume that the filesystem will be on partition 1, and that it will be an ext3 filesystem.

I hate that the standard pivotroot uses the /mnt mountpoint. I use /flash instead. When I’m looking for an OpenWRT filesystem I look for /flash (and also /sbin/init). (So I need to mkdir /flash on both the flash-based and USB root filesystems.)

Finally, I want to know what’s going on – especially with fsck – and a way to turn off the pivotroot easily. I put logs in /tmp/pivotroot.log, I make the power LED flash while I’m fsck’ing and searching for a root fs, and I have rcS check whether the pivotroot script is executable. A quick chmod -x /flash/etc/init.d/pivotroot, and I can boot off flash.

Enough yakking, here’s the code. /etc/init.d/pivotroot, first, but there’s more below:

#!/bin/sh
# From http://oldwiki.openwrt.org/UsbStorageHowto.html - DHK 7/14/09
#
# Greatly enhanced to select among (maybe) several USB Mass Storage devices
# Assumes that the root fs will be on an ext3 filesystem in partition 1,
# and looks for /flash and /jffs in the root directory. Also assumes that
# mounting and checking each candidate device is harmless. Runs e2fsck
# over partitions before mounting them; note that this causes spurious
# date-related complaints every time, since time has not been synchronized
# yet at the time pivotroot is run.
#
# Output from this script is stashed at /tmp/pivotroot.log.
#
# DHK 7/23/2009

# Set Power LED flashing while we look for OpenWRT root
if [ -f /etc/diag.sh ]; then
  LED=1
  . /etc/diag.sh
  POW=`cat /proc/diag/led/power`
  set_led power f
fi

# install needed modules for usb and the ext3 filesystem
# We could defer loading the filesystem modules until we know there's a useful partition.
# **NOTE** for usb2.0 replace "uhci" with "ehci_hcd"
# **NOTE** for ohci chipsets replace "uhci" with "usb-ohci"
# **NOTE** for WL-500gP usb-uhci not usb-ohci
echo -n "pivotroot loading kernel modules: "
for module in usbcore usb-uhci scsi_mod sd_mod usb-storage ext2 jbd ext3 ; do {
  echo -n "$module "
  insmod $module
}; done
echo
# this may need to be higher if your disk is slow to initialize
sleep 4s

PART1=`find /dev/scsi -name part1`

if [ -z $PART1 ]; then
  echo No partitions, skipping pivotroot
else
  # Look for a mountable USB stick with, maybe, OpenWRT on it
  for dev in `find /dev/scsi -name part1`; do
    echo pivotroot checking $dev
    e2fsck -p $dev
    fsck=$?
    echo fsck status is $fsck
    if [ $fsck -eq 2 ]; then
      echo Corrected errors on $dev, need to reboot
      sleep 5
      reboot
    elif [ $fsck -eq 1 ]; then
      echo Corrected errors on $dev
    elif [ $fsck -gt 2 ]; then
      echo No usable filesystem on $dev
      continue
    fi
    echo Mounting $dev
    mount -t ext3 $dev /flash
    mt=$?
    if [ $mt -eq 0 ]; then
      if [ -x /flash/sbin/init -a -d /flash/jffs -a -d /flash/flash ]; then
        echo Found OpenWRT root on $dev
        # Side-effect - leave /flash mounted
        break;
      else
        echo "Missing /sbin/init, /jffs, or /flash (mount status was $mt)"
        [ -x /flash/sbin/init ] || echo Failed -x /sbin/init
        [ -d /flash/flash ]     || echo Failed -x /flash
        [ -d /flash/jffs ]      || echo Failed -x /jffs
        umount $dev
      fi
    else
      echo mount status is $mt
    fi
  done

  # if everything looks ok, do the pivot root
  [ -x /flash/sbin/init ] && {
          mount -o move /proc /flash/proc && \
          pivot_root /flash /flash/flash && {
                  mount -o move /flash/dev /dev
                  mount -o move /flash/tmp /tmp
                  mount -o move /flash/jffs2 /jffs2 2>&-
                  mount -o move /flash/sys /sys 2>&-
          }
  }
fi

# Restore Power LED to previous state
[ $LED -eq 1 ] && set_led power $POW

Addition to /etc/init.d/rcS (put this before the line LOGGER=”cat”):

# Improved from http://oldwiki.openwrt.org/UsbStorageHowto.html
# Switch the root filesystem to USB, if present
# DHK 7/14/09
if [ $2 == "boot" -a -x /etc/init.d/pivotroot ] ; then
        /etc/init.d/pivotroot > /tmp/pivotroot.log
fi

Finally, I add this line to the bottom of /etc/banner, but on the flash filesystem only. When I ssh in to an OpenWRT box I’ll know immediately how it booted:

Booted off internal flash

Remember, this works for me, but I make no guarantees it will work for you. Make sure you’ve saved your configuration before fooling with this stuff, and that you know how to reflash your router if you brick it.

Posted in OpenWRT | 18 Comments

Introducing pivotroot for OpenWRT

I use OpenWRT for my wireless access point, and for some other projects. I often set up an OpenWRT box running off an external USB stick, instead of its internal flash. This gives me more storage space – for software packages but also pictures when I’m hosting a web site, etc. Another advantage of an external USB stick is that it’s cheap to replace when you burn out the flash, although I’ve never actually had this happen to me.

So how do you boot OpenWRT off an external USB stick? Well, midway through the boot process you change the root filesystem from the one on the internal flash to the one on the external stick. That means you need to check first that there’s a filesystem to boot off of. There’s a standard procedure on the OpenWRT wiki, using what’s called a pivotroot script.

In White Russian (the older OpenWRT release) the procedure was to replace /sbin/init with pivotroot. This is scary, because if you step wrong you’ve bricked your router – made it unbootable. In Kamikaze (the newer release), the recommendation has changed, and instead pivotroot is explicitly called from rcS, a very early bootup script. This is a better idea, and when I was done with my changes it was even safer.

What does a pivotroot script need to do? It needs to:

  1. Make sure the kernel can recognize your USB stick, by loading kernel modules to support USB and USB storage devices
  2. Make sure the kernel can recognize the filesystem on your USB stick, by loading more kernel modules. For example, I use the ext3 filesystem on my USB stick, and that’s not built in to the OpenWRT kernels
  3. Check whether your USB stick is even present
  4. Mount the filesystem on your USB stick
  5. Finally, if everything else is OK, it remounts all the filesystems so that your USB stick is the root filesystem and the flash-based root filesystems are mounted elsewhere

Note that this procedure still uses the flash-based root filesystem to get partway booted. Any special software packages you need to boot up – all those kernel modules, for example – and the pivotroot script itself need to be on the flash-based root filesystem, and not the root filesystem you see once you’re booted. This also makes debugging a pivotroot script tricky, because by the time OpenWRT is booted the environment is very different from the one pivotroot sees.

Next time I’ll outline the changes I made to improve the standard OpenWRT pivotroot.

Posted in OpenWRT | Leave a comment

What I wish my N810 had

There are some things that would make my Nokia N810 better or easier to use (for my purposes).

  1. Line-out. Apple got this right, there’s line-out on the iPods (and even then you need to buy the right adapter). I plug the N810 into an FM modulator wired inline into my car’s antenna; in order to get the audio loud enough, I have to turn the volume on the N810 and on the car radio way up. There’s an audible background noise from all this, and I need to remember to turn it down before I switch back to headphones (N810) or an FM station (radio).

    I could use a USB audio widget, plugged through a converter to get down to the N810′s mini connector. But the USB connector on the N810 is only accessible if the easel-legs are open, which isn’t exactly car-friendly. Plus I might have to fool around with kernel modules. I haven’t tried any of this yet.

  2. Working GPS. ‘nuf said.
  3. gphoto. When I’m on vacation I’d like to be able to pull pictures off my camera, and view them on the N810. I could also push them to my home server next time I found a network (WiFi or 3G, using my phone). To do this I need gphoto, or something like it. This is probably a simple matter of setting up the cross-dev environment and porting it, I haven’t gotten around to trying.
  4. fsck in the boot menu. I should ask fanoush, or hack it together myself.
  5. Automated podcast downloads. It would be nice if gpodder would do this for me, instead of having to be manually triggered. It’s on their roadmap.
  6. Polishing: I wish the keyboard were a little better, that it had a Tab key, that I didn’t need to use the Fn key to get to “/” (come on, if I’m typing it’s likely to be URLs or filenames!), that the directional-button-thing were on the front of the tablet and not buried on the keyboard. None of these are killers.

To reiterate, I’m happy and I use it alot. In fact the screen protector has gotten scarred up and I may need to replace it – I’ve never had to do that before. This is just the “room for improvement” category.

Posted in N810 | Leave a comment

How to Set Up a Nokia N810

Herewith my hard-won knowledge on setting up a Nokia N810. You want to boot off the removable memory card – partly because it’s replaceable (when the flash memory wears out), and partly because it’s removeable (for backups, and when you need to reflash it).

  1. Learn how to reflash the tablet. I do this from my Linux box.
  2. Get root. My favorite way to do this is by installing OpenSSH, which sets the root password as part of its install process. Then you can open an X Terminal and ssh root@localhost.
  3. Prepare the removeable card. Mine is an 8GB SDHC micro card (with the mini adapter), formatted as 1GB for the bootable ext2 partition, and the rest as VFAT for N810 data. This is also a good time to practice with fsck on the two partitions, and to define your backup strategy (I use tar).
  4. Set up to boot from the removeable card. Download and follow instructions from fanoush’s site.
  5. Shutdown the tablet, and back up the removeable card.
  6. Put the card back in. boot up, and start installing and configuring software.

I use these apps daily:

  1. gpodder
  2. claws-mail
  3. mediabox
  4. maemo-mapper
  5. omweather on the desktop
  6. openssh

I also have iodine installed, but use it less often. I gave myself sudo access as well. Beware the published recipes for sudo access – believe it or not, the ones that were current 6 months not only didn’t worked, they soft-bricked the tablet so I would have to reflash, or at least reboot to the internal card (see why we did that?) and undo my changes.

Posted in N810 | Leave a comment

6 months with the Nokia N810 tablet

Six months ago, when I was between jobs, I bought myself a Nokia N810 tablet. I’ve had time to get used to it – it no longer “smells new” – and I still like it. It took me awhile to get the software setup just right, I’ll cover that in a separate post.

There has been an iPhone in the house for several months now, but I’m still happy with the N810. In fact, I use it several times a day. I use it for:

music and podcasts: this is the real winner, I’ve basically stopped using my iPod. While the iPod interface is easier to use, especially without looking at it, the WiFi connectivity of the tablet makes it much easier to get the content on. iPods are tethered to a Mac (for iTunes) or some other computer, the tablet isn’t.

I use gpodder for podcasts, and mediabox for my music. I used Canola2 for months, but although it’s prettier and integrates podcasts and music together, it’s slow, and doesn’t do a great job with the podcasts.

email and blog reading: it’s smaller than a laptop, but quite usable for reading and for short-to-medium replies. I couldn’t live without the slide-out keyboard. I use claws-mail with its RSS and HTML plugins.

GPS: the open source maemo-mapper app is fantastic, but there’s some sort of impedance mismatch with the N810 GPS, and it takes forever to get synced up. (The system GPS software seems to get a location long before maemo-mapper.) So this is the weakest app, and it doesn’t get much use.

Web surfing: The small screen starts to become an issue here, but for many web sites it’s OK. I use the builtin browser.

ssh: for the obvious reasons.

Connectivity: I already mentioned the WiFi connectivity. I also have it paired to my phone, so I can leach off the AT&T 3G data networking. I also have iodine installed, although it hasn’t been very successful when travelling. With the phone pairing, I don’t really need it.

Posted in N810 | Leave a comment