Persisting /var to a usb drive

Hi there,

like many people who've posted in the past (0, 1, 2, tens of others I've gone through that I don't care to dredge up from my browser history), I'd like to be able to:

  • have a persistent /var/, since the majority of important data produced by the router resides here (!!!)
  • store the data in /var/ on an external usb drive
  • not have to build my own openwrt images

What is currently the best way to do this? I've tried doing it by creating symlinks (e.g. like in the last comment on 2), but to no avail.

Any ideas would be much appreciated. Thanks.

This is provided as a build time decision, which is at odds with

so at least this part can't be met.

I wouldn't be very confident that your USB stick is mounted early enough to be used as /var/, OpenWrt neither has an initramfs taking care of partition mounting, nor flexible/ dynamic dependency checks to take care of specialties of this dimension - not saying that it can't be made to work, but that it will need careful consideration, testing and potentially debugging.

2 Likes

I have been using persistent var for a year now and it works great. I developed my own method and then even when I discovered it was an option I could built in I still used my method.

How much space do you have in / - is your device tiny? Would you be amenable to extroot?

My method is to use the following skeleton for /var:

# ls -l /root/admin/skel/var
drwxr-xr-x 2 root root 4096 Nov 20 15:50 cache
lrwxrwxrwx 1 root root    9 Nov 20 15:42 lock -> /tmp/lock
lrwxrwxrwx 1 root root    8 Nov 20 15:42 run -> /tmp/run
lrwxrwxrwx 1 root root   10 Nov 20 15:42 state -> /tmp/state

I have that skel which, early in my configuration process, I replace /var with:

mv /var /oldvar
cp -a /root/admin/skel/var /
# Move the old /var/opkg-lists to the new /var/cache/opkg-lists and set up opkg to use the new location
cp -a /oldvar/opkg-lists /var/cache
sed 's/var\/opkg-lists/var\/cache\/opkg-lists/' -i /etc/opkg.conf

I think this can be adapted to being USB located, with some care. Riddle me the questions I posed and we'll go from those.

Hey slh,

I wouldn't be very confident that your USB stick is mounted early enough to be used as /var/ ,

Yup, this is the problem I've suspected I've been running into. Although the comment I linked (as [2] in my original post) would lead one to believe that that can be solved by modifying /etc/init.d/boot, I just had the same problem (a soft-bricked router).

Hi Kurt,

thanks for the detailed response! It gives me hope that there's a solution on the horizon. :slight_smile:

How much space do you have in / - is your device tiny? Would you be amenable to extroot?

I believe I have ~64MB in total on /, so there's about 58MB free upon a fresh install.

When first beginning to research the question of how to best use an additional storage device on my router, I read up on extroot. What turned me off the idea were the reports I was seeing everywhere about people having troubles upgrading openwrt if they were using an extroot.

Is it still the case that it's still a bit tricky / problematic? If not, I'd give it a go, but if it is, I'd rather not have to deal with headaches any time an upgrade is released.

My method is to use the following skeleton for /var

From what I can see, you essentially replace the /var -> /tmp symlink with a skeleton /var, copy in the opkg-lists and then reconfigure opkg to look for its package lists in the correct location. Does this survive a reboot? Or are you running this automatically on every boot? If so, I'm not sure this solution would suit me - I'm looking to persist more than the opkg-lists - I want logs and everything in /var/cache persisted (without having to copy back and forth on shutdown/boot).

But maybe I'm misunderstanding what you're doing - please correct me if so. :slight_smile:

Absolutely. Everything in all of /var except for the symlinks to /tmp persists across reboots. Those symlinks are actually there to prevent just those folders from persisting, since I've found that inhibits proper booting. Nothing else is really required. You actually don't need to create /var/cache and move opkg stuff there like I do. The system would create /var/opkg-lists just fine. I just do that because every time I see application cache data not going into /var/cache it makes my FSH OCD itch.

So, yes, after creating the new /var from my skel, my /var keeps going persistently and everything is intelligent enough to just make the directories they need. This is what my /var looks like right now:

drwxr-xr-x 4 root root 3488 Feb 28 14:58 cache
drwxr-xr-x 3 root root 3488 Mar  3 11:48 etc
drwxr-xr-x 3 root root 3488 Feb 28 15:08 lib
lrwxrwxrwx 1 root root    9 Feb 28 14:58 lock -> /tmp/lock
drwxr-xr-x 3 root root 3488 Feb 28 14:57 log
lrwxrwxrwx 1 root root    8 Feb 28 14:58 run -> /tmp/run
lrwxrwxrwx 1 root root   10 Feb 28 14:58 state -> /tmp/state
drwxr-xr-x 2 root root 3488 Mar  8 13:45 tmp

The only issue with upgrading that I found when I used it, is that on upgrade you have to re-do the extroot. What I did (and still do even for my devices that don't need extroot) is put /root into a separate partition on the same USB stick. This way on upgrade, early in the configuration process, I link in my saved /root, and have access to all my personal stuff, things I really want saved, scripts, custom binaries, devel, etc. I used to do a lot of upgrades and changes as I made custom builds, still do, so I create a script with every command needed to bring a new upgrade up to my base functionality.

I don't know how much you need for packages. Everything persistent in my /var is ~5MiB, which includes 1MiB for syslog, 800KiB for opkg, and ~3MiB for /var/cache/rrd, where I store luci_statistics info (as an aside, I ended up recently making /var/cache/rrd point to my /root/admin/rrd to make my luci_statistics graphs survive upgrades). Even if you left rrd where it was, you can get away with it in your space.

The reason NOT to, though, is that making /var persistent without extroot and putting log and RRD and potentially other oft-written data there will increase the number of writes to your flash. It's actually for that reason that I went with extroot, so I would put the wear on a device I could exchange, and not the internal flash, which is critical.

My advice is to use extroot. The downsides of using it can be mitigated, and the upsides are myriad.

Hi Kurt,

I had to read through this thread a couple of times but I finally figured it out. Your solution depends on an extroot being implemented, and then it works just fine. I hadn't put 2+2 together :slight_smile:

I've got extroot installed, have followed your instructions above, rebooted, and my /var is persistent! Thanks so much. If you wouldn't mind, I might reference you if I end up writing a short blog post about this, as I'm certain it's something a lot of people encounter as a stumbling block.

Thanks again and happy weekend!

I write too much :stuck_out_tongue:

There were three options:

  1. /var as a folder on normal root
  2. /var as a folder on extroot
  3. /var as a softlink to a folder on USB

My solution works with #1 & #2. With #3 there is a small chance of issues and would need changes (I'm not going to confuse the issue now with going into it, but will if you want for completeness sake).

I think you have the best solution. With #1 you can end up with increased writing to fragile flash, and #3 is more complex.