Computing stuff tied to the physical world

Hard disk power – bonus

In Hardware on Jun 2, 2011 at 00:01

Ok, there’s now a design for a high-side power switch which can power disk drives up and down at will.

Wait a minute…

You’re not supposed to power down disk drives just like that! It might be in the middle of a disk write. Even journaled disks are at risk, because journaling usually covers meta data (directories, files sizes, allocation maps, etc) … but not the data itself. So an unfortunate power down could leave the disk in an awful state: sure, the diks will be scanned and fixed on startup, but even then, some of the data blocks might contain inconsistent data. Whoops – bad idea!

One solution would be to add a JeeLink to the computer, and have it send out the power down command only after it finishes flushing and unmounting the disk. It’ll take some scripting, depending on the OS, but it’s all doable. Also, this isn’t really for disks which need to be online most of the time – for that, the normal hard disk spin down and idling modes will be fine.

But I’d like it to be a bit more automatic than that. I don’t want to have to remember to turn off the disks. Nor tie it to a specific time of day, or day-of-the-week. The whole point of these disks, is that I rarely need them. Some disks may stay off for weeks, even months.

Here’s an idea: by adding a current sensor to each disk power supply line, we could monitor disk activity and make sure that power is never shut off while a disk is “doing something”. By adding a bit of extra logic in the sketch, we could implement a timer so that the disk will only be powered down if the disk has been idle for say 15 minutes. Most operating systems have a periodic flush in place, so that changes always get flushed out to disk fairly soon after they have been buffered by the OS. If nothing has happened for a while, then we know there’s no important change pending.

OK, how do you measure the amount of current a circuit draws? Easy: insert a small resistance in series with the load, and measure the voltage drop. For the same reasons as before, we can’t do this “low side”, i.e. in the ground connection. But high-side would be fine:

Screen Shot 2011 05 30 at 02.00.54

With 1A of current, we get (using Ohm’s E=IxR law): E = 1 x 0.1 = 0.1V voltage drop across the resistor. And since the high side of the resistor is tied to “+”, all we need to do is connect the other side to an analog input.

The nice thing about the power control circuit presented yesterday, is that it has a MOSFET between + and the disk drive power pin. And MOSFETs are really very much like a small resistor when turned on. So we can simply use the MOSFET itself as a sense resistor:

Screen Shot 2011 05 30 at 01.54.25

Here are the characteristics of the P-MOSFET I’m going to try this with:

Screen Shot 2011 05 30 at 02.06.51

As you can see, at 3.3V, the MOSFET acts almost exactly like a 0.1Ω resistor: 0.1V drop at 1A – perfect!

There is still one problem though: when the MOSFET is turned off, the voltage on the low side will be at ground level, which is 8.7V below the JeeNode’s “ground”. So we can’t just tie it to an analog input pin, it would fry the ATmega. That’s is why I added a 10 kΩ resistor: it will still be a very “bad” input signal when the MOSFET is off, but the resistor will limit the current to less than 1 mA, and it will flow through the internal ESD protection diode. That amount of current should be harmless.

So now we have a way to sense the current. When the disk draws 1A, the analog input will be at 0.1V below 3.3V, i.e. 3.2V, which can easily be measured. Since the ADC resolution is 3.3 mV, this means that a change in power consumption as small as 33 mA could in principle be detected by this setup. Should be accurate enough to detect a disk spinning up or down and the seek actuator moving.

I’ve ordered a bunch of parts and will report when something useful comes out of these experiments.

  1. You still havn’t caught the case of the disk that is still mounted, but the os spins it down. It can stay spinned down for hours, so your circuit switches it off. Result would also be corrupted data on the volume. A 15 minute timer would only reduce the chance of this happening.

    You will probably need some logic in the OS to monitor the last time the disk was used and then, unmount it on spin down and send an switch-off command after that.

    You can use the OS to detect if the disk is still powered on and simply re-send the poweroff command if it still is.

    • Result would also be corrupted data on the volume.

      Are you sure? I thought all OS’es will periodically flush dirty blocks in their caches.

      Agreed, powering down a mounted disk will generate OS errors. But it should not lead to inconsistent disk state, methinks…

    • Fact is that in for example Linux there are all kinds of compilation options and options to have data buffered in ram etc. It’s not something you can automatically rely on.

      Take this case: You have an h264 video file of 600 megabytes which contains about one hour of video. The file is small enough to buffer into ram entirely, but the os knows that you will not need it entirely, so it buffers only 300 megabytes for the first 30 minutes of video. So we play the video and when you reach minute 15 your system will shut the hard-disks down when still mounted.

      The OS will detect that the disk is offline, and the video will continue playing fine until it’s at the end of the first 300M that was buffered, resulting in a stop of playback or crash of the player. You can actually try this on the most recent versions of OSX (10.6) and Linux (2.6.38) because this still is an “open issue”! However, the os builders don’t see this as an issue since they assume full control over power ups and downs of harddrives!

      The above example is read only, however if you have a filesystem that keeps a journal, you can also have a problem with writes. Files on disk will be updated when they are opened and closed. Normally this will be written to a journal and synced at some point in time. But the point in time at which this happens is in fact non-deterministic and journaled filesystems can also get corrupted. The journal just minimizes the chance of this happening by assuming the hardware cache of the harddrive has a certain size and the drive has a big enough capacitor to provide power for writing the journal from it’s cache to disk when the power disappears.

      As you can see, a lot of OS and hardware dependent details start to pop up from this point on.

  2. In my opinion, any computer system that leaves the file system “dirty” for any significant length of time (say > 30 seconds) is not very robust. Power failures do happen. I can’t imagine why an OS would intentionally spin down a disk without flushing buffers, but then I’m not an OS designer.

  3. And what about spinning up?

  4. Current sensing of the harddisk? What about the activity led to check for activity?

    • It’s an interesting idea, but a little more involved because you then have to somehow mount a sensor on each device.

      My thinking was that if the above simple current sensing is not sensitive enough, I could add an op-amp to get 10x more sensitivity (that would work for up to 3A). Then, hopefully even that blinking LED can probably be detected via current sensing.

  5. What about using automounting the disk to a fixed mounting point? The OS detects when the mounting point is accessed, and then sends the power-on signal to the disk, waits until it’s ready, and then gives the user access to it. When the mounting point is not in use anymore, after a timeout, the system unmounts it (therefore flushes all the associated buffers) and then sends the power-down signal.

    • Note that I’m also doing this to prevent auto-mounting (archive disks which shouldn’t come online unless I really want them to, or backup disks which can only be accessed a certain time of day), and to control other devices (label printer, small NAS, audio amp, scanner). So for me, actual hardware power control is also important.

      I understand that it may seem like a solution looking for a problem, but there really are several more uses once you can control any DC-powered device. And the current monitoring would be fun – perhaps I’ll even add temperature sensors using a string of 1-wire DS18B20’s.

  6. “Gepost donderdag 9 juni 2011 01:33:21”

    You are not the only one that’s building hardware for harddisk-power-switching. Check “rew”‘s messages on the site above.

Comments are closed.