LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices



Reply
 
Search this Thread
Old 11-24-2009, 02:26 PM   #1
jjinno
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:
Code:
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:
Code:
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:
Code:
ACTION=="remove", SUBSYSTEM=="block", ENV{DEVNAME}=="/dev/usb-sd*", PROGRAM="/etc/udev/scripts/usbdrive.sh", RUN+="/bin/umount %c"
And the script:
Code:
#!/bin/sh
# 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, 04:27 PM   #2
jjinno
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?
Code:
# 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/usbcleanup.sh /media/usb/%k"
And my usbcleanup.sh script:
Code:
#!/bin/bash

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

# Figure out the directory location...
if [ -d $1 ]
then
    DIR=$1
elif [ -d /media/$1 ]
then
    DIR="/media/$1"
elif [ -d /media/usb/$1 ]
then
    DIR="/media/usb/$1"
fi

# Make sure un-mount has happened
/bin/mount|/bin/grep usb|/bin/grep $DIR >/dev/null
if [ $? -ne 0 ]
then
    # Is the directory full?
    /bin/ls -l $DIR|/bin/egrep "r|w|x|-|:" >/dev/null
    if [ $? -ne 0 ]
    then
        # One more check for dir contents
        /bin/ls $DIR|/bin/egrep "*" >/dev/null
        [ $? -ne 0 ] && /bin/rm -rf $DIR
    fi
fi
 
Old 11-24-2009, 04:29 PM   #3
jjinno
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.
 
  


Reply


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 12:28 PM
USB flash disk read problems using udev rules janfranken Linux - Desktop 6 02-18-2009 07:33 PM
cat: /etc/udev/rules.d/70-persistent-net.rules: No such file or directory rcg1984 Linux From Scratch 2 09-17-2008 08:02 AM
Udev rules and USB bus cferron Linux - Hardware 4 03-11-2008 10:37 AM
slackware-current, udev 0.96, and custom udev rules not working rignes Slackware 6 08-10-2006 04:43 AM


All times are GMT -5. The time now is 04:51 PM.

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