Ferry Boender

Programmer, DevOpper, Open Source enthusiast.

Email: ferry.boender@gmail.com
Github: https://www.github.com/fboender
Résumé: PDF, Dutch
PGP key: PGP key
Blog: Electricmonk.nl
Keybase: fboender

Auto-mount external USB disk on a server

Sunday, October 4th, 2015

Althought modern Linux desktops generally automatically mount external USB disks when plugged in, servers usually don't do this. When I replaced my home server desktop model with a Raspberry Pi 2 (running Raspbian), I wanted it to automatically mount USB drives and, more importantly, make the same USB drive available at the same path at all times.

Enter usbmount

The USBmount Debian package automatically mounts USB mass storage devices (typically USB pens) when they are plugged in, and unmounts them when they are removed. The mountpoints (/media/usb[0-7] by default), filesystem types to consider, and mount options are configurable. When multiple devices are plugged in, the first available mountpoint is automatically selected. If the device provides a model name, a symlink /var/run/usbmount/MODELNAME pointing to the mountpoint is automatically created.

Just what I needed.

root@rasp# sudo apt-get install usbmount
# Plug in USB drive
root@rasp# ls -la /var/run/usbmount/
total 0
lrwxrwxrwx 1 root root 11 Oct  4 10:30 Seagate_Expansion_1 -> /media/usb0
lrwxrwxrwx 1 root root 11 Oct  4 10:30 ST4000DM_000-1F2168_1 -> /media/usb1

Great. Now I wanted the "Seagate_Expansion_1" disk to always become available at /storage. I could have created a symlink from /storage to  /var/run/usbmount/Seagate_Expansion_1, but I ran into a problem with SSHfs when trying to mount a server-side symlink on my client machine:

user@client$ sshfs -o transform_symlinks -o follow_symlinks Shares/timmy-storage/ Not a directory

So a symlink was out of the question. The binding option of 'mount' however, worked just fine:

# On the server
root@rasp# rm /storage
root@rasp# mkdir /storage
root@rasp# mount --bind /var/run/usbmount/Seagate_Expansion_1 /storage

# On the client
user@client$ sshfs Shares/timmy-storage/
user@client$ ls -l Shares/timmy-storage
total 72
drwxr-xr-x 1 1002 1003 4096 Sep 17 13:58 apps
drwxr-xr-x 1 root root 4096 Aug 24 09:15 backup

So I modified /etc/usbmount/mount.d/00_create_model_symlink and added the following code:

if [ "$name" = "Seagate_Expansion_1" ]; then
    mount --bind "/var/run/usbmount/$name" /storage

This is not a very clean solution, but it serves its purpose just fine. A nicer implementation would create a new file "01_mount_bind" which reads a config file to determine which model names to mount –bind where. That implementation is left as a reader exercise ;-)

With this setup the /storage path will automatically become available at boot-time or when the correct USB drive is plugged in. I can use SSHfs to mount the remote /storage on my Linux machine. Samba takes care of the Windows users.

The text of all posts on this blog, unless specificly mentioned otherwise, are licensed under this license.