Mounting usb automatically & having usb's label as mountpoint
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.
synthesis
Updated on September 18, 2022Comments
-
synthesis over 1 year
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
-
Admin about 10 yearsIf 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?
-
Admin about 10 yearsPlease 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)?
-
Admin about 10 yearsIs a solution for a specific USB drive enough? Otherwise, you are going to need to create
udev
rules. -
Admin about 10 yearsYes. For a one USB (also would like the mountpoint to be its label name)
-
Admin about 10 yearsOK, please edit your question and post the output of
blkid
with the USB attached.
-
-
Obviously about 9 yearsCould 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 isID_FS_LABEL_ENC
? There's nothing likeENV{..}
in my 2 rules files. -
Gilles 'SO- stop being evil' about 9 years@geotheory
ENV{ID_FS_LABEL_ENC}
is the filessytem's label. What you show looks like a UUID which is available viaENV{ID_FS_UUID_ENC}
, thusENV{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 tocustomname
. -
Obviously about 9 yearsAh I just needed to unmount and relabel using gparted in gui. Thanks.