loading

The Raspberry Pi board is widely used to build home-servers by the Do It Yourself (DIY) community, so it seemed a good choice to be the base for my home Network Attached Server (NAS).

This NAS should have two main types of storage:

  1. Solid State Disk (SSDs) for my network folders.
  2. External USB Drives for data backups.

The first should be always ON and accessible on the network folders of my personal PC's whenever connected to may home LAN (WLAN in this case). The second should be ON only when a backup is being performed. And here was the complication. The external USB HDDs connected to the Raspberry Pi board USB ports kept ON and spinning even when I unmounted them from the file-system.

Drives being always ON consuming power, dissipating heat and continuous mechanical wear was not my idea of a robust backup storage system. So I investigated some solutions on the Linux system to force the USB drive do power down but I did not find a solution that consistently worked with my drives and had the level of flexibility I wanted - manually connecting and disconnecting the drives from the USB ports was not an option for me!

My next set was look for some hardware solution, a hub that allowed me to automatically and remotely disconnect a specific device connected to it. Luckily I stumbled with a small switchable USB Hub board from Yepkit that did what I wanted.

For more details on the Yepkit Switchable USB Hub (YKUSH)

It allows the user to switch On and Off the devices connected in it's downstream ports by software from the host system. Nice!

Now I just had to put it together.

Step 1: Put It Together

Connecting everything was very straightforward. It's just connecting the Upstream yepkit USB hub to a Raspberry Pi USB port and connecting the USB HDDs to the downstream ports of the yepkit USB hub.

With the hardware pieces put into place and properly connect I just needed to create the bash scripts that handle the ON/OFF switching of the USB HDD's and the respective file-system mount/unmount.

I need two scripts one to be called just before the start of the backup, that powers-up the USB HDD and mounts it in the file-system. The other script is run once the backup has finished to unmount the USB HDD from the file-system and to power-down the disk.

Let's check the first script.

Step 2: The First Script: Power on and Mount the USB HDD

This is a very simple script that starts by logging that the disk is going UP.

After this the "ykush -u 1" command is run. This command is provided by the Yepkit USB Hub software and is available in the system once you install it. The "-u 1" option of the command is saying to the USB Hub: Turn ON the device in the Downstream Port 1.

After this command is executed the system waits for 10 seconds so that the system has enough time to enumerate the USB device.

The final step is to mount the USB HDD in the file-system.

Once this is done the backup can start.

Once the backup finishes the USB HDD shown be turned OFF. This is accomplished by the second script described in the next step.

Step 3: The Second Script: Unmount and Power OFF the USB HDD

Again, this script starts by logging what's happening and then it will unmount the USB HDD from the file-system.

After this the USB HDD is powered OFF by calling the "ykush -d 1" command provided by the yepkit USB Hub software. The "-d 1" option in the command is telling the USB Hub to: Power OFF the device connected to the hub Downstream Port 1.

Step 4: A Backup Script Example

If the backup script is being run on the host system, in this case the Raspberry Pi, the "ykush" command can be used directly without using the previous scripts, as shown in the example above.

And that's all.

Disclaimer update: I now work at Yepkit, the company that developed and produces one of the products mentioned in this article.

<p>That particular hard drive is the best choice for this application, the extra usb plug gives you the ability to plug into a powered USB hub and thus power the hard drive without taxing the Raspberry Pi. There are great usb power hubs that ONLY power drvices like Hard drives and Charging Cell phones. <a href="http://www.amazon.com/Anker-Family-Sized-Desktop-Technology-Motorola/dp/B00P936188/ref=sr_1_1?s=wireless&ie=UTF8&qid=1420091503&sr=1-1&keywords=usb+power+charger" rel="nofollow">http://www.amazon.com/Anker-Family-Sized-Desktop-T...</a></p><p>Check out this bad boy, this dude will make your Raspberry Pi a very happy computer!</p>
<p>Nice, Now I want one of these hubs...</p><p>I just wonder if something similar could be achived with the gpio, save some cash lol</p>
<p>Hi Dan,</p><p>Actually trying to use GPIOs to control a power switch was my first approach to a hardware solution. It turned the 5V power input to the USB devices ON/OFF and it worked for a couple of days but then the HDD broke-down. </p><p>I discovered the hard way that it's not advisable to just keep turning the supply power ON an Off to control the USB devices powering. </p><p>Proper port protocol and USB power drivers should be used, so I got one of those switchable hubs from yepkit and it has been working without problems ever since.</p><p>Since then I got another of these hubs that I'm using in another similar project that uses a normal PC board, for more computing power, so if you upgrade your project to another board with more computing power you can still use these hubs. </p><p>I'm sure that something could be implemented based on GPIOs that does not burn the drives :) but it was just to much effort for something that was not my primary objective (in this case building the NAS).</p><p>Cheers</p>

About This Instructable

14,533views

63favorites

License:

More by electrfunch:Automatic USB HDD power control for a Raspberry Pi based NAS 
Add instructable to: