Continuing on Automounting plugged devices in linux, which left things a bit basic. Running Gentoo, I'm automounting a combination of local and network devices on my laptop with autofs-5.0.4-r5. Doing autofs alone gives me a couple of gripes:

  • I have to predefine names for mountpoints per device, but occasionally after rebooting my MMC card reader switches positions with my bootable USB stick
  • I'd like automatic LABEL based mountpoints to appear, autofs alone AFAIK can't do that

Which is what udev and udisks are certainly capable of doing. But:

  • I don't want the filesystem to be mounted automatically when plugging the device, just give me the ghost mount point named LABEL

Actual filesystem mounting should only occur on first access to the mountpoint, just like autofs ghosting works. This should lower the risk of dirtying filesystems by accidentally unplugging devices, while still providing great accessibility.

On top of this I could use knowing at all times what's going on, in my DE.

  • Is there a system tray app that is capable of indicating status of all automounts i.e. green = nothing is mounted, red = something is mounted?
  • Is there a distribution-agnostic way to provide filesystem (un)mount operation notifications, for example using notify-send? What about Gentoo-specific, that works for me too. Scripting udev doesn't help with NFS and CIFS, right?

This combo should be the ultimate automount setup, or am I missing something?

  • I've been with Gnome now for the past few years, so rolling my own isn't as relevant anymore.
    – lkraav
    Commented Aug 13, 2018 at 9:51

2 Answers 2


This is all feasible with udev. Have a look at UAM, which seems to address most of the issues you've mentioned, but actually does mount automatically. If you like, you could make use of the great capabilities it provides, but modify it so that the media are not mounted automatically. You could also contact the author and suggest your non-automatic mounting approach as a new feature. Meanwhile, have a brief look at its capabilities:

  • It works on top of udev, making it DE-independent and event-based. (It also suggests a workaround for optical media that might not be spotted by udev.)
  • Lets you define MOUNTPOINT_TEMPLATES that can solve the naming issue you've mentioned in a number of custom ways (see uam.conf), potentially also using LABEL (as you desired)
  • Notification mechanisms are supported, you'll just need to adjust them to your DE-specific needs.
  • It seems to have a clear design, letting you easily locate where, when and how each action is triggered.
  • Today I was referred to tinymount on #awesome, those interested in this topic should probably check it out
    – lkraav
    Commented Jun 2, 2012 at 20:30
  • In the meanwhile, I've gone on to mostly using bashmount.
    – lkraav
    Commented Aug 1, 2013 at 21:40

Here is how I recently did it, and I am quite happy with this approach now. This is for Ubuntu 12.04 + gentoo, but I guess any distro, which allows to install udev and autofs should work.

Prerequisites: You have to have installed udev + autofs.

Step 1)

Create the following "/etc/udev/rules.d/90-usbsd-auto.rules" file (of course you might use any name as long as it ends with ".rules"). :

# Add symlink /dev/usbdisks/<label> to /dev/sd[a-z][1-9] 
# if partition has a label
# Add symlink /media/usb/<label> to /media/autousb/<label>
# for automounter support
ACTION=="add", KERNEL=="sd*", ENV{DEVTYPE}=="partition", \
    ENV{ID_BUS}=="usb", ENV{ID_FS_LABEL_ENC}=="?*", \
    SYMLINK+="usbdisks/$env{ID_FS_LABEL_ENC}", MODE:="0660", \
    RUN+="/bin/rm /media/usb/$env{ID_FS_LABEL_ENC}", \
    RUN+="/bin/ln -sf /media/autousb/$env{ID_FS_LABEL_ENC} /media/usb/$env{ID_FS_LABEL_ENC}"

# Fallback: If partition has a NO label, use kernel name (sd[a-z][1-9])
ACTION=="add", KERNEL=="sd*", ENV{DEVTYPE}=="partition", \
    ENV{ID_BUS}=="usb", ENV{ID_FS_LABEL_ENC}!="?*", \
    SYMLINK+="usbdisks/%k", MODE:="0660", \
    RUN+="/bin/rm /media/usb/%k", \
    RUN+="/bin/ln -sf /media/autousb/%k /media/usb/%k"

# Some FileSystems emit a "change" event when they are unmounted.
# UDEV seems to delete the device symlink in this case :-(
# So we need to re-create it here
ACTION=="change", KERNEL=="sd*", ENV{DEVTYPE}=="partition", \
    ENV{ID_BUS}=="usb", ENV{ID_FS_LABEL_ENC}=="?*", \
    SYMLINK+="usbdisks/$env{ID_FS_LABEL_ENC}", MODE:="0660"

