Mounting external usb drives automatically to its label

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 I have 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 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 accessed 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.

Hope this was helpful. While working with usbmount I have found some more articles which might be helpful for you e.g. when you have issues with permissions or want to use ntfs as a file system.

16 thoughts on “Mounting external usb drives automatically to its label

  1. Hello, I don’t know if i’m gonna get an answer but I’m going to give it a shot. Your script works very well (I had absolutely no idea how to get the label name of an usb drive). But I still have a problem, and i’m not a pro in UNIX. When I connect a drive that has no label (ID_FS_LABEL is not defined), the symlink name will be the last value of $label (the label of the last drive connected if this one got a label). The thing I want to do is to create a symlink for drive with no label that would be for example “Unnamed_Device_1” or “Unnamed_Device_2” (getting one different symlink for every device unlabeled). As I said, i’m not a pro in UNIX, i tried all sort of things to resolve this problem but it didn’t work. So if you have time to help with that, it would be awesome. Thanks.

    1. Thanks Cyril for sharing. I actually adjusted my example above that when their is no label that no symlink is being created.

      It is good to have your example as well though if someone is looking for a different approach.

  2. Hello and great tutorial, Im also looking for a system that mounts and unmounts automatically USb devices by pressing a button copy some files and be on your way, but for me it doesnt matter what device its plugged in because i need it for a sensor station for research so that the data can be available to anyone with a usb . My questions since you’ve toying with “usbmount” when you plugg a usb it automatically mounts it, meaning any device withouth needing to format it to fat or nfts? Second I´ve read that when unpluged its automatically unmount, does it work properly or you have found any errors? And third you have written that NTFS its giving some trouble, I guess that could be fixed if you use a script that unmounts it by command. Thanks again for this tutorial and your answer.

    1. My questions since you’ve toying with “usbmount” when you plugg a usb it automatically mounts it, meaning any device withouth needing to format it to fat or nfts?

      usbmount supports any file system which it has drivers for and is in the FILESYSTEMS list in the file /etc/usbmount/usbmount.conf

      Second I´ve read that when unpluged its automatically unmount, does it work properly or you have found any errors?

      Yes it works properly because it mounts the drive with the sync option so it flushes data straight away. But this can slow down the copy process (especially with NTFS) so you can disable it by removing option sync from MOUNTOPTIONS in /etc/usbmount/usbmount.conf.

      In a script you will then need to flush the data yourself. There is no need to umount the drive but you simply have to run the command /bin/sync at the end of the command to flush the data.

      Hope this clarifies.

      1. Hi, yes it works fine for me , but I’ve encounter an error, so yes it does copy the file if i use shutilcopy2 etc my problem is that I cant copy files manually meaning i cant copy and paste anything to my external usb , it says “permision Denied” have you encounter this problem?

        1. It is mounted for root per default. If you wanna change permissions it is mounted you can change mount flags. See /etc/usbmount/usbmount.conf – there is an example above FS_MOUNTOPTIONS=

  3. Hi I have a question, as to the use of this, as if you are only are pluging in one usb drive device at a time why wouldn’t you just always point your programs to /media/usb0 ? Then it doesn’t matter what drive you have plugged in, as long as one is there.

    1. If you only have one device this works but one has to be very careful. I have used this script on a small server where a normal user was changing backup hard drives (rotating) – how quickly did it happen that the USB device was plugged in first, before removing the one already plugged in. And it also happened that more than one drive was connected (for copying data etc.) – with this script I wanted to make sure that the backup did not simply just backup to any USB device connected to the server but only the one with a specific name.

      1. if you are using it for backup drives,( only 1 USB drive plugged in at one time ) just make sure the back is done and put in a different drive. Just point the backup software to /media/usb0 . I guess if you have more than one USB drive your solution can be useful, by creating a group of hard drives that have the same volume name and use the symlink as the pointed device. Good job and thanks for the response. I will have to give this some thought as I am using a the server for plex and have 2 usb drives. Your code has gotten me thinking that I should backup up the usb drives can you show me how you would modify your script so that it would only run if the volume name was “backup” ?

        Thanks
        Richard

        1. Here is a sample backup script I use with duplicity which only makes a backup if the destination is available (resp. USB plugged in)

          #!/bin/bash
          set -o errexit # Exit on error
          set -o nounset # Trigger error when expanding unset variables

          SRC_LOCATION=/home/samba/shares/
          DEST_LOCATION=/var/run/usbmount/USBHARDDRIVELABEL

          (
          # when this process for this profile is already running this script will stop
          /usr/bin/flock -n 200

          # check whether destination location exists and is a symlink created with usbmount
          if [ -L $DEST_LOCATION ]; then

          # check whether dest location actually exist and is a directory
          if [ -d $DEST_LOCATION ]; then
          /usr/bin/duplicity $SRC_LOCATION file://$DEST_LOCATION/
          /bin/sync
          exit 0
          fi
          fi

          echo “No external hard disc found at $DEST_LOCATION”
          exit 1

          ) 200>/var/lock/duplicity-shares.lock

  4. Hi there
    I’ve tried usbmount and it mounts my pendrive
    The problem is I can’t write to the pendrive and I can’t unmount it either.
    (If I do it from command line with sudo I can unmount it)
    So I tried the article of the link (issues with permissions) but it didn’t work (can’t write/can’t unmount)
    Can you give me any advice? Thanks in advance.

Leave a Reply to tom_len Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.