Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: launch script when USB-storage process starts

  1. #1

    Default launch script when USB-storage process starts

    I'm trying to get a script to launch upon the insertion of a USB memory stick. Using ps -e I can see 2 processes start when the stick arrives: hald-addon-stor and usb-storage
    is there a system interrupt that I can scan for, and then launch my script? my script uses data from the memory stick, so every time it's inserted the script should launch fresh.

  2. #2
    Join Date
    Aug 2008
    Location
    Brazil
    Posts
    3,240

    Default Re: launch script when USB-storage process starts

    When the stick is inserted you get a number of messages in the message log (dmesg). Maybe you can less and grep it for usb warning (as user, no need to su to root), and take action on the pertinent message?

    I'm not a programmer (not for a long time, anyway), so I'm not sure this will work for you.

    On second thought, maybe this is easier if you check /etc/mtab, which is a copy of fstab with current removable devices added by hal. The last inserted stick will be listed in the last line of this file.

  3. #3
    Join Date
    Oct 2008
    Location
    Birmingham. AL
    Posts
    858

    Default Re: launch script when USB-storage process starts

    Quote Originally Posted by imatinkerer View Post
    I'm trying to get a script to launch upon the insertion of a USB memory stick. Using ps -e I can see 2 processes start when the stick arrives: hald-addon-stor and usb-storage
    is there a system interrupt that I can scan for, and then launch my script? my script uses data from the memory stick, so every time it's inserted the script should launch fresh.
    If I was a kernel maintainer, I would be very nervous about something like this because it could easily be turned to malware. Anytime software automatically runs on a system change it needs to be carefully vetted, strictly limited and closely controlled.

    Just providing a hook to allow a ring-3 user to automatically run something on hardware change is a security risk, in my opinion. Something like that needs to be at the kernel level.

    (Speaking as someone who used to write anti-virus and security software way back in the DOS era ...)

    The approach suggested here, that your program could poll for changes, is the best idea.

  4. #4
    Join Date
    Jun 2008
    Location
    Podunk
    Posts
    32,336
    Blog Entries
    15

    Default Re: launch script when USB-storage process starts

    Quote Originally Posted by smpoole7
    imatinkerer;2054862 Wrote:
    > I'm trying to get a script to launch upon the insertion of a USB
    > memory stick. Using ps -e I can see 2 processes start when the stick
    > arrives: hald-addon-stor and usb-storage
    > is there a system interrupt that I can scan for, and then launch my
    > script? my script uses data from the memory stick, so every time it's
    > inserted the script should launch fresh.


    If I was a kernel maintainer, I would be very nervous about something
    like this because it could easily be turned to malware. Anytime software
    automatically runs on a system change it needs to be carefully vetted,
    strictly limited and closely controlled.

    Just providing a hook to allow a ring-3 user to automatically run
    something on hardware change is a security risk, in my opinion.
    Something like that needs to be at the kernel level.

    (Speaking as someone who used to write anti-virus and security software
    way back in the DOS era ...)

    The approach suggested here, that your program could poll for changes,
    is the best idea.
    Hi
    The OP could write a udev rule associated with the specific device and
    run the script in the users group.

    Code:
    BUS=="usb", ENV{DEVTYPE}=="usb_device", ACTION=="add",
    SYSFS{idVendor}=="xxxx", SYSFS{idProduct}=="yyyy", MODE="0660",
    GROUP="users", RUN="/soem/script"
    where xxxx and yyyy match the info from dmesg when the device is
    plugged in.

    http://reactivated.net/writing_udev_rules.html

    --
    Cheers Malcolm (Linux Counter #276890)
    openSUSE 11.2 RC 1 (i586) Kernel 2.6.31.3-1-desktop
    up 1:18, 1 user, load average: 0.33, 0.16, 0.13
    ASUS eeePC 1000HE ATOM N280 1.66GHz | GPU Mobile 945GM/GMS/GME


  5. #5

    Default Re: launch script when USB-storage process starts

    this last post is very elegant and appears to fit the bill; however, I'm having some difficulty still:
    I created a new rule - /etc/udev/rules.d/30-usb-action.rules that has but one line of code - BUS=="usb", ENV{DEVTYPE}=="usb_device", ACTION=="add", SYSFS{idVendor}=="0781", MODE="0660", GROUP="users", RUN="/home/dpf/import_pix.sh"

    I omitted the idProduct data because I'm using two different USB sticks

    I've read through the referenced link (http://reactivate.net/writing_udev_rules.html)and am attempting a similar process (in that writing, the author wants to automatically mount a camera connected via USB). I'm trying to run a script to copy *.jpg files from the USB stick into a directory on SDA1.

    I can manually run the script and it works, but nothing happens upon inserting the USB stick. I get a screen-pop that the system has recognized the arrival of the stick and even get messages in DMESG. MTAB remains unpopulated until I open Dolphin (file browser) and select the VFAT in the left vertical banner.

    DMESG:
    powernow: This module only works with AMD K7 CPUs
    IA-32 Microcode Update Driver: v1.14a <tigran@aivazian.fsnet.co.uk>
    bootsplash: status on console 0 changed to on
    usb 1-1.3: new full speed USB device using uhci_hcd and address 5
    usb 1-1.3: configuration #1 chosen from 1 choice
    usb 1-1.3: New USB device found, idVendor=0781, idProduct=5151
    usb 1-1.3: New USB device strings: Mfr=1, Product=2, SerialNumber=3
    usb 1-1.3: Product: OEM UFD
    usb 1-1.3: Manufacturer: SanDisk Corporation
    usb 1-1.3: SerialNumber: 20052444300EE4E050F2
    Initializing USB Mass Storage driver...
    scsi2 : SCSI emulation for USB Mass Storage devices
    usbcore: registered new interface driver usb-storage
    USB Mass Storage support registered.
    usb-storage: device found at 5
    usb-storage: waiting for device to settle before scanning
    scsi 2:0:0:0: Direct-Access SanDisk OEM UFD 0.2 PQ: 0 ANSI: 2
    sd 2:0:0:0: [sdb] 250879 512-byte hardware sectors (128 MB)
    sd 2:0:0:0: [sdb] Write Protect is off
    sd 2:0:0:0: [sdb] Mode Sense: 03 00 00 00
    sd 2:0:0:0: [sdb] Assuming drive cache: write through
    sd 2:0:0:0: [sdb] 250879 512-byte hardware sectors (128 MB)
    sd 2:0:0:0: [sdb] Write Protect is off
    sd 2:0:0:0: [sdb] Mode Sense: 03 00 00 00
    sd 2:0:0:0: [sdb] Assuming drive cache: write through
    sdb: sdb1
    sd 2:0:0:0: [sdb] Attached SCSI removable disk
    sd 2:0:0:0: Attached scsi generic sg1 type 0
    usb-storage: device scan complete
    printk: 3 messages suppressed.
    found SMC SuperIO Chip (devid=0x0a rev=00 base=0x00e0): FDC37N971
    smsc_ircc_present: can't get sir_base of 0x3e8
    linux-dux0:/home/dpf #


    MTAB (after using Dolphin)
    linux-dux0:/home/dpf # cat /etc/mtab
    /dev/sda2 / ext3 rw,acl,user_xattr 0 0
    proc /proc proc rw 0 0
    sysfs /sys sysfs rw 0 0
    debugfs /sys/kernel/debug debugfs rw 0 0
    udev /dev tmpfs rw 0 0
    devpts /dev/pts devpts rw,mode=0620,gid=5 0 0
    fusectl /sys/fs/fuse/connections fusectl rw 0 0
    securityfs /sys/kernel/security securityfs rw 0 0
    none /proc/sys/fs/binfmt_misc binfmt_misc rw 0 0
    /dev/sdb1 /media/disk vfat rw,nosuid,nodev,uid=1000 0 0
    linux-dux0:/home/dpf #

  6. #6
    Join Date
    Jun 2008
    Location
    Podunk
    Posts
    32,336
    Blog Entries
    15

    Default Re: launch script when USB-storage process starts

    Quote Originally Posted by imatinkerer
    this last post is very elegant and appears to fit the bill; however, I'm
    having some difficulty still:
    I created a new rule - /etc/udev/rules.d/30-usb-action.rules that has
    but one line of code - BUS=="usb", ENV{DEVTYPE}=="usb_device",
    ACTION=="add", SYSFS{idVendor}=="0781", MODE="0660", GROUP="users",
    RUN="/home/dpf/import_pix.sh"

    I omitted the idProduct data because I'm using two different USB
    sticks
    <snip>
    Hi
    A couple of suggestions. You may need to change the position of the
    rule, eg try 65-usb-action.rules (that's what i got my blackberry
    working on).

    Also add two rules in the udev file for both product id's.

    Last of all it may pay to move your script to say /usr/local/bin and
    set ownership to root:root and chmod 755 it.

    --
    Cheers Malcolm (Linux Counter #276890)
    openSUSE 11.2 RC 1 (i586) Kernel 2.6.31.3-1-desktop
    up 7:47, 1 user, load average: 0.05, 0.28, 0.25
    ASUS eeePC 1000HE ATOM N280 1.66GHz | GPU Mobile 945GM/GMS/GME


  7. #7
    Join Date
    Sep 2008
    Location
    Earl Shilton UK
    Posts
    298

    Default Re: launch script when USB-storage process starts

    Does HAL/udev actually *mount* the volume when it is plugged in, or does that only happen when you click it with dolphin? I was just thinking about it not showing in mtab until opened with dolphin.

    If that's the case, if the script tries to (for example) copy from the volume to somewhere, it's not going to work until the volume is mounted - unless it gets mounted automagically upon access... I don't know to be honest, but thought I'd chuck my 2 cents in anyway
    Happily using Linux since 1998
    Share & Enjoy

  8. #8

    Default Re: launch script when USB-storage process starts

    I'm suspecting that the volume does not mount automatically, despite the screen-pop alerting me to the arrival of the device.
    Is there a way to confirm my suspicion, and (furthermore) to force it to mount? I could include a line in the script, then let it sleep for x seconds.

  9. #9
    Join Date
    Jun 2008
    Location
    Podunk
    Posts
    32,336
    Blog Entries
    15

    Default Re: launch script when USB-storage process starts

    Quote Originally Posted by imatinkerer
    I'm suspecting that the volume does not mount automatically, despite the
    screen-pop alerting me to the arrival of the device.
    Is there a way to confirm my suspicion, and (furthermore) to force it
    to mount? I could include a line in the script, then let it sleep for x
    seconds.
    Hi
    Install ivman and enable the service.

    "Ivman, or Ikke's Volume Manager, is a daemon to handle the
    mounting of media as they are inserted/attached to the system. It can
    also be used to execute arbitrary commands when a device with certain
    properties is added to the system (Windows autoplay style
    functionality), and to exe- cute arbitrary commands when device
    properties change. Ivman uses HAL to monitor the state of your
    system's hardware."

    Code:
    sudo zypper in ivman
    sudo /sbin/chkconfig ivman on
    sudo /usr/sbin/rcivman start
    So in theory you may be able to use this to start your script by
    configuring /etc/ivman/IvmConfigActions.xml

    --
    Cheers Malcolm (Linux Counter #276890)
    openSUSE 11.2 RC 1 (i586) Kernel 2.6.31.3-1-desktop
    up 20:41, 1 user, load average: 0.08, 0.13, 0.09
    ASUS eeePC 1000HE ATOM N280 1.66GHz | GPU Mobile 945GM/GMS/GME


  10. #10

    Default Re: launch script when USB-storage process starts

    so, I was all excited last night when I installed Ivman and the USB stick mounted. I verified this by
    cat /etc/mtab
    and saw sdb1 and by doing an
    ls /media/disk to see the contents of the stick

    the real test is in unplugging the stick and re-plugging it. when that happens, I see the screen pop that a VFAT device has arrived, but now I'm back to the same issue of the stick not mounting.

    I believe that the udev rule (stated in previous post) is functioning because I see /dev/mem_stick

    I've tried renaming my udev rule to a higher number (65-usb-action.rules) and a lower number (10-usb-action.rules) to no avail.

    Once again, if I click on the VFAT volume in Dolphin, the stick mounts and I can see it in MTAB and from an ls /media/disk

    Does anybody know the connection between the screen pop and the actual mount?

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •