views:

2385

answers:

2

Hi, I know that Ubuntu (and Fedora) uses Upstart istead of the classical System V init daemon (SysVinit).

I would like to know how to detect when a USB-drive has been inserted, mount it and copy some files to it. I would like Upstart to call my own script for this.

If it is possible I would like Upstart to call the script for a specific USB-drive, so that I would get normal functionality for every USB-drive except my "instant backup" USB-drive.

If Upstart could send the USB-drive identification string as an argument to my script I guess that would be the ideal solution, as I would be able to have the id string in my script and possibly could make the script handle two USB-drives without much change.

And as a side-note, do you know any other system except Upstart which handles USB-drives, network mounted file systems and such in a nice way? (As SysVinit seems not to.)

+5  A: 

upstart doesn't seem to come with "usb device plugged in" signals out of the box. The focus so far has been to do pretty much exactly the same thing as init, and the "cool advertised features" are in the future.

From the Fedora wiki: "...getting Upstart itself in place now, even though it will only be functioning as SysV does now, will allow us to begin a smooth transition toward this model."

Luckily, you can implement the future yourself by having udev run a script to send your custom upstart signal so upstart can call your backup script. You could also have udev call your backup script directly.

udev already has a simple way to run scripts when devices are plugged and unplugged. See rename your usb hard drive's device name with udev rules. On my system, I would have to use udevadm monitor --env instead of the tutorial's udevmonitor --env. After following the tutorial, you would create another udev rule like this one:

echo 'SUBSYSTEM=="block", ID_SERIAL_SHORT=="101A9041C67D182E", \
NAME="myusbdrive", \
RUN+="/my/backup/script $env{NAME}"' > /etc/udev/rules.d/S96-mydrive.rules

Replacing ID_SERIAL_SHORT with your device's actual id, and $env{NAME} with whatever udev environment variable(s) your script needs to find the backup device. You might need to background the script to avoid blocking udev.

If you want to use upstart, you could have your udev rule run /sbin/initctl emit back-it-up VARIABLE=$env{VARIABLE} ... and then write a script in /etc/event.d beginning with the line start on back-it-up.

See also http://stackoverflow.com/questions/469243/how-can-i-listen-for-usb-device-inserted-events-in-linux-in-python for hints on doing the same with DBus. DBus might be more convenient if you want to have the logged in user run a usermode "watch for backup drive" daemon.

joeforker
Thanks for your informative reply! I have a question. Why shouldn't I use Upstart for this? (You didn't give a motivation.)
DeletedAccount
Upstart doesn't seem to provide the "usb drive plugged in" messages, so you could either define your own with udev (or dbus, or ...), or simply have udev call your backup script directly and be compatible with many more Linux systems.
joeforker
I believe that for your use case DBus would be the way to go, as you probably want the backup service to be user-facing. Also, @joforker - great answer!
Guss
Thank you joeforker for your informative answer! And thank you for your added comment Guss! I'll dwelve deeper into this now. :-)
DeletedAccount
@Kent, I was able to automount a drive on insert using dbus by adding 2 or 3 lines to the example from q469243. Fun stuff.
joeforker
I think the upstart solution would be better if instead of emitting the limited `back-it-up VARIABLE=...` event, you could emit a more general `udev SUBSYSTEM=$SUBSYSTEM ID_SERIAL_SHORT=$ID_SERIAL_SHORT` event. Then the upstart start on line becomes `start on udev SUBSYSTEM=block ID_SERIAL_SHORT=101...`
caspin
+1  A: 

In Ubuntu 9.10 and newer Upstart has some udev capabilities through the upstart-udev-bridge service.

#thumbdrive_special.conf
start on block-device-added

task

script
   if [ `blkid $DEV` -eq "YOUR-THUMBDRIVES-UUID" ]; then
      /home/you/bin/thumbdrive_special $DEV
   fi
end script

I love how simple and elegant upstart can be. However, a DBus solution might be better if less elegant. With a DBus solution you could pop up notifications to the user and provide easy user control.

caspin