LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices

Reply
 
Search this Thread
Old 11-01-2010, 01:03 PM   #1
mpapet
Member
 
Registered: Nov 2003
Location: Los Angeles
Distribution: debian
Posts: 453

Rep: Reputation: 46
Udev not doing remove rules?


Hi,

I'm using udev version 163 from Debian Squeeze and the following remove rule does not work. As in, udev does not fire the script listed. The add rule works perfectly.

ACTION=="remove", KERNEL=="libmtp0", ATTRS{idVendor}=="1e74", ATTRS{idProduct}=="4632", GROUP="plugdev", RUN+="/usr/local/bin/mnt_mtp -u"
ACTION=="add", ATTRS{idVendor}=="1e74", ATTRS{idProduct}=="4632", SYMLINK+="libmtp0", MODE="666", GROUP="plugdev", RUN+="/usr/local/bin/mnt_mtp -m"


A rule as simple as the following works, but there's no need for the script to fire off every time I pull a usb device out.
ACTION=="remove", RUN+="/usr/local/bin/mnt_mtp -u"

There are no remove rules in /etc/udev/rules.d at all so I don't have anything to work with.

What's wrong?
 
Old 11-02-2010, 06:47 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,503

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Indeed the "remove" action is poorly documented. The caveat is that if you remove the device, the udev system is not able to parse the device attributes anymore, since they are deleted (e.g. the content of /sys/block/sdb is removed).

In this case we can use the udev environment to run an external script upon removing the usb device. First we can check what environment variables we have at our disposal every time a remove is detected. We can use udevmonitor --env (on recent releases it shoud be udevadm monitor --environment --udev):
Code:
# udevmonitor --env
udevmonitor prints the received event from the kernel [UEVENT]
and the event which udev sends out after rule processing [UDEV]

UDEV  [1288697550.654831] remove@/block/sdb
UDEV_LOG=3
ACTION=remove
DEVPATH=/block/sdb
SUBSYSTEM=block
SEQNUM=1107
MINOR=16
MAJOR=8
PHYSDEVPATH=/devices/pci0000:00/0000:00:1d.7/usb1/1-3/1-3:1.0/host10/target10:0:0/10:0:0:0
PHYSDEVBUS=scsi
PHYSDEVDRIVER=sd
UDEVD_EVENT=1
ID_VENDOR=JetFlash
ID_MODEL=TS2GJF110
ID_REVISION=0.00
ID_SERIAL=JetFlash_TS2GJF110_69a44ec615507a
ID_TYPE=disk
ID_BUS=usb
ID_PATH=pci-0000:00:1d.7-usb-0:3:1.0-scsi-0:0:0:0
DEVLINKS=/dev/disk/by-id/usb-JetFlash_TS2GJF110_69a44ec615507a /dev/disk/by-path/pci-0000:00:1d.7-usb-0:3:1.0-scsi-0:0:0:0
DEVNAME=/dev/sdb
Here I removed a JetFlash USB stick. As you can see there are a number of udev's environment variables that can uniquely identify my device. Suppose we choose the ID_MODEL, I will write a rule like this:
Code:
ACTION=="remove", ENV{ID_MODEL}=="TS2GJF110", RUN+="/usr/local/bin/test_remove.sh"
Every time ID_MODEL assumes the value "TS2GJF110" the rule is executed and if the ACTION is "remove" the external script /usr/local/bin/test_remove.sh is launched.

There is a chance your script is executed two or more times, depending on how many times the ID_MODEL variable is assigned (as you can see from the output of udevmonitor). In that case you can specify more environment variables until you're sure the device is uniquely identified and the rule is executed only once. For example I can do:
Code:
ACTION=="remove", ENV{ID_MODEL}=="TS2GJF110", ENV{ID_FS_UUID}=="6CE8-0F2C", RUN+="/usr/local/bin/test_remove.sh"
Hope this helps.

Last edited by colucix; 11-02-2010 at 07:07 AM.
 
  


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 rules greplinux Linux - Newbie 6 06-03-2010 11:46 PM
How to edit "/etc/udev/rules.d/40-basic-permissions.rules" file cygan Linux - Newbie 11 03-09-2009 05:22 AM
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
slackware-current, udev 0.96, and custom udev rules not working rignes Slackware 6 08-10-2006 03:43 AM
need help fixing my udev rules or ivman rules hedpe Linux - Hardware 2 03-18-2006 10:07 AM


All times are GMT -5. The time now is 01:45 PM.

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