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.
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.
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.
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
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 #
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
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
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.”
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
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?
Hi
Are you just unplugging the device or syncing and unmounting the device
via the command line? (the latter I hope??).
I would look at removing the udev rule and just trying out ivman and
ensuring your script syncs and unmounts the device.
–
Cheers Malcolm °¿° (Linux Counter #276890)
SUSE Linux Enterprise Desktop 11 (x86_64) Kernel 2.6.27.29-0.1-default
up 17 days 22:29, 3 users, load average: 0.09, 0.11, 0.06
GPU GeForce 8600 GTS Silent - CUDA Driver Version: 190.18
alas, my method of removal is completely crude: I’m simply unplugging the USB stick. I’m trying to emulate the process for my finished product: a digital picture frame.
I’ve dismantled an old compaq armada laptop and built it into a shadow box picture frame.
The laptop is of sufficient age to be equipped with but a single USB port.
For my testing, I have a 4-port USB hub plugged into the lone computer USB, with a USB keyboard and mouse for my I/O. I’m plugging and unplugging the USB stick to/from the 4-port hub.
My ultimate goal is to eliminate human intervention by having a script import (copy) the picture files onto the laptop hard drive whenever the USB arrives.
I sincerely appreciate all the input and forebearance for my noob-ness.
Hi
It’s not a problem plugging the device in. What you need to ensure is
that once your script finished it runs sync command then umounts the
device. At that point you could display a dialog for a short period of
time to say the device can be removed. The dialog could then be closed
without any user intervention.
–
Cheers Malcolm °¿° (Linux Counter #276890)
SUSE Linux Enterprise Desktop 11 (x86_64) Kernel 2.6.27.29-0.1-default
up 18 days 10:05, 3 users, load average: 0.08, 0.06, 0.08
GPU GeForce 8600 GTS Silent - CUDA Driver Version: 190.18
ok - that makes perfect sense and seems easily accomplished with a simple tag line at the end of the script, such as sync | umount /dev/sdb1
my hurdle now: I’ve come full circle and the drive fails to mount upon insertion. I’ve completely removed my udev rule and the only addition to the /etc/ivman/IvmConfigActions.xml is
< ivm:Match name="hal.block.device" value="/dev/sdb1">
<ivm:Option name="exec" value="/home/dpf/import_pix.sh" />
</ivm:Match>
I can confirm the mount failure via the /etc/mtab - there is no last line about sdb1, until after I select (click on) the VFAT volume in the Dolphin left vertical banner.
Once the sd1 drive gets mounted (by the above procedure), I can issue the sync | umount /dev/sdb1 and last line of the mtab file (referencing sdb1) disappears
I think ivman is not mounting your drive.
Also, without a custom udev rule your usb stick may get different sd* devices (depending on how many are already plugged in: sdb, sdc, etc). Of course this might not be a problem if the stick is the only mass storage usb device ever used as it should then get the first available (probably sdb).
I used ivman with fluxbox way back when HAL/udev was cool & new and it will automount usb mass storage devices, so I think maybe it needs some configuration. I didn’t see anything in that actions file you mentioned that would tell ivman to mount /dev/sdb1 anywhere particularly.
I’d start by making ivman mount your device when it is plugged in, then sort the script exec. If its mounted, it will be in /etc/mtab and also show with df.
Maybe your DE (KDE/Gnome?) is interfering with ivman working? Perhaps it has its own automount mechanism which is somehow blocking ivman?
Have you looked at this: http://productivelinux.com/2009/01/31/how-to-automatically-mount-usb-drives-with-rox-filer-ivman-and-pmount/
You can ignore rox-filer, but it looks kinda similar to what I read to get mine working (ages ago)
Sorry to double-post. I also found this (for gentoo, but might offer some pointers):
USB still not mounting. Once again, I’ve completely removed the/etc/udev/rules.d/30-usb-action.rules
linux-dux0:/home/dpf # cat /etc/ivman/IvmConfigActions.xml
<?xml version=“1.0” encoding=“UTF-8”?>
<ivm:ActionsConfig version=“0.2” xmlns:ivm=“http://www.eikke.com/ivm”>
<!-- perform these rules on devices already attached when Ivman starts →
<ivm:Option name=“checkOnInit” value=“true” />
<!-- try to mount any mountable volume at all -->
<ivm:Match name="ivm.mountable" value="true">
<ivm:Option name="mount" value="true" />
</ivm:Match>
<!-- import pix from USB mem_stick -->
< ivm:Match name="hal.block.device" value="/dev/sdb1">
<ivm:Option name="exec" value="/home/dpf/import_pix.sh" />
</ivm:Match>
-->
</ivm:ActionsConfig
linux-dux0:/home/dpf #
**
the PC booted up with the USB inserted**
linux-dux0:/home/dpf # rcivman status
Checking for service ivman running
**
removed USB**
linux-dux0:/home/dpf # rcivman status
Checking for service ivman running
**
re-inserted USB**
linux-dux0:/home/dpf # rcivman status
Checking for service ivman unused
mtab remains unchanged throughout
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
linux-dux0:/home/dpf #
and here’s the last portion of DMESG output:
usb 1-1.3: new full speed USB device using uhci_hcd and address 6
usb 1-1.3: configuration #1 chosen from 1 choice
scsi3 : SCSI emulation for USB Mass Storage devices
usb 1-1.3: New USB device found, idVendor=0781, idProduct=5406
usb 1-1.3: New USB device strings: Mfr=1, Product=2, SerialNumber=3
usb 1-1.3: Product: U3 Cruzer Micro
usb 1-1.3: Manufacturer: SanDisk
usb 1-1.3: SerialNumber: 2444100EE500C771
usb-storage: device found at 6
usb-storage: waiting for device to settle before scanning
ivman[2932]: segfault at c ip 0804c1d4 sp bfa05720 error 4 in ivman[8048000+e000]
scsi 3:0:0:0: Direct-Access SanDisk U3 Cruzer Micro 8.01 PQ: 0 ANSI: 0 CCS
sd 3:0:0:0: [sdb] 3907711 512-byte hardware sectors (2001 MB)
sd 3:0:0:0: [sdb] Write Protect is off
sd 3:0:0:0: [sdb] Mode Sense: 45 00 00 08
sd 3:0:0:0: [sdb] Assuming drive cache: write through
sd 3:0:0:0: [sdb] 3907711 512-byte hardware sectors (2001 MB)
sd 3:0:0:0: [sdb] Write Protect is off
sd 3:0:0:0: [sdb] Mode Sense: 45 00 00 08
sd 3:0:0:0: [sdb] Assuming drive cache: write through
sdb: sdb1
sd 3:0:0:0: [sdb] Attached SCSI removable disk
sd 3:0:0:0: Attached scsi generic sg1 type 0
usb-storage: device scan complete
linux-dux0:/home/dpf #
so, in a nutshell, it looks like the USB is being detected at some level, but the mount is failing
Did you install pmount & modify the hal fdi file like was mentioned in the gentoo article?
I am out of my depth here to be honest. I recall that when I did use it (this is a few years ago) I did manage to get it to automount a usb drive for me. If I recall correctly, I used a combination of ivman & pmount to do it, and there was no other ‘automagic’ drive detection running - I was using fluxbox here.
Have you tried running ivman in the foreground in a console so you can see what it is doing when you un/plug the usb drive?
I know it is possible to do, no help to you there though
ivman includes the halmount program, so you can issue ‘halmount -u’ to properly unmount the USB stick from the command line.
wonder of wonders: it’s WORKING!!
I tried a different script int the /etc/ivman/IvmConfigActions.xml
and it ran that script just fine! The main difference was the directory in which the “test” script lived - it was in /home/dpf/.kde4/Autostart
I re-read this entire thread and picked up an early suggestion to put the script in the /usr/local/bin directory
I wish I had paid closer attention earlier - this solved it!!!
Now my import script is working, the USB is mounting.
thanks to ALL who contributed!!!