5

How can I mount my usb automatically when I plug it in? Also would like the mountpoint to be the usb's label each time it is mounted automatically.

Edit: I am using Raspian (on Raspberry Pi) which is based on Debian. Trying to mount a USB flash drive. I am running Raspian in command line mode, and so want to auto mount in command line

5
  • If you're using GNOME 3 I believe this is the default behavior. It does this on my Fedora 19 laptop using GNOME 3.8.4. Is this not your case?
    – slm
    Mar 16, 2014 at 16:24
  • Please tell us some more about your situation. Which version of Debian are you using? Which desktop environment? What are you trying to mount (usb can be many things)?
    – terdon
    Mar 16, 2014 at 16:28
  • Is a solution for a specific USB drive enough? Otherwise, you are going to need to create udev rules.
    – terdon
    Mar 16, 2014 at 17:38
  • Yes. For a one USB (also would like the mountpoint to be its label name)
    – synthesis
    Mar 16, 2014 at 17:40
  • OK, please edit your question and post the output of blkid with the USB attached.
    – terdon
    Mar 16, 2014 at 17:43

1 Answer 1

5

Udev manages devices via rules that determine what to do when a device is inserted (or removed). Udev itself doesn't handle mounting, but you can make it invoke an external program to do the mounting.

There are rules, stored in the various files under /etc/udev/rules.d/, that create entries in /dev/disk/by-label/. We can use the same matching conditions to match a USB device which has a filesystem label and run a custom script.

ENV{ID_FS_LABEL_ENC}=="?*", ENV{ID_FS_USAGE}=="filesystem|other", \
SUBSYSTEMS=="usb", \
RUN += "/usr/local/sbin/udev-mount-by-label '%E{ID_FS_LABEL_ENC}'"

The custom script should create the mount point and perform the mounting. It should take a bit of care in case the directory already exists. The script I've written will do nothing if the mount point is already in use as a mount point, but will happily shadow a non-empty directory. Customize to your taste.

#!/bin/sh
export mount_point="/media/$1"
current_device=$(awk '$2 == ENVIRON["mount_point"] {print $1; exit}' </proc/mounts)
if [ -n "$current_device" ]; then
  echo 1>&2 "$current_device already mounted on $mount_point"
  exit 1
fi
mount "/dev/disk/by-label/$1" "$mount_point"

Don't forget to unmount the device before unplugging it, otherwise you may lose data.

Ubuntu - Automatically mount external drives to /media/LABEL on boot without a user logged in? shows the same technique with a different script.

3
  • Could you possibly be a bit more explicit about the steps to take. I can see the folders you're talking about, but can't decipher how to mount my USB from /media/07fb1-etc-etc.. to something like /media/customname. What is ID_FS_LABEL_ENC ? There's nothing like ENV{..} in my 2 rules files.
    – geotheory
    Apr 19, 2015 at 22:44
  • 2
    @geotheory ENV{ID_FS_LABEL_ENC} is the filessytem's label. What you show looks like a UUID which is available via ENV{ID_FS_UUID_ENC}, thus ENV{ID_FS_UUID_ENC}=="07fb1-etc-etc..", ENV{ID_FS_USAGE}=="filesystem|other", RUN+="/usr/local/sbin/udev-mount-by-uuid '%E{ID_FS_UUID_ENC}' 'customname'" and a script that mounts /dev/disk/by-uuid/$1 onto /media/$2. That, or change the label of the filesystem to customname. Apr 19, 2015 at 22:54
  • 1
    Ah I just needed to unmount and relabel using gparted in gui. Thanks.
    – geotheory
    Apr 20, 2015 at 20:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .