0

I'm looking into building a small embedded device, probably using a Banana Pi M2 Zero. In any case, the device will not have a graceful "turnoff" - it will simply lose power. However, I'm hoping to be continually writing log data to a USB stick. Two things I am concerned about: (1) whether or not I can write to a FAT-ish (or some other generally-readable filesystem) USB stick and not have it corrupt on sudden shutoff, and (2) whether or not the sudden shutoff will damage the USB stick electrically.

On (2), I'm just looking for a likelihood answer. On (1) I'm also wondering if I can do things to mitigate any issue. Does mounting the drive with a "sync" flag fix things? Is there anything else I can do?

1
  • 2
    (2) has nothing to do with Unix or Linux so it's simply off-topic here. It might be on-topic on Super User.
    – muru
    Commented Jul 1 at 13:53

1 Answer 1

1
  1. definitely not FAT32. That is a filesystem with zero support for knowing where the power went off. So, you never know whether your file was half written, and if so, for how long, or whether you corrupted your directory tables or there's really the half-written entries in there. Journalling file systems solve that issue. Log-based file systems are more or less what the name suggest, instead of a classic table of files, you write a log of what you changed and the system knows how to represent that as files (not quite that easy, in reality); such filesystems would architecturally usually be what you aim for in this situation.
    I don't have strong opinions on file systems for unreliable USB storage; it's all hardware data loss-likely to me, so being clever on top generally only takes you so far. However, it seems to me that F2FS is a good choice for such a medium, and the fsync_mode=strict would ensure a certain degree of actual file consistency.

  2. I've seen USB sticks fail from being left in a moderately temperated desk drawer for 3 months. I've seen them fail through voltage regulator failure, I've seen them fail after 10 times being plugged in and out mechanically. USB sticks are low cost devices that are in fact a whole computer inside with highly dense flash memory optimized for cheapness with (hopefully) relatively fantastic error correction codes. Predicting failure modes of such hardware is basically impossible – they're basically consumer products with "the damage done here is that the user is not happy, and will buy a low-margin device from someone else next time, possibly", not industrially qualified products.

the device will not have a graceful "turnoff" - it will simply lose power.

As an electrical engineer: wah. That seems like a fundamental design flaw that would send the engineers behind your system back to the drawing table, unless you are in an environment where losing power is a necessity, not an accident. (For example, you might be in an environment where due to explosive atmosphere you might have to short out all voltages rails within N milliseconds. Not what I think is happening here.)

This is easy to solve; a simple large electrolytic capacitor or a supercap, together with a simple diode that avoids powering the rest of the circuitry from that capacitor, would allow for a couple dozen to hundred milliseconds of holdover, which would allow for very controlled syncing of data to storage. You only need to have an input pin that observes the supply voltage to give you an early warning. (You might need to immediately disable things like UART pins connected to other, unpowered, microcontrollers, because you might otherwise be backfeeding power through their protection diodes, as suddenly the voltage on their input pins is higher than on their supply.)

You might not want to go the very current-hungry route of USB sticks, then. What you actually go for would exceed the scope of this platform.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .