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).

Automatic USB HDD power control for a Raspberry Pi based 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.

Automatic USB HDD power control for a Raspberry Pi based NAS

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.

 

For more detail: Automatic USB HDD power control for a Raspberry Pi based NAS


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top