LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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
 
LinkBack Search this Thread
Old 01-16-2009, 01:56 AM   #1
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Rep: Reputation: 39
Automatically running a script when a memory stick is inserted


Is it possible to automatically run a script or execute a command when a specific memory stick is inserted? What I actually want to do is mount a filesystem contained in a file on the flash drive, I have the relevant entry in the fstab, but that only works if the drive is present during boot.
 
Old 01-16-2009, 02:20 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: OpenSUSE 12.1 CentOS 6.2
Posts: 8,994

Rep: Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347
Yes. You can try to add an udev rule, specific to that particular USB stick. This issue was already discussed some time ago. Take a look here (see posts #3 and #5). Also, here is the official "Writing udev rules" document. See section "Running external programs on certain events".
 
Old 01-16-2009, 02:23 AM   #3
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Original Poster
Rep: Reputation: 39
Thanks. Did a search but that post didn't come up for some reason.
 
Old 01-16-2009, 02:31 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: OpenSUSE 12.1 CentOS 6.2
Posts: 8,994

Rep: Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347
Strange. If I click on the link of my own post, it comes up. You can try the search form: look for the string "running script USB" and select "Search in titles only" (you have to do it in the Advanced Search). The first hit is the thread I linked. Anyway, the document about Writing Udev Rules, contains all the answers to your question.
 
Old 01-16-2009, 02:40 AM   #5
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Original Poster
Rep: Reputation: 39
No I just mean I didn't find it when I searched before posting. The link works fine, thanks
 
Old 01-20-2009, 06:32 AM   #6
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Original Poster
Rep: Reputation: 39
My udev rule is partially working. The rule I'm using at the moment is
Code:
BUS=="usb",SYSFS{serial}=="21135700171F8E04",SYMLINK+="msr",RUN+="touch /home/myname/it-works"
The symlink is created in /dev, but the it-works file isn't created by the command. The command I actually want to run, to mount a filesystem image, doesn't work either. I've grepped dmesg and /var/log/messages for udev and for the name of my home directory, but didn't find anything relevant. Can anyone suggest anything?
 
Old 01-20-2009, 08:46 AM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: OpenSUSE 12.1 CentOS 6.2
Posts: 8,994

Rep: Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347
Since udev does not know about the shell environment you have to use the absolute path to the touch command. Moreover the best way to execute a command is to place it in a script (I guess the touch was only for testing purposes) and put the correct sha-bang #!/bin/bash in it. In this way the shell environment is acquired. Be sure to set permissions 755 to the script.

Anyway I suggest another approach to test your rule. The basic idea is that you have to refine your rule in order to be matched one and only one time during the chain of events performed by udev. I mean if the rule is not enough fine, there is a chance the script is executed more than one time and not only upon inserting the device, but also upon removing it. This is my testing rule:
Code:
ACTION=="add", SUBSYSTEM=="block", SYSFS{size}=="4014080", SYSFS{serial}=="69a44ec615507a", RUN+="/usr/local/bin/test.sh"
The first field marked in red ensures the rule is executed upon inserting the pen-drive only. My test script /usr/local/bin/test.sh is
Code:
#!/bin/bash
/bin/date >> /home/colucix/usb-pen-drive.log
using the date command and appending (not just redirecting) the output to a file, gives me the ability to test if the script is executed and if it is executed more than one time.

The trick is to chose the rules taking them from the output of
Code:
# udevinfo -a -p /sys/block/sda

Udevinfo starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.

  looking at device '/block/sda':
    KERNEL=="sda"
    SUBSYSTEM=="block"
    SYSFS{stat}=="     372     2029     2464     3812        0        0        0        0        0     2258     3812"
    SYSFS{size}=="4014080"
    SYSFS{removable}=="1"
    SYSFS{range}=="16"
    SYSFS{dev}=="8:0"

  looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb1/1-6/1-6:1.0/host38/target38:0:0/38:0:0:0':
    ID=="38:0:0:0"
    BUS=="scsi"
    DRIVER=="sd"
    SYSFS{ioerr_cnt}=="0x0"
    SYSFS{iodone_cnt}=="0x2e0"
    SYSFS{iorequest_cnt}=="0x2e0"
    SYSFS{iocounterbits}=="32"
    SYSFS{timeout}=="60"
    SYSFS{state}=="running"
    SYSFS{rev}=="0.00"
    SYSFS{model}=="TS2GJF110       "
    SYSFS{vendor}=="JetFlash"
    SYSFS{scsi_level}=="3"
    SYSFS{type}=="0"
    SYSFS{queue_type}=="none"
    SYSFS{queue_depth}=="1"
    SYSFS{device_blocked}=="0"
    SYSFS{max_sectors}=="240"

    ...omitted...
The /sys/block/sda device id specific for my system and my usb device, so you have first to determine which entry is created after inserting the pen-drive under /sys. Then to match the rule once and only once you have to chose at least one of those fields marked in red. They are specific of the newly added device, whereas the other fields are related to the parent device.

Finally, you can test the udev rule (having the pen-drive inserted) using
Code:
# udevtest /block/sda
again this is specific to my system, but take in mind you have to pass an argument with the /sys part stripped out (cfr. my udevinfo command above). If the rules is correct you should see a line similar to
Code:
main: run: '/usr/local/bin/test.sh'
Also look at /var/log/messages for debug output. For example, you should see something like "udevd-event[17454]: run_program: exec of program '/lib/udev/touch' failed" right now. In summary, if your rule already works, except for the RUN part, you have only to be sure it runs one time only.
 
Old 01-20-2009, 02:18 PM   #8
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Original Poster
Rep: Reputation: 39
Thanks for your help. I've done as you suggested, but the test command still fails, with no message in /var/log/messages to indicate why.

udevinfo gave the following:
Code:
Udevinfo starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.

  looking at device '/block/sdb':
    KERNEL=="sdb"
    SUBSYSTEM=="block"
    DRIVER==""
    ATTR{capability}=="13"
    ATTR{stat}=="      45      141      415       50        0        0        0        0        0       34       50"
    ATTR{size}=="4108288"
    ATTR{removable}=="1"
    ATTR{range}=="16"
    ATTR{dev}=="8:16"

  looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb2/2-5/2-5:1.0/host10/target10:0:0/10:0:0:0':
    KERNELS=="10:0:0:0"
    SUBSYSTEMS=="scsi"
    DRIVERS=="sd"
    ATTRS{modalias}=="scsi:t-0x00"
    ATTRS{ioerr_cnt}=="0x0"
    ATTRS{iodone_cnt}=="0x7e"
    ATTRS{iorequest_cnt}=="0x7e"
    ATTRS{iocounterbits}=="32"
    ATTRS{timeout}=="60"
    ATTRS{state}=="running"
    ATTRS{rev}=="5.00"
    ATTRS{model}=="Flash Disk      "
    ATTRS{vendor}=="USB 2.0 "
    ATTRS{scsi_level}=="3"
    ATTRS{type}=="0"
    ATTRS{queue_type}=="none"
    ATTRS{queue_depth}=="1"
    ATTRS{device_blocked}=="0"
    ATTRS{max_sectors}=="240"

  looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb2/2-5/2-5:1.0/host10/target10:0:0':
    KERNELS=="target10:0:0"
    SUBSYSTEMS==""
    DRIVERS==""

  looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb2/2-5/2-5:1.0/host10':
    KERNELS=="host10"
    SUBSYSTEMS==""
    DRIVERS==""

  looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb2/2-5/2-5:1.0':
    KERNELS=="2-5:1.0"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb-storage"
    ATTRS{modalias}=="usb:v0204p6025d0100dc00dsc00dp00ic08isc06ip50"
    ATTRS{bInterfaceProtocol}=="50"
    ATTRS{bInterfaceSubClass}=="06"
    ATTRS{bInterfaceClass}=="08"
    ATTRS{bNumEndpoints}=="02"
    ATTRS{bAlternateSetting}==" 0"
    ATTRS{bInterfaceNumber}=="00"

  looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb2/2-5':
    KERNELS=="2-5"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb"
    ATTRS{serial}=="21135700171F8E04"
    ATTRS{product}=="Flash Disk"
    ATTRS{manufacturer}=="USB 2.0"
    ATTRS{quirks}=="0x4"
    ATTRS{maxchild}=="0"
    ATTRS{version}==" 2.00"
    ATTRS{devnum}=="7"
    ATTRS{busnum}=="2"
    ATTRS{speed}=="480"
    ATTRS{bMaxPacketSize0}=="64"
    ATTRS{bNumConfigurations}=="1"
    ATTRS{bDeviceProtocol}=="00"
    ATTRS{bDeviceSubClass}=="00"
    ATTRS{bDeviceClass}=="00"
    ATTRS{bcdDevice}=="0100"
    ATTRS{idProduct}=="6025"
    ATTRS{idVendor}=="0204"
    ATTRS{bMaxPower}=="100mA"
    ATTRS{bopenSauceributes}=="80"
    ATTRS{bConfigurationValue}=="1"
    ATTRS{bNumInterfaces}==" 1"
    ATTRS{configuration}==""
    ATTRS{dev}=="189:134"

  looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb2':
    KERNELS=="usb2"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb"
    ATTRS{serial}=="0000:00:1d.7"
    ATTRS{product}=="EHCI Host Controller"
    ATTRS{manufacturer}=="Linux 2.6.23.1-42.fc8 ehci_hcd"
    ATTRS{quirks}=="0x0"
    ATTRS{maxchild}=="6"
    ATTRS{version}==" 2.00"
    ATTRS{devnum}=="1"
    ATTRS{busnum}=="2"
    ATTRS{speed}=="480"
    ATTRS{bMaxPacketSize0}=="64"
    ATTRS{bNumConfigurations}=="1"
    ATTRS{bDeviceProtocol}=="01"
    ATTRS{bDeviceSubClass}=="00"
    ATTRS{bDeviceClass}=="09"
    ATTRS{bcdDevice}=="0206"
    ATTRS{idProduct}=="0000"
    ATTRS{idVendor}=="0000"
    ATTRS{bMaxPower}=="  0mA"
    ATTRS{bopenSauceributes}=="e0"
    ATTRS{bConfigurationValue}=="1"
    ATTRS{bNumInterfaces}==" 1"
    ATTRS{configuration}==""
    ATTRS{dev}=="189:128"

  looking at parent device '/devices/pci0000:00/0000:00:1d.7':
    KERNELS=="0000:00:1d.7"
    SUBSYSTEMS=="pci"
    DRIVERS=="ehci_hcd"
    ATTRS{msi_bus}==""
    ATTRS{broken_parity_status}=="0"
    ATTRS{numa_node}=="-1"
    ATTRS{modalias}=="pci:v00008086d00002836sv00001179sd0000FF00bc0Csc03i20"
    ATTRS{local_cpus}=="00000000,00000000"
    ATTRS{irq}=="23"
    ATTRS{class}=="0x0c0320"
    ATTRS{subsystem_device}=="0xff00"
    ATTRS{subsystem_vendor}=="0x1179"
    ATTRS{device}=="0x2836"
    ATTRS{vendor}=="0x8086"

  looking at parent device '/devices/pci0000:00':
    KERNELS=="pci0000:00"
    SUBSYSTEMS==""
    DRIVERS==""
Taking info from the first device in the above list, I changed the udev rule:
Code:
~$ cat /etc/udev/rules.d/95-msr-usb.rules
BUS=="usb", SYSFS{serial}=="21135700171F8E04", ATTR{size}=="4108288", SYMLINK+="msr", RUN+="/home/openSauce/test.sh"
Including '"ACTION=="add",' at the beginning of the above rule made the below error appear in /var/log/messages, so I took it out:
Code:
Jan 20 19:57:09 linux-1 udevd[604]: add_to_rules: invalid ACTION operation
Jan 20 19:57:09 linux-1 udevd[604]: add_to_rules: invalid rule '/etc/udev/rules.d/95-msr-usb.rules:1'
For now it shouldn't matter if the command is run twice, although it's something to look at if and when I can get it to run at all.

The test script I'm attempting to run:
Code:
$ cat /home/openSauce/test.sh
#!/bin/bash

date >> test.log
[21:04:14] openSauce@linux-1:~$ ls -l test.sh 
-rwxrwxr-x 1 openSauce openSauce 30 2009-01-20 19:42 test.sh
And the result after inserting the pen drive:
Code:
$ ls /dev | grep msr
msr
[21:06:34] openSauce@linux-1:~$ ls /home/openSauce/test.log
ls: cannot access /home/openSauce/test.log: No such file or directory
[21:06:37] openSauce@linux-1:~$
With nothing helpful in messages (these messages all have an earlier timestamp than the most recent insertion of the pendrive, they relate to the situation before I removed 'ACTION=="add"' from the rule):
Code:
# grep udev /var/log/messages
Jan 20 12:01:57 linux-1 kernel: udev: renamed network interface ath0 to wlan0
Jan 20 19:36:55 linux-1 kernel: udev: renamed network interface ath0 to wlan0
Jan 20 19:53:28 linux-1 udevd[604]: add_to_rules: invalid ACTION operation
Jan 20 19:53:28 linux-1 udevd[604]: add_to_rules: invalid rule '/etc/udev/rules.d/95-msr-usb.rules:1'
Jan 20 19:53:28 linux-1 udevd[604]: add_to_rules: invalid ACTION operation
Jan 20 19:53:28 linux-1 udevd[604]: add_to_rules: invalid rule '/etc/udev/rules.d/95-msr-usb.rules:1'
Jan 20 19:57:09 linux-1 udevd[604]: add_to_rules: invalid ACTION operation
Jan 20 19:57:09 linux-1 udevd[604]: add_to_rules: invalid rule '/etc/udev/rules.d/95-msr-usb.rules:1'
 
Old 01-20-2009, 02:43 PM   #9
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: OpenSUSE 12.1 CentOS 6.2
Posts: 8,994

Rep: Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347
Please, can you try the following rule? This time also rename the rule giving a smaller number, since the order of the rules is relevant. In my test I used /etc/udev/rules.d/10-pen-drive.rules.
Code:
ACTION=="add", SUBSYSTEM=="block", ATTR{size}=="4108288", ATTRS{serial}=="21135700171F8E04", RUN+="/home/openSauce/test.sh"
If ACTION is still not accepted, try to remove it again and re-run. Also, after insertion of the pen-drive, can you post the output of udevtest /block/sdb? Thanks. A last consideration: looking at the output of udevinfo, you don't have any SYSFS{serial} so I'm surprised the rule is matched and creates /dev/msr. Maybe I'm missing something.

Edit: just noticed on some recent systems, like opensuse 11.x, the udevtest command has been replaced by the udevadm tool:
Code:
udevadm test /block/sdb

Last edited by colucix; 01-20-2009 at 02:54 PM. Reason: a little add-on as per above
 
Old 01-21-2009, 02:03 AM   #10
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Original Poster
Rep: Reputation: 39
Code:
# rule and script are as they should be...
[07:45:15] root@linux-1:~# ls -l /home/openSauce/test.sh
-rwxrwxr-x 1 openSauce openSauce 30 2009-01-20 19:42 /home/openSauce/test.sh

[07:45:27] root@linux-1:~# cat /home/openSauce/test.sh
#!/bin/bash

date >> test.log

[07:45:29] root@linux-1:~# ls test.log
ls: cannot access test.log: No such file or directory

[07:45:35] root@linux-1:~# cat /etc/udev/rules.d/10-msr-usb.rules
ACTION=="add", SUBSYSTEM=="block", ATTR{size}=="4108288", ATTRS{serial}=="21135700171F8E04", RUN+="/home/openSauce/test.sh"

# then after the pendrive is inserted and automounted, the script has not run successfully

[07:45:47] root@linux-1:~# ls test.log
ls: cannot access test.log: No such file or directory
The only output of grep udev /var/log/messages with today's date is about my wireless NIC. That's also the only message in dmesg | grep udev. I also grepped /var/log/* but still couldn't find anything.

My system is Fedora 8, apparently it has both udevtest and udevadm, here is the output of both. Hopefully this will tell you something! They do both mention running the script, although there is no evidence of it running or of an error that I can find.

Code:
[07:46:31] root@linux-1:~# udevtest /block/sdb
This program is for debugging only, it does not run any program,
specified by a RUN key. It may show incorrect results, because
some values may be different, or not available at a simulation run.

parse_file: reading '/etc/udev/rules.d/05-udev-early.rules' as rules file
parse_file: reading '/etc/udev/rules.d/10-msr-usb.rules' as rules file
parse_file: reading '/etc/udev/rules.d/40-alsa.rules' as rules file
parse_file: reading '/etc/udev/rules.d/40-multipath.rules' as rules file
parse_file: reading '/etc/udev/rules.d/40-redhat.rules' as rules file
parse_file: reading '/etc/udev/rules.d/50-udev-default.rules' as rules file
parse_file: reading '/etc/udev/rules.d/51-vdr.rules' as rules file
parse_file: reading '/etc/udev/rules.d/60-cdrom_id.rules' as rules file
parse_file: reading '/etc/udev/rules.d/60-libsane.rules' as rules file
parse_file: reading '/etc/udev/rules.d/60-net.rules' as rules file
parse_file: reading '/etc/udev/rules.d/60-pcmcia.rules' as rules file
parse_file: reading '/etc/udev/rules.d/60-persistent-input.rules' as rules file
parse_file: reading '/etc/udev/rules.d/60-persistent-storage-tape.rules' as rules file
parse_file: reading '/etc/udev/rules.d/60-persistent-storage.rules' as rules file
parse_file: reading '/etc/udev/rules.d/60-wacom.rules' as rules file
parse_file: reading '/etc/udev/rules.d/61-persistent-storage-edd.rules' as rules file
parse_file: reading '/etc/udev/rules.d/64-device-mapper.rules' as rules file
parse_file: reading '/etc/udev/rules.d/64-md-raid.rules' as rules file
parse_file: reading '/etc/udev/rules.d/70-persistent-cd.rules' as rules file
parse_file: reading '/etc/udev/rules.d/70-persistent-net.rules' as rules file
parse_file: reading '/etc/udev/rules.d/75-cd-aliases-generator.rules' as rules file
parse_file: reading '/etc/udev/rules.d/75-persistent-net-generator.rules' as rules file
parse_file: reading '/etc/udev/rules.d/80-drivers.rules' as rules file
parse_file: reading '/etc/udev/rules.d/85-pcscd_ccid.rules' as rules file
parse_file: reading '/etc/udev/rules.d/85-pcscd_egate.rules' as rules file
parse_file: reading '/etc/udev/rules.d/90-alsa.rules' as rules file
parse_file: reading '/etc/udev/rules.d/90-hal.rules' as rules file
parse_file: reading '/etc/udev/rules.d/95-msr-usb.rules' as rules file
parse_file: reading '/etc/udev/rules.d/95-pam-console.rules' as rules file
parse_file: reading '/etc/udev/rules.d/95-udev-late.rules' as rules file
parse_file: reading '/etc/udev/rules.d/97-bluetooth.rules' as rules file
parse_file: reading '/etc/udev/rules.d/99-fuse.rules' as rules file
parse_file: reading '/etc/udev/rules.d/xen-backend.rules' as rules file
udevtest: looking at device '/block/sdb' from subsystem 'block'
run_program: '/bin/bash -c '/sbin/lsmod | /bin/grep ^dm_multipath''
run_program: '/bin/bash' (stdout) 'dm_multipath           24401  0 '
run_program: '/bin/bash' returned with status 0
match_rule: set ENV 'DEVTYPE=disk'
run_program: 'usb_id --export /block/sdb'
run_program: '/lib/udev/usb_id' (stdout) 'ID_VENDOR=USB_2.0'
run_program: '/lib/udev/usb_id' (stdout) 'ID_MODEL=Flash_Disk'
run_program: '/lib/udev/usb_id' (stdout) 'ID_REVISION=5.00'
run_program: '/lib/udev/usb_id' (stdout) 'ID_SERIAL=USB_2.0_Flash_Disk_21135700171F8E04-0:0'
run_program: '/lib/udev/usb_id' (stdout) 'ID_SERIAL_SHORT=21135700171F8E04'
run_program: '/lib/udev/usb_id' (stdout) 'ID_TYPE=disk'
run_program: '/lib/udev/usb_id' (stdout) 'ID_INSTANCE=0:0'
run_program: '/lib/udev/usb_id' (stdout) 'ID_BUS=usb'
run_program: '/lib/udev/usb_id' returned with status 0
udev_rules_get_name: add symlink 'disk/by-id/usb-USB_2.0_Flash_Disk_21135700171F8E04-0:0'
run_program: 'path_id /block/sdb'
run_program: '/lib/udev/path_id' (stdout) 'ID_PATH=pci-0000:00:1d.7-usb-0:5:1.0-scsi-0:0:0:0'
run_program: '/lib/udev/path_id' returned with status 0
udev_rules_get_name: add symlink 'disk/by-path/pci-0000:00:1d.7-usb-0:5:1.0-scsi-0:0:0:0'
run_program: 'edd_id --export /dev/.tmp-8-16'
run_program: '/lib/udev/edd_id' (stderr) 'no kernel EDD support'
run_program: '/lib/udev/edd_id' returned with status 2
udev_rules_get_name: add symlink 'msr'
udev_rules_get_name: no node name set, will use kernel name 'sdb'
udev_device_event: device '/block/sdb' already in database, cleanup
udev_node_add: creating device node '/dev/sdb', major=8, minor=16, mode=0640, uid=0, gid=6
udev_node_update_symlinks: update symlink 'disk/by-id/usb-USB_2.0_Flash_Disk_21135700171F8E04-0:0' of '/block/sdb'
udev_db_get_devices_by_name: found index directory '/dev/.udev/names/disk\x2fby-id\x2fusb-USB_2.0_Flash_Disk_21135700171F8E04-0:0'
update_link: found 1 devices with name 'disk/by-id/usb-USB_2.0_Flash_Disk_21135700171F8E04-0:0'
update_link: found '/block/sdb' for 'disk/by-id/usb-USB_2.0_Flash_Disk_21135700171F8E04-0:0'
update_link: compare (our own) priority of '/block/sdb' 0 >= 0
update_link: 'disk/by-id/usb-USB_2.0_Flash_Disk_21135700171F8E04-0:0' with target 'sdb' has the highest priority 0, create it
udev_node_update_symlinks: update symlink 'disk/by-path/pci-0000:00:1d.7-usb-0:5:1.0-scsi-0:0:0:0' of '/block/sdb'
udev_db_get_devices_by_name: found index directory '/dev/.udev/names/disk\x2fby-path\x2fpci-0000:00:1d.7-usb-0:5:1.0-scsi-0:0:0:0'
update_link: found 1 devices with name 'disk/by-path/pci-0000:00:1d.7-usb-0:5:1.0-scsi-0:0:0:0'
update_link: found '/block/sdb' for 'disk/by-path/pci-0000:00:1d.7-usb-0:5:1.0-scsi-0:0:0:0'
update_link: compare (our own) priority of '/block/sdb' 0 >= 0
update_link: 'disk/by-path/pci-0000:00:1d.7-usb-0:5:1.0-scsi-0:0:0:0' with target 'sdb' has the highest priority 0, create it
udev_node_update_symlinks: update symlink 'msr' of '/block/sdb'
udev_db_get_devices_by_name: found index directory '/dev/.udev/names/msr'
update_link: found 1 devices with name 'msr'
update_link: found '/block/sdb' for 'msr'
update_link: compare (our own) priority of '/block/sdb' 0 >= 0
update_link: 'msr' with target 'sdb' has the highest priority 0, create it
udevtest: run: '/home/openSauce/test.sh'
udevtest: run: '/sbin/multipath -v0 8:16'
udevtest: run: 'socket:/org/freedesktop/hal/udev_event'
udevtest: run: '/sbin/pam_console_apply /dev/sdb /dev/disk/by-id/usb-USB_2.0_Flash_Disk_21135700171F8E04-0:0 /dev/disk/by-path/pci-0000:00:1d.7-usb-0:5:1.0-scsi-0:0:0:0 /dev/msr'
udevtest: run: 'socket:/org/kernel/udev/monitor'
Code:
[07:47:15] root@linux-1:~# /sbin/udevadm test /block/sdb
This program is for debugging only, it does not run any program,
specified by a RUN key. It may show incorrect results, because
some values may be different, or not available at a simulation run.

parse_file: reading '/etc/udev/rules.d/05-udev-early.rules' as rules file
parse_file: reading '/etc/udev/rules.d/10-msr-usb.rules' as rules file
parse_file: reading '/etc/udev/rules.d/40-alsa.rules' as rules file
parse_file: reading '/etc/udev/rules.d/40-multipath.rules' as rules file
parse_file: reading '/etc/udev/rules.d/40-redhat.rules' as rules file
parse_file: reading '/etc/udev/rules.d/50-udev-default.rules' as rules file
parse_file: reading '/etc/udev/rules.d/51-vdr.rules' as rules file
parse_file: reading '/etc/udev/rules.d/60-cdrom_id.rules' as rules file
parse_file: reading '/etc/udev/rules.d/60-libsane.rules' as rules file
parse_file: reading '/etc/udev/rules.d/60-net.rules' as rules file
parse_file: reading '/etc/udev/rules.d/60-pcmcia.rules' as rules file
parse_file: reading '/etc/udev/rules.d/60-persistent-input.rules' as rules file
parse_file: reading '/etc/udev/rules.d/60-persistent-storage-tape.rules' as rules file
parse_file: reading '/etc/udev/rules.d/60-persistent-storage.rules' as rules file
parse_file: reading '/etc/udev/rules.d/60-wacom.rules' as rules file
parse_file: reading '/etc/udev/rules.d/61-persistent-storage-edd.rules' as rules file
parse_file: reading '/etc/udev/rules.d/64-device-mapper.rules' as rules file
parse_file: reading '/etc/udev/rules.d/64-md-raid.rules' as rules file
parse_file: reading '/etc/udev/rules.d/70-persistent-cd.rules' as rules file
parse_file: reading '/etc/udev/rules.d/70-persistent-net.rules' as rules file
parse_file: reading '/etc/udev/rules.d/75-cd-aliases-generator.rules' as rules file
parse_file: reading '/etc/udev/rules.d/75-persistent-net-generator.rules' as rules file
parse_file: reading '/etc/udev/rules.d/80-drivers.rules' as rules file
parse_file: reading '/etc/udev/rules.d/85-pcscd_ccid.rules' as rules file
parse_file: reading '/etc/udev/rules.d/85-pcscd_egate.rules' as rules file
parse_file: reading '/etc/udev/rules.d/90-alsa.rules' as rules file
parse_file: reading '/etc/udev/rules.d/90-hal.rules' as rules file
parse_file: reading '/etc/udev/rules.d/95-msr-usb.rules' as rules file
parse_file: reading '/etc/udev/rules.d/95-pam-console.rules' as rules file
parse_file: reading '/etc/udev/rules.d/95-udev-late.rules' as rules file
parse_file: reading '/etc/udev/rules.d/97-bluetooth.rules' as rules file
parse_file: reading '/etc/udev/rules.d/99-fuse.rules' as rules file
parse_file: reading '/etc/udev/rules.d/xen-backend.rules' as rules file
udevtest: looking at device '/block/sdb' from subsystem 'block'
run_program: '/bin/bash -c '/sbin/lsmod | /bin/grep ^dm_multipath''
run_program: '/bin/bash' (stdout) 'dm_multipath           24401  0 '
run_program: '/bin/bash' returned with status 0
match_rule: set ENV 'DEVTYPE=disk'
run_program: 'usb_id --export /block/sdb'
run_program: '/lib/udev/usb_id' (stdout) 'ID_VENDOR=USB_2.0'
run_program: '/lib/udev/usb_id' (stdout) 'ID_MODEL=Flash_Disk'
run_program: '/lib/udev/usb_id' (stdout) 'ID_REVISION=5.00'
run_program: '/lib/udev/usb_id' (stdout) 'ID_SERIAL=USB_2.0_Flash_Disk_21135700171F8E04-0:0'
run_program: '/lib/udev/usb_id' (stdout) 'ID_SERIAL_SHORT=21135700171F8E04'
run_program: '/lib/udev/usb_id' (stdout) 'ID_TYPE=disk'
run_program: '/lib/udev/usb_id' (stdout) 'ID_INSTANCE=0:0'
run_program: '/lib/udev/usb_id' (stdout) 'ID_BUS=usb'
run_program: '/lib/udev/usb_id' returned with status 0
udev_rules_get_name: add symlink 'disk/by-id/usb-USB_2.0_Flash_Disk_21135700171F8E04-0:0'
run_program: 'path_id /block/sdb'
run_program: '/lib/udev/path_id' (stdout) 'ID_PATH=pci-0000:00:1d.7-usb-0:5:1.0-scsi-0:0:0:0'
run_program: '/lib/udev/path_id' returned with status 0
udev_rules_get_name: add symlink 'disk/by-path/pci-0000:00:1d.7-usb-0:5:1.0-scsi-0:0:0:0'
run_program: 'edd_id --export /dev/.tmp-8-16'
run_program: '/lib/udev/edd_id' (stderr) 'no kernel EDD support'
run_program: '/lib/udev/edd_id' returned with status 2
udev_rules_get_name: add symlink 'msr'
udev_rules_get_name: no node name set, will use kernel name 'sdb'
udev_device_event: device '/block/sdb' already in database, cleanup
udev_node_add: creating device node '/dev/sdb', major=8, minor=16, mode=0640, uid=0, gid=6
udev_node_update_symlinks: update symlink 'disk/by-id/usb-USB_2.0_Flash_Disk_21135700171F8E04-0:0' of '/block/sdb'
udev_db_get_devices_by_name: found index directory '/dev/.udev/names/disk\x2fby-id\x2fusb-USB_2.0_Flash_Disk_21135700171F8E04-0:0'
update_link: found 1 devices with name 'disk/by-id/usb-USB_2.0_Flash_Disk_21135700171F8E04-0:0'
update_link: found '/block/sdb' for 'disk/by-id/usb-USB_2.0_Flash_Disk_21135700171F8E04-0:0'
update_link: compare (our own) priority of '/block/sdb' 0 >= 0
update_link: 'disk/by-id/usb-USB_2.0_Flash_Disk_21135700171F8E04-0:0' with target 'sdb' has the highest priority 0, create it
udev_node_update_symlinks: update symlink 'disk/by-path/pci-0000:00:1d.7-usb-0:5:1.0-scsi-0:0:0:0' of '/block/sdb'
udev_db_get_devices_by_name: found index directory '/dev/.udev/names/disk\x2fby-path\x2fpci-0000:00:1d.7-usb-0:5:1.0-scsi-0:0:0:0'
update_link: found 1 devices with name 'disk/by-path/pci-0000:00:1d.7-usb-0:5:1.0-scsi-0:0:0:0'
update_link: found '/block/sdb' for 'disk/by-path/pci-0000:00:1d.7-usb-0:5:1.0-scsi-0:0:0:0'
update_link: compare (our own) priority of '/block/sdb' 0 >= 0
update_link: 'disk/by-path/pci-0000:00:1d.7-usb-0:5:1.0-scsi-0:0:0:0' with target 'sdb' has the highest priority 0, create it
udev_node_update_symlinks: update symlink 'msr' of '/block/sdb'
udev_db_get_devices_by_name: found index directory '/dev/.udev/names/msr'
update_link: found 1 devices with name 'msr'
update_link: found '/block/sdb' for 'msr'
update_link: compare (our own) priority of '/block/sdb' 0 >= 0
update_link: 'msr' with target 'sdb' has the highest priority 0, create it
udevtest: run: '/home/openSauce/test.sh'
udevtest: run: '/sbin/multipath -v0 8:16'
udevtest: run: 'socket:/org/freedesktop/hal/udev_event'
udevtest: run: '/sbin/pam_console_apply /dev/sdb /dev/disk/by-id/usb-USB_2.0_Flash_Disk_21135700171F8E04-0:0 /dev/disk/by-path/pci-0000:00:1d.7-usb-0:5:1.0-scsi-0:0:0:0 /dev/msr'
udevtest: run: 'socket:/org/kernel/udev/monitor'
[07:47:33] root@linux-1:~#
 
Old 01-21-2009, 02:13 AM   #11
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: OpenSUSE 12.1 CentOS 6.2
Posts: 8,994

Rep: Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347
Hmmm... didn't notice from your previous post, but in your script test.sh you have to use the full path of test.log in order to create it in your home directory. Again the issue is that udev does not know about the shell environment so it has no $HOME and maybe does not have a current working directory at all. So just try to modify the script as
Code:
#!/bin/bash
date >> /home/openSauce/test.log
and please still stick with my last suggested rule (it should take care of running upon insertion only and just one time):
Code:
ACTION=="add", SUBSYSTEM=="block", ATTR{size}=="4108288", ATTRS{serial}=="21135700171F8E04", RUN+="/home/openSauce/test.sh"
Indeed, the output from udevtest is correct (quite the same as mine, running on CentOS 5.2).
 
Old 01-21-2009, 02:07 PM   #12
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Original Poster
Rep: Reputation: 39
Quote:
Originally Posted by colucix View Post
Hmmm... didn't notice from your previous post, but in your script test.sh you have to use the full path of test.log in order to create it in your home directory. Again the issue is that udev does not know about the shell environment so it has no $HOME and maybe does not have a current working directory at all. So just try to modify the script as
Oops, yes, should have noticed that. Got it working now, thanks very much!

One final note for anyone trying to do the same thing (i.e. mount a filesystem image contained on the pendrive):

The pendrive does not mount until after the script test.sh returns. Therefore if you want to do anything with the files on the drive, your script should look something like this:

Code:
{ /bin/sleep 3; your-commands-here; } &
i.e. use sleep to wait for the drive to mount, and use & to get the script to return immediately.
 
Old 01-22-2009, 04:54 AM   #13
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: OpenSUSE 12.1 CentOS 6.2
Posts: 8,994

Rep: Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347
Well done, openSauce! Regarding your last statement, I would add that you should try to locate which is the rule that mounts the USB device and place the newly created rule after that. Looking at your output in one of the posts above, the matching rule for mounting USB device can be /etc/udev/rules.d/50-udev-default.rules. If you try to rename your rule as 55-msr-usb.rules, maybe the device will be already mounted when you try to access files on it.

Here is the relevant section of the Writing Udev Rules manual:
Quote:
Files in /etc/udev/rules.d/ are parsed in lexical order, and in some circumstances, the order in which rules are parsed is important. <omitted>. One device can be matched by more than one rule. This has it's practical advantages, for example, we can write two rules which match the same device, where each one provides its own alternate name for the device. Both alternate names will be created, even if the rules are in separate files. It is important to understand that udev will not stop processing when it finds a matching rule, it will continue searching and attempt to apply every rule that it knows about.
Cheers!
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to start running a script once a CD is inserted garagestudios Programming 14 12-26-2007 03:22 PM
running a script automatically skylimit Linux - Newbie 4 11-26-2006 07:02 PM
running linux from a memory stick FREEMAN10 Linux - Laptop and Netbook 13 06-03-2006 01:37 PM
Running a script as su automatically TongueTied Linux - Security 1 05-01-2006 09:06 AM
Automatically running script at shutdown samac Slackware 2 05-14-2005 10:10 AM


All times are GMT -5. The time now is 03:11 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
Open Source Consulting | Domain Registration