Minimizing flash writes with overlayfs

Hi all

I'm working on a hotplug script for adblock-lean and I'm thinking how to minimize flash writes when that script needs to later be removed and/or re-added.

My current assumption is that moving that file (say from /etc/hotplug.d/block/99-abl-hotplug.sh to /etc/hotplug.d/block/.99-abl-hotplug.sh) and later moving it back writes less bytes to flash than deleting the file and later creating it again.

Is this assumption correct?

The question is mainly addressed to people who know the inner workings of overlayfs. Please only informed answers.

Thank you!

Like here: https://github.com/search?q=repo%3Atorvalds%2Flinux+path%3A%2F^fs\%2Foverlayfs\%2F%2F++write+OR+sync+OR+update&type=code

Thanks for the link but I can't really analyze that code.

Well I'm not deeply steeped in the inner workings of overlayfs, but I have been doing similar and not encountered flash wear issues.

In my opinion, the answer to your question is yes, your assumption is correct IF:

  1. You created the file, in the first place, directly onto overlay (ie you did not build it into rom with Imagebuilder, firmware-selector et al)
  2. You use the mv command ( NOT the cp command followed by a delete - yes, obvious for Linux-ers, but not necessarily for Windows-ers).

mv just updates the directory entries as long as the original is on overlay and not rom. If it is on rom, it will up-write the entire file from rom to overlay... I think.

I read a doc about overlayfs in the meantime and looks like this is correct. Moreover, to my understanding, once the file migrates from the 'lower' into the 'upper' filesystem, further mvs will only update the filesystem entry as well. Still not 100% sure as none of that is written explicitly.

Neither am I, but I have not seen an issue - at least not yet (some old systems still running that do the mv once every restart, so very little writing, 3 or 4 times per year most likely.

Later versions keep the file on /tmp using ln to create a link, so no longer an issue for that use case.

Do you mean something like

ln -s /etc/hotplug.d/block/99-smth.sh /tmp/actual-99-smth-file.sh

?
What happens if the file on /tmp/ is removed but the symlink remains? Does the hotplug system tolerate this? Does it just skip the symlink file when it can't be resolved?

Yes, pretty much.

I've not tried it with a hotplug script. It will probably syslog an error...

I'll maybe try a few things over a coffee if I get a minute....

It would be the other way round:
ln -s /tmp/actual-99-smth-file.sh /etc/hotplug.d/block/99-smth.sh

ie it makes a link from /tmp/actual-99-smth-file.sh to /etc/hotplug.d/block/99-smth.sh

What I was doing was creating the file on /tmp with the package startup script.
The symlink stays as a link file on flash. The link is valid if the /tmp file is present, or gives "No such file or directory" if it doesn't.

I don't know how this will fit into your use case.... but once the link file is created, there are no more writes to flash.

Ye, I messed up the order.

I'm trying adblock-lean on a device with very limited resources (a mikrotik hex lite)
It seems to be working, but then I restarted the router and it wasn't working anymore.

So I checked via SSH and saw that the service was “enabled” but “stopped.”
I tried restarting the service, and after a while Adblock-Lean was up and running again.

I’ve only been using OpenWRT for a few days, so I don’t know much about it yet. I’ve read that, in theory, if the router is slow, the service might try to start before all its dependencies have finished loading, and I’ve also read about hotplug.
Could that be the case?

Moreover I know that some init managers start services in alphabetical order, and adblock_lean, with its name, is the first of the list.

P.S. Also the restart is failing now and the stop and start too...
Maybe I have other problems...

If it has 64MiB of RAM indeed then it's going to be tough for you to get anything working, especially on a modern'ish OpenWrt version, as newer OpenWrt versions consume more memory.
The startup issues you are describing are most likely the consequence of the system running out of memory. Unlikely to be caused by anything else. You can use the command logread to see what's going on with the system.

In general, if you want to do anything with this device, you will need to make a custom build and remove basically everything that loads into memory and is removable.

I think you're right; I can't get it to boot no matter what I try. I got a little carried away when I read about this issue: https://github.com/lynxthecat/adblock-lean/issues/22

Basically, it have the same hardware, but as you say, two years have passed and OpenWRT has probably grown.

Which OpenWrt version did you install on it?

24.10.5

p.s. with the service blocked:

free --mega
              total        used        free      shared  buff/cache   available

Mem:          55200       22660       14352         840       18188       10740

Frankly, it's a bit of a miracle that 24.10 occasionally boots on this device at all with default packages. There are a few things you could try:

  1. Use OpenWrt 23.05 (earliest OpenWrt version supported by adblock-lean)
  2. Use zram-swap
  3. Customize your build via the firmware selector. Probably add zram-swap and remove luci (if removing luci, you will need to configure the device via the terminal, either by issuing uci commands or by editing the config files under /etc/config/). I'm not sure what else you can remove there but maybe someone else knows.
  4. Any combination of the above.

Edit: you could also try including zstd in the package list in Firmware selector, and changing the adblock-lean options:

compression_util="zstd"
intermediate_compression_options="-7"
final_compression_options="-8"

This should conserve just a bit more memory.

Thanks for the suggestions, I'll try to understand some of the things you told me

No problem. To make it easier for you: open the firmware selector link and search for "hex" - there is only one device matching this keyword. On the right side, you can select the desired OpenWrt version. Then click on Customize installed packages and/or first boot script. This will show you the packages which will be included in the generated firmware. I mentioned the packages luci, zram-swap, zstd. luci is included by default - you can remove it to make some more free memory but then you lose the web interface to configure OpenWrt. So it's your choice. In the same field, you can add the packages I suggested: zram-swap and zstd.

Edit: to clarify, personally I've never used zram-swap, so I'm not sure whether just installing the package is enough. Maybe you need to change some config option to make it actually work. You'll need to look it up.

Is this an automatic building system? :open_mouth:
Okay, sure, I'll look into it!

Thanks² :+1:

Yes, it is.

P.s. It would be better if the continuation of this discussion happened in adblock-lean main thread:
https://forum.openwrt.org/t/adblock-lean-set-up-adblock-using-dnsmasq-blocklist