OpenWrt RecoveryOS Tweak - overlayfs

Update: It’s come to my attention that Windows will not see a Fat partition on a card if the first partition is ext2. I have changed the instructions below to so that the fat partition is first on the card, thus allowing you to use it with Windows.

I've been playing around with the OpenWrt RecoveryOS a bit recently. I like it because it's small, fast and there's a good amount of packages available in Projectgus' repo. The problem with all these awesome packages and the RecoveryOS, though, is that there's not much space left in the Zipit's internal flash to store these goodies. 

Thankfully, OpenWrt has already implemented a new "filesystem" type called overlayfs. There have been "Union FS" options available on linux for some time but apparently none have been accepted by the mainline kernel developers. Union Filesystems allow you to combine two filesystems on top of one other as if they were a single filesystem. What this allows us to do is install software onto another storage device (ie micro/mini sd card) and pretend as if it's installed in the Zipit's internal flash. Now, how do we use this overlayfs? Well, it's actually as simple as using mount with some extra options. You can read the documention here but I will explain briefly how I am using it on the Zipit. 

First, let me explain the setup I'm using. My card has two partitions; partition 1 is Fat32 (any size you choose) and partition 2 is 256mb ext2. Partition 2 holds the linux software and can be larger if you wish. Partition 1 holds music, documents, etc. Next we need to setup OpenWrt to automount the card partitions. Add the following to /etc/config/fstab in the RecoveryOS:

config mount
        option target /mnt/mmcblk0p1
        option device /dev/mmcblk0p1
        option fstype vfat
        option options rw,sync
        option enabled 1
        option enabled_fsck 0

config mount
        option target /mnt/mmcblk0p2
        option device /dev/mmcblk0p2
        option fstype ext2
        option options rw,sync
        option enabled 1
        option enabled_fsck 1

This mounts the card partitions (if available) at boot time. OpenWrt will also automount cards if inserted after boot (but I have not been able to get it to automount the overlayfs yet). Now let's make some directories on our ext2 partition:

mkdir /mnt/mmcblk0p2/usr
mkdir /mnt/mmcblk0p2/usr/bin
mkdir /mnt/mmcblk0p2/usr/lib
mkdir /mnt/mmcblk0p2/usr/sbin

At this point we can mount the usr directory on the card as an overlay on /usr:

mount -t overlayfs /mnt/mmcblk0p2/usr -olowerdir=/mnt/mmcblk0p2/usr,upperdir=/usr /usr

If I create a file called /usr/mytest.txt, the file will be written to the "upperdir" and not to the "lowerdir", and in this case will be written to the jffs2 flash partition. If I want to create the same file on the card, I write to /mnt/mmcblk0p2/usr/mytest.txt. If a file exists in both places, the upperdir file takes precidence (if I read the documentation correctly :). 

But how do we install software to the card if opkg will be installing to /usr which is the jffs2 partition? Opkg can be told to install to another destination. Add the following to /etc/opkg.conf:

dest card /mnt/mmcblk0p2

If you want to install something to the internal flash you would use opkg as normal:

opkg install <packagename>

but if you want to install to the card you use the —dest switch:

opkg -d card <packagename>

To make the overlayfs mount at boot time, we need to add it to /etc/config/fstab like we did above with the card partitions:

config mount
        option target /usr
        option device /mnt/mmcblk0p2/usr
        option fstype overlayfs
        option options lowerdir=/mnt/mmcblk0p2/usr,upperdir=/usr
        option enabled 1
        option enabled_fsck 0

I've been using this setup for a while now with much success. You can use these instructions to overlay other directories (/bin, /sbin, etc) if you so choose. Good luck and have fun!

Archives By Month