Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 11-24-2009, 01:26 PM   #1
LQ Newbie
Registered: Sep 2009
Posts: 23

Rep: Reputation: 16
udev rules not unmounting usb

(Fedora 4... don't hassle me... :-P)

Here is my working udev rule for addition:
ACTION=="add", KERNEL=="sd??", BUS=="usb", DRIVER=="usb-storage", NAME="usb-%k", SYMLINK="usb/%k"
ACTION=="add", KERNEL=="sd??", BUS=="usb", DRIVER=="usb-storage", RUN+="/bin/mkdir -p /media/usb/%k"
ACTION=="add", KERNEL=="sd??", BUS=="usb", DRIVER=="usb-storage", RUN+="/bin/mount /dev/usb-%k /media/usb/%k", OPTIONS="last_rule"
And here was my first (ideal = no scripts involved) attempt for removal:
ACTION=="remove", SUBSYSTEM=="block", ENV{DEVNAME}=="/dev/usb-sd*", RUN+="/bin/umount $(/bin/mount|/bin/grep ${DEVNAME}|/bin/awk '{print $3}')"
And after that failed, I tried to get something working with a script:
ACTION=="remove", SUBSYSTEM=="block", ENV{DEVNAME}=="/dev/usb-sd*", PROGRAM="/etc/udev/scripts/", RUN+="/bin/umount %c"
And the script:
# Get the mount entry for ${DEVNAME}
MOUNT=`/bin/mount|/bin/grep ${DEVNAME}|/bin/awk '{print $3}'`
[ $? -eq 0 ] && echo $MOUNT || exit 1
According to the man-page, I should have $DEVNAME available for use in both "RUN" and "PROGRAM". None of these have worked though, leaving my usb-mount there.

The idea here is to NOT have to offshoot the work to a script (aka. my first attempt)... which is why I am getting into trouble.
Any help on what I am doing wrong?
Is inline bash just not allowed?
Old 11-24-2009, 03:27 PM   #2
LQ Newbie
Registered: Sep 2009
Posts: 23

Original Poster
Rep: Reputation: 16
I was able to make it work with some minor modifications... Apparently udev hates inline-bash?
# Auto-mount USB storage (on add):
ACTION=="add", BUS=="usb", KERNEL=="sd??", DRIVER=="usb-storage", NAME="usb-%k", SYMLINK="usb/%k"
ACTION=="add", BUS=="usb", KERNEL=="sd??", DRIVER=="usb-storage", RUN+="/bin/mkdir -p /media/usb/%k"
ACTION=="add", BUS=="usb", KERNEL=="sd??", DRIVER=="usb-storage", RUN+="/bin/mount /dev/usb-%k /media/usb/%k", OPTIONS="last_rule"

# Auto-unmount USB storage (on remove):
ACTION=="remove", SUBSYSTEM=="block", ENV{DEVNAME}=="/dev/usb-sd*", RUN+="/bin/umount /media/usb/%k"
ACTION=="remove", SUBSYSTEM=="block", ENV{DEVNAME}=="/dev/usb-sd*", RUN+="/etc/udev/scripts/ /media/usb/%k"
And my script:

# Dont run without input...
[ "$1" = "" ] && exit 1

# Figure out the directory location...
if [ -d $1 ]
elif [ -d /media/$1 ]
elif [ -d /media/usb/$1 ]

# Make sure un-mount has happened
/bin/mount|/bin/grep usb|/bin/grep $DIR >/dev/null
if [ $? -ne 0 ]
    # Is the directory full?
    /bin/ls -l $DIR|/bin/egrep "r|w|x|-|:" >/dev/null
    if [ $? -ne 0 ]
        # One more check for dir contents
        /bin/ls $DIR|/bin/egrep "*" >/dev/null
        [ $? -ne 0 ] && /bin/rm -rf $DIR
Old 11-24-2009, 03:29 PM   #3
LQ Newbie
Registered: Sep 2009
Posts: 23

Original Poster
Rep: Reputation: 16
I say this qualifies as a "no scripts" approach, only cause there is no reason I *have* to delete any empty directories (although it is nicer)...

I fear putting a rule with "rm -rf" in it... so I guess this is as good as I can get for now.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
udev: Making rules for device with multiple virtual USB-Serial Ports damien_d Linux - Hardware 1 07-12-2009 11:28 AM
USB flash disk read problems using udev rules janfranken Linux - Desktop 6 02-18-2009 06:33 PM
cat: /etc/udev/rules.d/70-persistent-net.rules: No such file or directory rcg1984 Linux From Scratch 2 09-17-2008 07:02 AM
Udev rules and USB bus cferron Linux - Hardware 4 03-11-2008 09:37 AM
slackware-current, udev 0.96, and custom udev rules not working rignes Slackware 6 08-10-2006 03:43 AM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:15 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration