tags:

views:

375

answers:

5

Hello,

How can I detect when a flash drive is plugged in? I'm using a bare Debian installation, without any GUI and want to be notified in my Python script when a new flash drive appears... I know that D-BUS distributes such information, but i dont want to use D-BUS. Is there a more bare access to that information? Shouldn't that be available under /proc or /sys? How can I "connect" to that source?

Bye falstaff

A: 

/proc/partitions shows all the partitions known to the kernel.

Kimvais
Yes, but how does this help detect the insertion event? Especially as not all Linux distributions auto-mount by default (though most do).
sleske
Even unmounted partitions show on /proc/partitions - you can poll the file. However, Peter's answer (udev) is probably better since udev is there unless falstaff is using a 2.4 series kernel
Kimvais
That's true, didn't think of it. udev is still a better solution usually, but if you have to make do with polling, that should work.
sleske
+8  A: 

All mayor Linux distros include udev, that allows you to write custom scripts on hardware events.

PeterMmm
A: 

When an USB device is plugged in syslog writes messages concerning this to /var/log/messages. The "dmesg" command shows this log. You can check near the end of the log to see which channel the device was attached to, it is usually /dev/sd(letter)(number) depending on the partitions and number of serial disks plugged into the system.

whatnick
But for this to work you'd have to poll dmesg, which is very ugly (polling eats performance and interferes with power management).
sleske
I do mention it attaching to /dev/sd** ... Well anyway lots of polling goes on. The previous one mentions some polling too.
whatnick
@whatnick: Yes, that's why downvoted it at first :-/.
sleske
+4  A: 

You can read uevents from kernel via a Netlink socket and look for events where "ACTION" is "add" (you can also watch if a partition from a device was mounted or unmounted, or if a device is being removed). That's as close to the source of events as one can get in user space. As far as I know, this is how udev detects inserted removable media.

But probably using D-Bus/HAL API via Python bingings will be much easier (no uevents data parsing, etc). Not sure why you are opposed to this. Since you are using Python, I suspect that resources are not really the issue.

Alex B
Thanks! I try your or PeterMmm's approach, im not yet sure if im going to use udev... Well, i use python to programm a prototyp on my machine, which havent any resource issues, but the destination is an embedded device, where i dont want to install all that D-Bus/HAL stuff...
falstaff
+3  A: 

If you are targetting an embedded device, then you can run mdev instead of udev. Then you can write mdev rules that are quite simple and triggers a script.

Of course you are not directly monitoring from your script, mdev is doing it, but you can launch any command. You can probably do the same thing with udev, but it always looked quite complicated to me.

shodanex
+1 for picking up on the embedded target.
whatnick