I am maintaining a file server running Ubuntu 12.04 in my company and was faced with the problem how to backup my system easily and on a low cost. As we have a huge amount of data and a rather slow internet connection have I made the decision to backup onto external hard drives which we will rotate frequently. In the end USB drives are still the cheapest option to backup your data… ;).
This blog post is though not about backing up your data but rather how to mount a USB drive automatically when connected to a server and access it with its label. This helped me to have a specific mount point for backup hard discs but won’t interfere if anyone else needs to connect another USB drive to the server. I am sure you might have different use cases for such feature as well. I have tested following configuration on a Ubuntu Server 12.04 but it should run on other Debian based operating systems as well.
OK this said let’s get started with the configuration then.
While I was looking for a tool to automatically mount my usb drives have I found usbmount which was very simple and pretty much worked out of the box.
You can install it with the following command:
apt-get install usbmount
After usbmount is installed you can simply connect your usb drive to your server and usbmount will automatically mount it to /media/usb0. When an additional drive is connected it will be mounted to /media/usb1 and so on.
That’s a good start… but there is a draw back that every drive connected to the server will be mounted to /media/usb0 so we cannot really distinguish the different drives from each other. But usbmount has a feature for this that it creates a symlink for each drive in the folder /var/run/usbmount.
Such symlink can look like the following:
lrwxrwxrwx 1 root root 11 Apr 11 10:53 SAMSUNG_HM160HI_1 -> /media/usb0
The name of the symlink is built by using the vendor, model and partition number. So accessing the hard disc you simply access it using this symlink. This is great when you want to identify a specific hard disc. There is still one problem with it though when I want a group of hard disc to be access the same way I cannot really use this symlink.
So my idea was why not using the volume label and when I format hard drives I simply give all those drives the same volume label.
This can be done for instance with following format command using ext4 as an example (of course you can change ext4 to whatever file system you need):
mkfs.ext4 -L VOLUMENAME /dev/sdb1
Unfortunately usbmount doesn’t create any symlink of drives by its volume label. I therefore created following script to do this:
#!/bin/sh # This script creates the volume label symlink in /var/run/usbmount. # Copyright (C) 2014 Oliver Sauder # # This file is free software; the copyright holder gives unlimited # permission to copy and/or distribute it, with or without # modifications, as long as this notice is preserved. # # This file is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY, to the extent permitted by law; without # even the implied warranty of MERCHANTABILITY or FITNESS FOR A # PARTICULAR PURPOSE. # set -e # Exit if device or mountpoint is empty. test -z "$UM_DEVICE" && test -z "$UM_MOUNTPOINT" && exit 0 # get volume label name label=`blkid -s LABEL -o value $UM_DEVICE` # If the symlink does not yet exist, create it. test -z $label || test -e "/var/run/usbmount/$label" || ln -sf "$UM_MOUNTPOINT" "/var/run/usbmount/$label" exit 0
This script you have to save to /etc/usbmount/mount.d/01_create_label_symlink and do not forget to add executable rights to it (chmod +x /etc/usbmount/mount.d/01_create_label_symlink). usbmount will run this script whenever a hard disc is connected.
Beside creating this symlink we also have to make sure that the symlink will be removed when the hard disc is disconnected. usbmount already has a script which removes symlink from /var/run/usbmount but we have to adjust it as it only removes the first link and not all. We can simply open the file /etc/usbmount/umount.d/00_remove_model_symlink and remove the break statement in line 19.
The script will then look like the following:
#!/bin/sh # This script removes the model name symlink in /var/run/usbmount. # Copyright (C) 2005 Martin Dickopp # # This file is free software; the copyright holder gives unlimited # permission to copy and/or distribute it, with or without # modifications, as long as this notice is preserved. # # This file is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY, to the extent permitted by law; without # even the implied warranty of MERCHANTABILITY or FITNESS FOR A # PARTICULAR PURPOSE. # set -e ls /var/run/usbmount | while read name; do if test "`readlink "/var/run/usbmount/$name" || :`" = "$UM_MOUNTPOINT"; then rm -f "/var/run/usbmount/$name" fi done exit 0
So now when we connected a hard disc to the server there will be a symlink created /var/run/usbmount/VOLUMENAME and such path we can then use to point to for backups or whatever other use case you have.