Docker storage (Zram ?)

Hi

just finished migrating to Dockers deployment on Rpi4 (Wohooo)
Now I have 2 things open:

  1. Should I user SSD (a) or Zram (b) to avoid SD wear ?
    2a. If SSD, what should I move to the SSD ? only the docker volumes ?
    2b. If Zram, how it works with Docker ?

If you were to go down the ZRAM path, I’d recommend not using Docker and just use openHABian. I’m not sure how/whether docker plays nicely with Zram. But if it does, you are kind of on your own to get it set up and configured because the way it works in openHABian is not going to be relevant to a Docker installation.

If you use an SSD, I’d put anything that you mount into the container as a volume onto the SSD. If possible (I vaguely remember there was a problem booting from external drives on RPi 4, maybe that’s fixed now) I’d move everything to the SSD.

But be aware, a power failure is far more likely to occur and is just as destructive to the file systems on flash based storage. You’re efforts would be better spent making sure your RPi shutdown cleanly whenever possible (UPS) first before worrying too much about SD card wearing.

I’d second Rich’ answer as I doubt anyone ever tried to combine Docker and ZRAM in an openHAB context so it might work or not but it’s for sure that you would be on your own with any problems should they arise.

Rich, any hints on this ? Maybe something I could add to openHABian, too …

Use at own risk (i’ve not tested):
If you mount your volumes to local paths, you can use ZRAM on that path.
Example:
docker-compose.yml

version: "2"

services:
  openhab2:
    network_mode: host
    volumes:
      - ./data/openhab/addons:/openhab/addons
      - ./data/openhab/conf:/openhab/conf
      - ./data/openhab/userdata:/openhab/userdata
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
    image: openhab/openhab:latest
    environment:
      - EXTRA_JAVA_OPTS=-Duser.timezone=Europe/Amsterdam
      - USER_ID=109
      - GROUP_ID=113
    devices:
      - /dev/ttyUSB-ZStick-5G:/dev/ttyACM0
      - /dev/ttyUSB-P1:/dev/ttyUSB0
    restart: always

ztab

dir    lz4     200M            600M            /opt/smarthome/data/openhab/addons       /openhab-addons
dir    lz4     200M            600M            /opt/smarthome/data/openhab/conf         /openhab-conf
dir    lz4     200M            600M            /opt/smarthome/data/openhab/userdata       /openhab-userdata

Again, i’ven’t test it, but in theorie it should work.

Even more then SSD ?
I saw some juice box based on 2x18650 batteries, will is suffice or I need real PC UPS ?

On the host or inside the docker ?

Sadly it’'s not an easy problem. First you need to set up an UPS, but not just any old spare battery. You need one that can communicate with the RPi so the RPi knows it needs to shut down. And for that it will probably end up consuming one of the RPi’s USB ports or some GPIO pins. I’ve no direct experience with any but I know they exist. You’ll have to do your own research to find something with the right cost. Searching for “raspberry pi ups” and/or “raspberry pi power management board” should get you to products to buy and tutorials to follow.

Once it’s communicating with the RPi you can either use NUT or the software the UPS to monitor the UPS. When the RPi loses power it will switch over to the battery. Here is where the software comes in though. When the battery gets below a certain charge level (usually hours later for an RPi) it will issue a shutdown -h command (or equivalent) and cleanly shutdown the RPi.

This should get you pretty far, but there may be times when the RPi has fallen off the network or is somehow otherwise semi-responsive. Rather than just yanking the power, you could wire up a button to some GPIO pins that you are a family member can press which in turn kicks off a reboot command. Search for “raspberry pi reboot button” for lots of tutorials on that.

That will give you two layers of protection from sudden power failures. Only in situations where the machine is truly frozen (e.g. kernel panic) will you need to resort to yanking the power cord. But in that case, if it’s truly unresponsive it probably isn’t writing to the file system anyway so the risk from the power loss is low.

And of course, moving stuff that writes a lot to zram is another mitigation from this. If there isn’t much writing to the file system going on, there is a lower risk of losing power in the middle of a write. But you will lose all the file system changes that only exist in zram, so while it will mitigate file system corruption, it does so at the cost of potential loss of data.

Unfortunately, I don’t run OH or anything else that I can’t afford to crash and have some down time periodically on RPis so I’ve not gone down this path with any of my RPis. My main OH server and network router/firewall (pfSense) and wifi AP are backed up by an UPS and I use NUT to cleanly shutdown all my VMs when the battery gets too low. I can get about an hour’s worth of uptime after a power outage and then, if the power is still off everything shuts down properly. At a minimum it saves the half hour it takes for my OMV virtual machine to do a file system check on all the drives when it comes back up.

What do you mean by “even more than SSD?” SSD is flashed based. If the SSD loses power in the middle of a write, not only the file being written to but any other parts of files that reside in that same sector (not sure of the proper terminology) will be lost. And with wear leveling, even files that haven’t changed since you first installed might be in that sector so you can end up with corruption of critical files like the kernel.

NOTE: on Linux, loss of power during a write is a bad idea even with HDDs, but the problem is particularly bad with SSDs and really really bad with SD and USB thumb drives.

Thanks @rlkoshak
I do understand from your comments that UPS is the best option. I do have UPS there but I never figured our its Serial port data :frowning:

So, ZRAM will help avoid corruption of data in the cost of losing some.
But, I still not sure how to use ZRAM with Docker. not much information on this.

On the host, that’s why your mount the paths to local paths as shown in my example.
But as said by the others, don’t forget an UPS!

There are some open source products that can help with that. I used one with my APC UPS for a while.

Well, you only need different hardware to run Linux :smiley: and it becomes plug and play.

  1. Get a board based on Allwinner A20 or A64 (recommended) where battery powering backup with full control over the battery is integrated. In term of performances, A64 is somewhere near Rpi3.
  2. Attach 1-4 x 18650
  3. Install Armbian | openHAB where ZRAM and other IO optimisations are there since day zero.

Enjoy OpenHab!

I have to note/recommend one good exception. Olimex Lime 2 (A20) with eMMC where SATA drive is also powered from that battery, others powers “just” the board.

Eventually I’ve went with dockers with SSD. I have UPS that I need to replace its batteries and as soon at the pledge will be over I’ll work on that.

image
image

Thanks!