# Fallback: If partition has NO label, use kernel name
ACTION=="change", KERNEL=="sd*", ENV{DEVTYPE}=="partition", \
    ENV{ID_BUS}=="usb", ENV{ID_FS_LABEL_ENC}!="?*", \
    SYMLINK+="usbdisks/%k", MODE:="0660"

# When device is removed, also remove /media/usb/<label>
ACTION=="remove", KERNEL=="sd*", ENV{DEVTYPE}=="partition", \
    ENV{ID_BUS}=="usb", ENV{ID_FS_LABEL_ENC}=="?*", \
    RUN+="/bin/rm /media/usb/$env{ID_FS_LABEL_ENC}"

# Fallback: If partition has no label, remove /media/usb/%k
ACTION=="remove", KERNEL=="sd*", ENV{DEVTYPE}=="partition", \
    ENV{ID_BUS}=="usb", ENV{ID_FS_LABEL_ENC}!="?*", \
    RUN+="/bin/rm /media/usb/%k"

What does this do: It creates two symlinks, one for the USB storage device partition under "/dev/usbdisks/<...>". It will either use the label of the USB storage partition or the kernel name, if there is no label. The second symlink will link from "/media/usb/<...>" to "/media/autousb/<...>" this is done for automounter support (see step 2).

Note: I originally did not use the rules with the ID_FS_LABEL_ENC variables, since this is dangerous in my opinion. What happens if you plug in two USB sticks which use the same label ?

But the poster specifically wanted to use the label of the USB disk, so I modified the rules accordingly.

To make sure udev reads these rules use

sudo udevadm control --reload-rules

With this udev rules file everything is setup to automount the USB storage device partition. Note though that right now, the device will NOT be automounted (intentionally). It will be automounted once you use it with

Step 2)

Setup autofs to automount /media/autousb/<...> : I added the following line to my "/etc/auto.master" file (for Ubuntu 12.04):

/media/autousb /etc/auto.usb --timeout=60

This means that AutoFS will unmount the device after 60 seconds of inactivity. You might want to use less or more, dependent on your taste.

For Gentoo you have to modify "/etc/autofs/auto.master" so it makes sense to use

/media/autousb /etc/autofs/auto.usb --timeout=60

Now I created "auto.usb" with the following content:


fstype=$(/sbin/blkid -o value -s TYPE /dev/usbdisks/${key})
if [ "${fstype}" = "vfat" ] ; then
  echo "-fstype=vfat,sync,uid=0,gid=plugdev,umask=007 :/dev/usbdisks/${key}"
  exit 0
if [ "${fstype}" = "ntfs" ] ; then
  echo "-fstype=fuse.ntfs-3g,sync,uid=0,gid=plugdev,umask=007 :/dev/usbdisks/${key}"
  exit 0
if [ "${fstype}" = "ext4" ] ; then
  echo "-fstype=ext4,sync,nocheck :/dev/usbdisks/${key}"
  exit 0

exit 1

This auto.usb needs to be executable, so that autofs uses this as a (bash) script. So for example

sudo chmod 0755 /etc/auto.usb

What does this do: This script will tell AutoFS (/usr/sbin/automount) how to mount the usb storage device partition.

The script will first use "/sbin/blkid" to find out what kind of file system is on the partition.

The script will then provide the right mount options depending on the device partition.

Note: I included sample code for "vfat" (probably most common for usb sticks), "ntfs" and "xfs" file systems. Of course it is quite easy to extend this to support more file systems.

Step 3) Optional... To "eject" == unmount your usb stick(s) (or partitions on your usb stick), create a script under /sbin/usbeject :

killall -s SIGUSR1 /usr/sbin/automount

With this script you might use "sudo usbeject" to unmount all mounted USB device partitions (by telling automount to unmount them).

Of course you can simply make sure that the partition is not used anywhere; automounter will then unmount the partition after the 60 second timeout...

The real trick here is to use symlinks from "/media/usb" to "/media/autousb":

  • The "/media/usb" symlinks will be created by udev, giving a user a simple overview which usb storage device partitions are there
  • AutoFS will then automount the partition on demand if you use it via /media/usb
  • With the scripted "auto.usb" file you might support any kind of file system and additionally you also are able to support any kind of naming scheme.
  • This approach supports USB sticks with multiple partition, even if these partitions use different file system types.

You must log in to answer this question.

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