Raspberry Pi: Dynamically mounting an external drive

A couple of weeks ago I wrote about how I use my rpi as a TimeMachine. It quickly turned out that things weren’t as nice and easy as I imagined. The hassle of dealing with corrupted HFS+ file systems was a real annoyance. But it was the silent buzzing of the external HDD made that annoyed me enough to do something about it.

Fighting the buzzing

I own a mildly aged WesternDigital MyBook Essential and even while unmounted I can hear a constant buzzing. Throughout the day it’s not a real problem but at night quite annoying. So I tried to find the cause.

Turns out there are the internet knows about a few issues with the internal system of the drive (which has its own power supply) that might keep the drive from completely shutting down. The first suggested solutions were all about compiling a custom kernel. That seemed a bit over the top and frankly, I am not that familiar with these things yet.

Unmounting, Detaching and how to get it back

udisks --detach finally led to success: The drive would now spin down completely and stay totally silent. But the system won’t recognize a detached device anymore, so no easy mounting.

It took a few attempts to get the Raspberry Pi to recognize a detached device again without physically plugging cables. The solution is to temporarily deauthorize the USB device. Once it is authorized again, the system recognizes it as a new device and allows to mount it again.

Cleaning up

From the beginning I had the plan to automatically mount and unmount the external drive based on whether it’s needed. It’s sole purpose is to be used as backup storage, so it only needs to be available while my computer is on and likely to run a backup.

The best indicator is whether my notebook is currently in the network or not. Therefore I wrote a little bash script (Gist)1 to check the presence of my computer in the network and then mount or unmount the drive as needed. A cronjob runs this script every ten minutes.

While I’m on it… Fixing the filesystem issue

And coming back one last time to the file system thing: I decided to reformat the drive as FAT32 and use it as regular network share. Luckily TimeMachine can be tricked into accepting regular network shares. It works without problems so far. Especially without corrupted filesystems.

  1. As I am still new to these scripts, don’t hesitate to share some feedback on this.