Introduction: Automatic USB HDD Power Control for a Raspberry Pi Based NAS
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:
- Solid State Disk (SSDs) for my network folders.
- 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.