Page 1 of 1

Detecting USB hotplug event - maybe with udev?

Posted: Sat Jul 09, 2016 6:48 am
by welshgeekboy
Hi all,

I'm trying to create a setup where you simply plug a USB storage device into the Pi and this initiates a program which copies across files in a specific folder on the Pi to the USB stick. This is aimed at an A+ board on a robot.

The Pi A+ has only one USB port, so I could plug in my wireless mouse and keyboard dongle, or a USB stick. Not both. So I can't control the Pi when the stick is in the port, and I can't access the drive when I'm using the mouse and keyboard.

I have been using a delay in a script, where the script starts running and waits 10s. By that time you have swapped the dongle for the drive, and it copies all the files across. Sometimes it works beautifully, other times it erases all the data from the USB stick. I think it's a problem with unmounting...

As a result I've been looking into other ways of copying data. Something I've noticed is that when you plug in a device with X running, it is automatically mounted (if it's a mass storage device) and a box pops up asking if you want to open the folder to view the files. This only happens when X is running though. So there's obviously SOMETHING on the Pi which is always on the lookout for new devices being plugged in. That same something can also run scripts (eg mounting the device) at the moment of hotplugging.

I have done some research and it looks like the something is udev, which handles devices (not just what's in the USB port) and runs whatever is necessary to get them working with the system. To do this it has rules files, which tell it what to do in the event of a certain device being plugged in. So I could create a udev rule (placed in /etc/udev/rules.d/ ) which ought to be able to run my backup script when I plug in the storage device.

I have played about with the rules file a bit and tried multiple variations of the code to try and get it working. I've looked on the internet and found some examples on how to do what I'm attempting. Nothing has worked so far. What I'm trying to do initially is just get a usable response from udev, so I'm not trying to run my backup code yet, I'm just trying to touch a file. Here's one variation of the rules file:

Code: Select all

ACTION=="add", SUBSYSTEM=="usb, ATTRS{idVendor}=="0781", ATTRS{idProduct}=="755d", RUN+="/usr/bin/udevattempt.sh"
So as far as I know, the file checks a load of stuff to see if the new device matches what device the rule file is actually for, then if it all matches it ought to run udevattempt.sh, which is executable (chmod-ed) and touches a file in /home/pi or /tmp/ (tried both). udevattempt.sh works when I run it manually.

The only thing that happens differently when I plug the stick in is that sometimes the Pi ask for authentication before it can mount the drive. I presume this is a result of my rules file. Possibly the rule doesn't work so the system gets a bit grumpy? :?

So anyway, what I'm asking is if anyone has any experience working with udev, or has tried something similar before and could help resolve my problem. I'm going to carry on looking for info but I figured the Pi community is probably the most reliable source of help! I'm also open to other ways of doing this (without udev) so long as the behaviour is the same (instant copying after hotplugging).

Please don't don't recommend a USB hub. :D

Thanks all!

Re: Detecting USB hotplug event - maybe with udev?

Posted: Sat Jul 09, 2016 7:35 am
by welshgeekboy
Hmm... I may have solved it.

Code: Select all

RUN+="/usr/bin/udevattempt.sh &"
Runs it in the background and creates a file in /tmp/

Here's my current rules file (in /etc/udev/rules.d/ ):

Code: Select all

SUBSYSTEM=="usb", ATTRS{idVendor}=="0781", ATTRS{idProduct}=="557d", RUN+="/usr/bin/udevattempt.sh &"
Found this from another Pi forum post on a similar topic!