简体   繁体   English

如何使用Python检测Ubuntu / Linux上仅USB和硬盘插入?

[英]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. 我已经研究过检测是否插入了备份媒体,并且打算在/media/username目录中使用文件系统监视服务inotify。

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? 如何检测当前可用的介质(USB,HDD)列表,并注意将来的介质? More technically, what are the characteristics of an actively available USB/HDD folder in the /media/username directory? 从技术上讲, /media/username目录中活动可用的USB / HDD文件夹的特征是什么?

  1. In Linux, you could use uevents from kernel and start sniffing for "ACTION"="add" Please check the following links: 在Linux中,您可以使用内核中的uevents并开始嗅探“ ACTION” =“ add”,请检查以下链接:

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

    and Netlink socket: http://www.kernel.org/doc/man-pages/online/pages/man7/netlink.7.html 和Netlink套接字: 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 或为Python使用DBUS / HAL API绑定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 检查dmesg消息,并确切查看已附加的内容和已安装的分区

PS: Here is an example (on SO) of how to do it in Python using DBUS binding: PS:这是一个示例(关于SO),该示例说明如何使用DBUS绑定在Python中执行此操作:

How can I listen for 'usb device inserted' events in Linux, in Python? 如何在Linux和Python中侦听“插入USB设备”事件?

UPDATE 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 如何检查是否安装了路径: https//serverfault.com/questions/143084/how-can-i-check-whether-a-volume-is-mount-where-it-is-supposed-to-be -使用- PYT

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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