Last month I experienced one of the worst but most common feeling of people relying too much on computers. I had a hard disk failure. It started with a few bad sectors and like a cancer, HDD became almost inoperable. I used ddrescue to backup the contents of the disk to another one, but quite often the disk stuck, and every following action resulted only to a new entry at /var/log/messages with "Hardware Failure" KQC. The only solution was to unplug the power from the HDD and replug it, so that ddrescue could continue backing up the disk. After 30 times doing this, I understood that due to the frequency of this hardware failures, it was impossible for me to monitor the system and reset hard disk.
So I made a php script that starts ddrescue and monitors its output if it is stuck by checking the "last successful read" field if it is more than 1 second. On failure detection the script sent, through libnotify, a message to user to reset hard disk and blocked till the disk was disconnected and reconnected. This made the process even more easier and permitted me to do other things at the PC on the same time. The only thing I had to do is every time the message pop-up I just unplug and re-plug the power on the external case. However after 50 resets this was also too much for me.
Automate power reset
I wanted something that will do everything on its own, so I thought that I should control the power of the hdd through the monitoring software. The first thing that I needed was a relay, and Saturday night is not a good time to go buy one. I remembered that, I had an old router/modem, when it booteed up it did the "click" sound of the relays. So I took it and disassembled it, seeking for a relay on its board. And I found it, it was an TX2-3v. This was perfect for me it was a 3volts controlled relay that could handle 12V at 2A (more than I needed). I took an old Fan with a mole connector and I modified it with the relay on it and two leds for showing state. I used a bit hot silicon to make the whole construction robust.
Control the relay
The next step was to control this relay. The concept is simple, if there is 2.5 - 3.5V 0.40mA at its control circuit (the blue-red cables) the relay will switch, and at our case it will power off the hard disk (by default without power to the relay, the HDD is switched on). I tried to connect relay directly with the system through RS232 on the RTC pin, but the RS232 protocol although it defines a voltage between 7V-12V for ON state, it is not more than 5mA, which is far few than what is need for this relay. I could solve this probably with a couple transistors and external power source like usb, but I had none of them. What I had was a nerdkits kit that I never used it in the past, and from what I knew it was possible to output that voltage with needed current. So following the basic guide I made a small circuit and program for the atmel to control an output pin of it and eventually the relay. The experiment was success and I started building up a complete system.
HDD Kicker is an AVR based small circuit and program to reset hard disk through the RS232. It is made in such way to prevent direct control over the power of HDD, so that any software bug does not switch the relay in an abnormal way (well "normal" is redefined already in this project). You can request a reset from the kicker and it will assure that there is a minimum time interval between resets and a minimum time for spin down and spin up. It will also count the resets and time from the last one and the progress of a reset process. All this will be displayed on 2 lines LCD screen that is connected on the circuit. There is also a test button to request reset without the need of RS232 message.
If you have played at least one time with Atmel MCU then the circuit is self explained. Pin PC4 is configured as output pin and the Relay is connect with a small resistor to drop voltage from 5V to ~ 3V. At pin PC5 a push button is connect to trigger test event. Pin PC5 is configured as pullup input, so if it is connected with the ground then you can monitor it from MCU's internal registers. The leds are connected after the relay at the 5V power of the HDD, and they work as a visual confirmation of the current relay state. I will not get in deep of how you can control pins on AVR as there are amazingly numerous articles for almost every bundle/kit.
I made also a video where you can see it working. I used OpenShot video editor for the first time and, being a bit enthusiast, I may overdid it with the directing:P Enjoy.
I am providing here the source but keep in mind it is more like a POC and not a complete and robust application that is well tested etc...
Monitoring script: ddrescue_kicker.tar.gz
HDD Kicker software: hdd_kicker.tar.gz
P.S. The whole project was made at MircoLab