简体   繁体   中英

How can I detect using Python the insertion of only USBs and hard drives on Ubuntu/Linux?

I'm building a backup program which involves detecting when media available for backup is inserted. I've looked into detecting the insertion of backup media, and I'm going to use the file system watch service inotify on the /media/username directory.

The problem is that I've looked into this directory and there are folders that don't represent any currently available medium. How can I detect the list of currently available mediums (USBs, HDDs) and watch for any future ones? More technically, what are the characteristics of an actively available USB/HDD folder in the /media/username directory?

  1. In Linux, you could use uevents from kernel and start sniffing for "ACTION"="add" Please check the following links:

    http://lwn.net/Articles/242046/

    and Netlink socket: http://www.kernel.org/doc/man-pages/online/pages/man7/netlink.7.html

  2. Or use DBUS/HAL API bindings for Python http://ubuntuforums.org/archive/index.php/t-904706.html

  3. Check dmesg messages and see exactly what was attached and to what partition was mounted

PS: Here is an example (on SO) of how to do it in Python using DBUS binding:

How can I listen for 'usb device inserted' events in Linux, in Python?

UPDATE

How to check if a path is mounted: https://serverfault.com/questions/143084/how-can-i-check-whether-a-volume-is-mounted-where-it-is-supposed-to-be-using-pyt

通过监视/dev/disks/by-label/目录,我可以很容易地检测到它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM