LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   udev RUN+= doesn't seem to work (https://www.linuxquestions.org/questions/linux-general-1/udev-run-%3D-doesnt-seem-to-work-553130/)

titopoquito 05-14-2007 03:25 AM

I tried further out of curiosity and got a similar setup working. It's a USB harddisk, not a memory card, but it is the same way regarded as scsi device by my system.

Changes I did were:

/etc/udev/10-local.rules -- creates a consistent symlink for my hd (usbhd-trekstor1 is the only partition on it). Full path to script as well as to the calling shell!
Code:

BUS=="scsi", KERNEL=="sd*",  ACTION=="add", SYSFS{model}=="HD400LD        ", SYSFS{vendor}=="SAMSUNG ", NAME="%k", SYMLINK="usbhd-trekstor%n", RUN+="/bin/sh /etc/udev/scripts/trekstor.sh"
/etc/udev/scripts/trekstor.sh -- the folder didn't exist, but I liked to put the script somewhere near my rules. I'm not sure if the sleep and sync commands are necessary, but they won't harm anyway. I could not get any GUI message working using Xdialog, so I created a logfile that will contain all mount/cp/umount successes. No error checking here, but it works. Changed the script to executable (chmod +x /etc/udev/scripts/trekstor.sh).
Code:

#!/bin/sh
export LOGFILE="/var/log/trekstor.log"

sleep 10
/sbin/mount /dev/usbhd-trekstor1 /mnt/usbhd-trekstor1 && echo "successly mounted trekstor at $(date)" >> $LOGFILE
touch /var/log/trekstor.log
cp "/mnt/data/video/fertig-archiv/Higgins - OSullivan _ WM 2007 Viertelfinale.avi" /mnt/usbhd-trekstor1/Snooker/ && echo "done copying files at $(date)" >> $LOGFILE
sleep 5
/bin/sync
/sbin/umount /mnt/usbhd-trekstor1 && echo "successfully unmounted trekstor at $(date)" >> $LOGFILE

Restarted udev, switched on my external harddisk and -- voilą!

EDIT: Of course this might be broken in another environment. Slackware 11, kernel 2.7.17.13, udev version 097

PlancksCnst 05-14-2007 01:15 PM

Success! (mostly)

Adding the path to bash worked, but I don't understand why - none of the other script calls in the rules have that. Now that it's at least getting the files over to the card, I looked up how to use rsync. So now instead of cp, I used rsync, which is much better. I also match on the serial # now, so it will work only for this memory card.

The only problem now is that it's executing the script 10 or 20 times every time I insert it.

titopoquito 05-14-2007 02:30 PM

Maybe cut out the "ADD" part to see what happens. At least here the script executed well without that. And it executed only once :)

PlancksCnst 05-15-2007 06:44 PM

So, I finally got it - still not perfect, but it works for now.
It was executing the script multiple times because of some kind of device hierarchy that I don't quite get yet. If you do a udevinfo on a device you'll see information for that device, and it's parent device, and it's parent's parent device, and so on, all the way up to the actual pci bus. Well, the model name for my device shows up a couple layers up. The serial number for the device shows up in an even higher layer. I tried matching on that instead of the model. It executed the script even more times. So I tried matching on an attribute on the "lowest" device which showed up in the udevinfo. It worked - the script was only executed once each time I inserted the card. So, apparently, it's executing the rule some number of times for each of these layers. Pretty weird. I wonder if it just has something to do with the way my distro (Zenwalk) is set up. I'll eventually get around to testing this on my Gentoo system, which hopefully will be more straightforward.

The remaining problem is now that I'm matching on a rather general attribute - the size. This is because there isn't anything more specific than that for that lowest-level device. If I add anything else with the same size, it will probably run this rule.

