Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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"
share|improve this question
add comment

4 Answers

up vote 3 down vote accepted

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.

share|improve this answer
    
environment doesn't have any DE. Thanks for uam. Looking into it –  bakytn Jan 23 '12 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. –  TechZilla Jan 24 '12 at 5:46
add comment

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.

share|improve this answer
    
But if I try to place another command (not unmount) but, say, /bin/touch /tmp/test. It doesn't work –  bakytn Jan 19 '12 at 20:59
    
so it's not possible to trigger udev rule when system detects that USB device is not plugged in anymore? –  bakytn Jan 19 '12 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. –  Gilles Jan 19 '12 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 Jan 19 '12 at 23:37
2  
@psusi: This is not just Linux specific, even with windows you should always 'eject' before pulling out a flash drive! –  TechZilla Jan 20 '12 at 0:58
show 1 more comment

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).

share|improve this answer
    
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 Jan 19 '12 at 20:57
add comment

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.

share|improve this answer
    
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 Jan 19 '12 at 20:14
1  
Who down voted this and why? –  psusi Jan 19 '12 at 20:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.