Chroot instance of OpenWrt on external USB storage

Why, you ask?

Due to flash storage constraints, it may be desirable to install packages on USB storage. However, as noted in extroot#alternatives.to.extroot, "installing OPKG packages in root is much more convenient, as its files are installed in locations that the rest of the OS expects them to reside."

Installing a chroot instance of OpenWRT allows one to conveniently install storage-greedy packages, such as LAMP stack, xmail MTA, etc., on an external USB hard drive!

Steps explained below:

  1. copy a vanilla OpenWrt system into a chroot directory
  2. bind mount the host system's live dev and proc directories
  3. add init.d scripts to start/stop chrooted services

Install OpenWrt Chroot FS

Download the appropriate openwrt-<arch>-rootfs.tar.gz and extract the root file system to the desired chroot directory.

wget http://downloads.openwrt.org/backfire/10.03.1/ar71xx/openwrt-ar71xx-rootfs.tar.gz
tar -xvzf openwrt-ar71xx-rootfs.tar.gz -C /mnt/openwrt/

Mount /dev and /proc binds and copying /etc files

To allow access to host resources from the chroot environment, /dev and /proc directories must be bound.

mount -o bind /dev /mnt/openwrt/dev 
mount -t proc none /mnt/openwrt/proc

And to allow for dns resolution:

cp /etc/resolv.conf /mnt/openwrt/etc/

The above should be added to an init script including appropriate START/STOP order values (start after USB storage mount…).

Install chroot

opkg install coreutils-chroot

Adding chrooted services

Install desired packages in your new chroot OpenWrt instance then copy init scripts to the hosts file system.

…on chroot:

chroot /mnt/openwrt
opkg install lighttpd

…from host root:

cp /mnt/openwrt/etc/init.d/lighttpd /etc/init.d/lighttpd_chroot

Modify the chroot init scripts to include /usr/sbin/chroot and path to the chroot executable, making certain that START order value is after the above-mentioned chroot init script.

#!/bin/sh /etc/rc.common
# Copyright (C) 2006 OpenWrt.org
START=53

CHROOT=/mnt/openwrt
BIN=lighttpd
DEFAULT=$CHROOT/etc/default/$BIN
LOG_D=$CHROOT/var/log/$BIN
RUN_D=$CHROOT/var/run
PID_F=$RUN_D/$BIN.pid

start() {
	[ -f $DEFAULT ] && . $DEFAULT
	mkdir -p $LOG_D
	mkdir -p $RUN_D
	chroot $CHROOT $BIN $OPTIONS
}

stop() {
	[ -f $PID_F ] && kill $(cat $PID_F)
}

Enable and enjoy!

Back to top

doc/howto/chroot.external.txt · Last modified: 2014/01/04 05:12 by sledmouth