LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 12-27-2007, 10:12 AM   #1
kscott121
Member
 
Registered: Jul 2003
Location: NC
Distribution: Fedora,Mepis,Debian
Posts: 84

Rep: Reputation: 15
udev handling of usb memory sticks


I am running a debian 4.0 system (embedded - no GUI)
I am trying to setup udev to properly mount and run a script when a usb stick is plugged in. I have attached 3 different USB sticks and shown my udev rules and the udevinfo output below. When I plug in, udev runs the z10-local.rules to properly mount the stick but the z80-local.rules rule ( to run the script) doesn't seem to work. Any ideas?
Also If I run the script manually it works fine.
Thanks in advance
Merry Christmas!
Ken

RULES:
debian4:/etc/udev/rules.d# cat z10-local.rules
# made by me 12/26/2007
ACTION=="add", KERNEL=="sda*", SUBSYSTEM=="block", RUN+="/bin/mount /dev/%k"
ACTION=="remove", KERNEL=="sda*", SUBSYSTEM=="block", RUN+="/bin/umount /dev/%k"
#
ACTION=="add", KERNEL=="sdb*", SUBSYSTEM=="block", RUN+="/bin/mount /dev/%k"
ACTION=="remove", KERNEL=="sdb*", SUBSYSTEM=="block", RUN+="/bin/umount /dev/%k"
#
ACTION=="add", KERNEL=="sdc*", SUBSYSTEM=="block", RUN+="/bin/mount /dev/%k"
ACTION=="remove", KERNEL=="sdc*", SUBSYSTEM=="block", RUN+="/bin/umount /dev/%k"debian4:/etc/udev/rules.d#

debian4:/etc/udev/rules.d# cat z80-local.rules
ACTION=="add", KERNEL=="sd?1", SUBSYSTEM=="block", RUN+="'/opt/usbmgmt &'"

UDEVINFO output:
debian4:/etc/udev/rules.d# udevinfo -a -p /sys/block/sda/sda1 | grep -A 6 KERNEL=
KERNEL=="sda1"
SUBSYSTEM=="block"
DRIVER==""
ATTR{stat}==" 44 261 5 5"
ATTR{size}=="8060896"
ATTR{start}=="32"
ATTR{dev}=="8:1"
debian4:/etc/udev/rules.d# udevinfo -a -p /sys/block/sdb/sdb1 | grep -A 6 KERNEL=
KERNEL=="sdb1"
SUBSYSTEM=="block"
DRIVER==""
ATTR{stat}==" 703 703 35 37"
ATTR{size}=="1993577"
ATTR{start}=="247"
ATTR{dev}=="8:17"
debian4:/etc/udev/rules.d# udevinfo -a -p /sys/block/sdc/sdc1 | grep -A 6 KERNEL=
KERNEL=="sdc1"
SUBSYSTEM=="block"
DRIVER==""
ATTR{stat}==" 610 610 0 0"
ATTR{size}=="511967"
ATTR{start}=="32"
ATTR{dev}=="8:33"
debian4:/etc/udev/rules.d#
 
Old 12-27-2007, 10:55 AM   #2
elliott678
Member
 
Registered: Mar 2005
Location: North Carolina
Distribution: Arch
Posts: 977

Rep: Reputation: 72
Code:
KERNEL=="sd[a-z]", NAME="%k", SYMLINK+="usb%m", GROUP="users", OPTIONS="last_rule"
ACTION=="add", KERNEL=="sd[a-z][0-9]", SYMLINK+="usb%n", GROUP="users", NAME="%k"
ACTION=="add", KERNEL=="sd[a-z][0-9]", RUN+="/bin/mkdir -p /mnt/usb%n"
ACTION=="add", KERNEL=="sd[a-z][0-9]", PROGRAM=="/lib/udev/vol_id -t %N", RESULT=="vfat", RUN+="/bin/mount -t vfat -o rw,noauto,flush,quiet,nodev,nosuid,noexec,noatime,dmask=000,fmask=111 /dev/%k /mnt/usb%n", OPTIONS="last_rule"
ACTION=="add", KERNEL=="sd[a-z][0-9]", RUN+="/bin/mount -t auto -o rw,noauto,sync,dirsync,noexec,nodev,noatime /dev/%k /mnt/usb%n", OPTIONS="last_rule"
ACTION=="remove", KERNEL=="sd[a-z][0-9]", RUN+="/bin/umount -l /mnt/usb%n"
ACTION=="remove", KERNEL=="sd[a-z][0-9]", RUN+="/bin/rmdir /mnt/usb%n", OPTIONS="last_rule"
That is what I use, don't know if it would work for you.
 
Old 12-27-2007, 12:53 PM   #3
kscott121
Member
 
Registered: Jul 2003
Location: NC
Distribution: Fedora,Mepis,Debian
Posts: 84

Original Poster
Rep: Reputation: 15
Elliot678,
Thanks for the quick reply and info but I have a few questions:
1)Which rules file do you put all these rules into?
2) When you have a OPTIONS="last_rule", doesn't that skip all remaining matching rules?
3)What does the "vol_id" script/program do?
4) In general, how does one test udev rules (ie watch if it even gets to a rule and why it does or doesn't run it etc.?
5) Are any other "actions" allowed other than "add" or "remove"?
6) If you pull out the USB stick that doesn't give the umount rule/command a chance to run (to properly close the device) does it?
Thanks in advance.
Ken
 
Old 12-27-2007, 01:37 PM   #4
elliott678
Member
 
Registered: Mar 2005
Location: North Carolina
Distribution: Arch
Posts: 977

Rep: Reputation: 72
Honestly, they are someone else's rules that I copied and used. I am not nearly good enough with udev to totally understand everything there, I just know that it works for me. I'll answer what I can.

1) I just put them in /etc/udev/rules.d/usbmount.rules
6) I am not totally sure, if you have something left that hasn't been written to the drive yet, it won't be happy. I haven't had any real problems with it though.
 
Old 12-27-2007, 04:49 PM   #5
kscott121
Member
 
Registered: Jul 2003
Location: NC
Distribution: Fedora,Mepis,Debian
Posts: 84

Original Poster
Rep: Reputation: 15
Solved!!! - udev handling of usb memory sticks

Used the following rules for managing the usb sticks and starting a management script when they appear. Put a 10 delay in the script (to allow the slow usb mounting process to finish - Not sure if that is needed).
Note script only works for sda1, sdb1, and sdc1 sticks (on purpose).Cheers and thanks!
Ken

ACTION=="add", KERNEL=="sd[a-z][1-9]", SUBSYSTEM=="block", RUN+="/bin/mount /dev/%k"
ACTION=="remove", KERNEL=="sd[a-z][1-9]", SUBSYSTEM=="block", RUN+="/bin/umount /dev/%k"

ACTION=="add", KERNEL=="sd[abc]1", SUBSYSTEM=="block", RUN+="/bin/bash -c '/opt/usbmgmt &'"
 
  


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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
udev: distinguish between USB floppy & memory stick jrfk2 Linux - General 4 02-15-2007 09:07 AM
memory sticks metallica1973 Linux - Hardware 3 11-14-2006 03:14 PM
Conflicting results with usb memory sticks Taliesin Linux - Hardware 4 03-31-2005 12:43 AM
USB memory sticks island_dude Linux - Hardware 1 01-20-2004 11:16 AM
Memory Sticks and Dig Cam wont Share the USB :( mudelf Linux - General 0 11-07-2003 01:48 AM


All times are GMT -5. The time now is 03:27 AM.

Main Menu
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