Here is the udevinfo for the device if it makes things any clearer.
Code:

  looking at device '/block/sdb':
    KERNEL=="sdb"
    SUBSYSTEM=="block"
    DRIVER==""
    ATTR{stat}=="      86      278      567      692        0        0        0        0        0      580      692"
    ATTR{size}=="1956864"
    ATTR{removable}=="1"
    ATTR{range}=="16"
    ATTR{dev}=="8:16"

  looking at parent device '/devices/pci0000:00/0000:00:1f.2/usb1/1-2/1-2:1.0/host21/target21:0:0/21:0:0:0':
    KERNELS=="21:0:0:0"
    SUBSYSTEMS=="scsi"
    DRIVERS=="sd"
    ATTRS{ioerr_cnt}=="0x2"
    ATTRS{iodone_cnt}=="0x66"
    ATTRS{iorequest_cnt}=="0x66"
    ATTRS{iocounterbits}=="32"
    ATTRS{timeout}=="30"
    ATTRS{state}=="running"
    ATTRS{rev}=="1.00"
    ATTRS{model}=="Flash Reader    "
    ATTRS{vendor}=="Multi  "
    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:1f.2/usb1/1-2/1-2:1.0/host21/target21:0:0':
    KERNELS=="target21:0:0"
    SUBSYSTEMS==""
    DRIVERS==""

  looking at parent device '/devices/pci0000:00/0000:00:1f.2/usb1/1-2/1-2:1.0/host21':
    KERNELS=="host21"
    SUBSYSTEMS==""
    DRIVERS==""

  looking at parent device '/devices/pci0000:00/0000:00:1f.2/usb1/1-2/1-2:1.0':
    KERNELS=="1-2:1.0"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb-storage"
    ATTRS{modalias}=="usb:v058Fp6331d0102dc00dsc00dp00ic08isc06ip50"
    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:1f.2/usb1/1-2':
    KERNELS=="1-2"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb"
    ATTRS{serial}=="058F0O1111B"
    ATTRS{product}=="Mass Storage Device"
    ATTRS{manufacturer}=="Generic"
    ATTRS{maxchild}=="0"
    ATTRS{version}==" 2.00"
    ATTRS{devnum}=="21"
    ATTRS{speed}=="12"
    ATTRS{bMaxPacketSize0}=="64"
    ATTRS{bNumConfigurations}=="1"
    ATTRS{bDeviceProtocol}=="00"
    ATTRS{bDeviceSubClass}=="00"
    ATTRS{bDeviceClass}=="00"
    ATTRS{bcdDevice}=="0102"
    ATTRS{idProduct}=="6331"
    ATTRS{idVendor}=="058f"
    ATTRS{bMaxPower}=="250mA"
    ATTRS{bmAttributes}=="80"
    ATTRS{bConfigurationValue}=="1"
    ATTRS{bNumInterfaces}==" 1"
    ATTRS{configuration}==""

  looking at parent device '/devices/pci0000:00/0000:00:1f.2/usb1':
    KERNELS=="usb1"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb"
    ATTRS{serial}=="0000:00:1f.2"
    ATTRS{product}=="UHCI Host Controller"
    ATTRS{manufacturer}=="Linux 2.6.20 uhci_hcd"
    ATTRS{maxchild}=="2"
    ATTRS{version}==" 1.10"
    ATTRS{devnum}=="1"
    ATTRS{speed}=="12"
    ATTRS{bMaxPacketSize0}=="64"
    ATTRS{bNumConfigurations}=="1"
    ATTRS{bDeviceProtocol}=="00"
    ATTRS{bDeviceSubClass}=="00"
    ATTRS{bDeviceClass}=="09"
    ATTRS{bcdDevice}=="0206"
    ATTRS{idProduct}=="0000"
    ATTRS{idVendor}=="0000"
    ATTRS{bMaxPower}=="  0mA"
    ATTRS{bmAttributes}=="e0"
    ATTRS{bConfigurationValue}=="1"
    ATTRS{bNumInterfaces}==" 1"
    ATTRS{configuration}==""

  looking at parent device '/devices/pci0000:00/0000:00:1f.2':
    KERNELS=="0000:00:1f.2"
    SUBSYSTEMS=="pci"
    DRIVERS=="uhci_hcd"
    ATTRS{msi_bus}==""
    ATTRS{broken_parity_status}=="0"
    ATTRS{enable}=="1"
    ATTRS{modalias}=="pci:v00008086d00002442sv00001179sd0000FF00bc0Csc03i00"
    ATTRS{local_cpus}=="f"
    ATTRS{irq}=="5"
    ATTRS{class}=="0x0c0300"
    ATTRS{subsystem_device}=="0xff00"
    ATTRS{subsystem_vendor}=="0x1179"
    ATTRS{device}=="0x2442"
    ATTRS{vendor}=="0x8086"

  looking at parent device '/devices/pci0000:00':
    KERNELS=="pci0000:00"
    SUBSYSTEMS==""
    DRIVERS==""



All times are GMT -5. The time now is 04:10 AM.