I wrote a rule to automount my USB stick to certain folder but when I unplug it the folder still mounted and when I ls there I see a lot of errors like: Directory bread (block: NNNN) failed

I tried to use

SUBSYSTEM=="block", ACTION=="remove", RUN+="/bin/umount /media/disk"

it's not working.

My mount rule looks like

SUBSYSTEM=="block", ACTION=="add", RUN+="/bin/mount -t vfat <options> /dev/%k /media/disk"

4 Answers 4


There are two problems here.

The first is that both your udev rules are not precise enough. Yes, they can initiate actions on plugging/unplugging a usb flash drive, but they might also be triggered when your hard drives are recognized at boot.

The second problem can be more severe in consequences: you should always unmount your drives before unplugging them. Otherwise both the drive and your system can suffer. I've personally seen people destroying their flash drives this way.

  • You are right about the hard drive (since I am using SUBSYSTEM=="block") but what is the right rules? I couldn't find other devices..and about unmount before unpluggin them...I am afraid that would be impossible to explain to my users. Actually I am using Linux RDP client and disk redirection. And when disk is redirected via RDP there is no way to unmount by RDP users.
    – bakytn
    Commented Jan 19, 2012 at 20:14
  • 1
    Who down voted this and why?
    – psusi
    Commented Jan 19, 2012 at 20:44

I've seen that flawed method around the webs and it's too generic in scope, as previous answer mentioned. It's also not dynamic enough, what if multiple USB devices were plugged in? This problem is common and solutions are around. You did not mention your distro, so I can't tell you which scripts are available packaged. I will mention them anyway, as they will show you a better method, you could always just borrow the applicable sections.

First you should really consider looking at udisks, If you use a full DE You might only need it alone. For a WM only... A great project which replaces the deprecated HAL, and uses udisks is called udisks-glue.

If you still decide to go without udisks, then have a look at udev scripts called uam. Gentoo has an ebuild, otherwise check out the source code at GitHub Bitbucket.

It's worth mentioning, Arch also has a bunch of great USB automount udev examples in their Wiki

Any of these options would be a lightweight way to do this, and are complete in implementation, specific in scope and tested for side effects.

  • environment doesn't have any DE. Thanks for uam. Looking into it
    – bakytn
    Commented Jan 23, 2012 at 10:37
  • @casey_miller: You should at least be able to get some ideas from the .in udev file, but the whole set is fairly well written IMO. It's also very lightweight. If you ask people (on sites other than ULSE) about automounting they seem to give heavy-duty overengineered solutions, or deprecated solutions using HAL. Which should not even be installed anymore, if you run a modern system. Commented Jan 24, 2012 at 5:46

You're looking for the "precog" action.

As rozcietrzewiacz said, it's a bad idea to pull your drive out while it's still mounted. By definition, the system doesn't know the drive is removed until it has already been removed. Therefore it can't automatically unmount the folders before you unplug it. Ergo, you need to tell the system that you're planning on pulling out the drive by unmounting it in some manner (manually or some safe remove option).

  • yeah I really know that it's a bad idea to unplug when drive is still mounted. The problem is that. Currently I don't any other ways. For further info read my comment to the answer by rozcietrzewiacz
    – bakytn
    Commented Jan 19, 2012 at 20:57

This is an area in which the Linux kernel is still broken. It lacks the ability to unmount a filesystem if it is in use. If some process somewhere still has a file open on that disk, then it can not be unmounted.

  • But if I try to place another command (not unmount) but, say, /bin/touch /tmp/test. It doesn't work
    – bakytn
    Commented Jan 19, 2012 at 20:59
  • so it's not possible to trigger udev rule when system detects that USB device is not plugged in anymore?
    – bakytn
    Commented Jan 19, 2012 at 20:59
  • 1
    @casey_miller It is possible, but as other answers explain, if the USB device is already unplugged, it's too late for unmounting. Commented Jan 19, 2012 at 23:17
  • @casey_miller, the rule you have written is triggered any time any block device is removed. As long as it is mounted, the block device won't be removed. You need to instead craft a rule that triggers on the underlying usb device.
    – psusi
    Commented Jan 19, 2012 at 23:37
  • 2
    @psusi: This is not just Linux specific, even with windows you should always 'eject' before pulling out a flash drive! Commented Jan 20, 2012 at 0:58

You must log in to answer this question.

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