Clean USB pivot_root for WGT634U and OpenWrt (Kamikaze).


The general idea is to create a root filesystem on a usb storage device / partition, via standard OpenWrt install. Then to flash the wgt634u with a squashfs based image that has the requisite kernel modules to recognize USB storage devices and their partitions and to mount a FAT or ext2 filesystem (other are possible) as the root filesystem (via pivot_root) in /etc/preinit. The candidate usb file systems are tested for some basic directories and files before a pivot_root is done from the squashfs (which becomes /rom) to /usb (which becomes, duh, /). No jffs filesystem is ever mounted. Under this paradigm, the on-board flash is never written to during normal device operation. I take /tmp out of RAM (I opt for RAM, not for fast /tmp). /etc/inittab is altered to run /etc/init.d/rcK which unmounts filesystems and prevents unclean restarts.


In the case where a USB storage device with a compatibale filesystem is not present at initialization, /etc/preinit will keep scanning /dev/scsi (or some specified directory) for a suitable host, bus, target, lun, part combination. When a mountable filesystem is found, the filesystem type is detected (for fat and ext2) and a filesystem check is run, before the target is mounted read & write. The kernel didn't panic when I removed one of my flash sticks (with the root fs). Although, the device did not recover when the stick was reinserted as it looks like when it was reinserted the filesystems on the stick got new device filenames.


This has been tested on: OpenWrt (Kamikaze) releases 3535.



svn co -r 3535 https://svn.openwrt.org/openwrt/trunk/

cd trunk/openwrt

make menuconfig


Target System (Broadcom BCM47xx/53xx [2.6])

OpenWrt Package Selection --->

Utilities --->

lsusb # very useful

FileSystems related --->

dosfstools # for mkdosfs

e2fsprogs # for e2fsck and mke2fs

util-linux --->

fdisk # very useful

swap-utils # for swap, untested

Kernel Configuration / Device Support --->

Filesystems support --->

kmod-fs-vfat # for vfat fs support

kmod-fs-ext2 # for ext2 fs support

# others? untested but useful!

Native Language support --->

kmod-nls-base # for fat and vfat


# select the correct one for you

USB support --->

kmod-usb-core # need usb host support




kmod-usb-storage # for mass storage devices








CFE> ifconfig eth0 -auto

et0: link down

Device eth0: hwaddr 00-00-C0-FF-EE-00, ipaddr, mask

gateway, nameserver, domain foo.bar.

*** command status = 0

CFE> flash -noheader flash0.os

Reading Done. 1806336 bytes read

Programming...done. 1806336 bytes written

*** command status = 0



usb 2-1: new full speed USB device using ohci_hcd and address 3

usb 2-1: configuration #1 chosen from 1 choice

scsi1 : SCSI emulation for USB Mass Storage devices

Vendor: Generic Model: USB Flash Drive Rev: %z!Y

Type: Direct-Access ANSI SCSI revision: 02

SCSI device sda: 64128 512-byte hdwr sectors (33 MB)

sda: Write Protect is off

sda: assuming drive cache: write through

SCSI device sda: 64128 512-byte hdwr sectors (33 MB)

sda: Write Protect is off

sda: assuming drive cache: write through

/dev/scsi/host1/bus0/target0/lun0: p1

sd 1:0:0:0: Attached scsi removable disk sda









mkdir -p /usb



mount $usb_root_device /usb




cd /usb

# directory stubs

mkdir proc

mkdir sys

mkdir dev

mkdir rom

# copy from from squashfs

mkdir bin

cp -a /rom/bin/* bin

mkdir etc

cp -a /rom/etc/* etc

mkdir usr

cp -a /rom/usr/* usr

mkdir lib

cp -a /rom/lib/* lib

mkdir sbin

cp -a /rom/sbin/* sbin

# copy from jffs

mkdir tmp

cp -a /tmp/* tmp

mkdir var

cp -a /var/* var

cp -a /etc/config etc

# do some final preparations on your new root filesystem

chmod oug+rwxt /tmp

# umount some filesystems that the kernel mounts or are required by preinit

# from /rom

cat << EOF >> etc/init.d/S99done


# umount old dev, proc and usbfs

umount /rom/proc/bus/usb

umount /rom/proc

umount /rom/dev






# umount dev, proc, sys and usbfs

usb_root_device=`mount | awk '{ if ($3 == "/") print $1 }'`


echo "unmounting memory based filesystems"

umount /sys

umount /dev/pts

umount /proc/bus/usb


echo "remounting root in read only mode"

mount $usb_root_device / -o remount -o ro


echo "halting system"




chmod +x etc/init.d/rcK



cat << EOF > inittab



tts/0::askfirst:/bin/ash --login

#tts/1::askfirst:/bin/ash --login




\rm /usb/bin/firstboot /usb/sbin/mount_root /usb/etc/preinit /usb/sbin/mtd /usb/sbin/jffs2root





cd trunk/openwrt/build_mipsel/root

# paranoia brick defense precautions

\rm bin/firstboot sbin/mount_root etc/preinit sbin/mtd sbin/jffs2root

# make mount point for usb

mkdir usb



# copy your own preinit to etc/preinit (assumes you have wget)

# in your build root directory (i.e. trunk/openwrt/build_mipsel/root), do

wget -O etc/preinit http://openwrt.pbwiki.com/f/WGT634U_USB_root_preinit

# don't forget to set it executable

chmod +x etc/preinit




cd trunk/openwrt

make target/install



tar -tvzf build_mipsel/linux-2.6-brcm/root.tar.gz | more




root@OpenWrt:/# df

Filesystem 1k-blocks Used Available Use% Mounted on

/dev/scsi/host0/bus0/target0/lun0/part1 31029 4665 24762 16% /


root@OpenWrt:/# mount

(null) on /rom type squashfs (ro)

/dev/scsi/host0/bus0/target0/lun0/part1 on / type ext2 (rw,nogrpid)

none on /proc type proc (rw)

none on /dev type devfs (rw)

none on /dev/pts type devpts (rw)

none on /proc/bus/usb type usbfs (rw)

none on /sys type sysfs (rw)