如何在 Linux 中检测 USB 驱动器插入?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8190542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 02:22:31  来源:igfitidea点击:

How do I detect usb drive insertion in Linux?

linuxqt

提问by

I've written an application for an embedded linux project, and I want my application to display a particular menu when the user inserts a USB drive. I'm programming the application in C++ with Qt.

我为嵌入式 linux 项目编写了一个应用程序,我希望我的应用程序在用户插入 USB 驱动器时显示特定菜单。我正在用 Qt 用 C++ 编写应用程序。

My system doesn't have d-bus but it is using udev. It seems to me that udev is the "proper" way to do this detection, but seems complicated.

我的系统没有 d-bus,但它使用的是 udev。在我看来, udev 是进行这种检测的“正确”方式,但似乎很复杂。

Can anyone point me in the right direction to get started with this? Is there a way to do it without udev, and if not, is there a good "getting started" guide for udev I could use? I really don't need much functionality, just a way for my application to be notified when a drive is inserted (and enough info for my app to mount the drive).

任何人都可以指出我正确的方向来开始这个吗?有没有办法在没有 udev 的情况下做到这一点,如果没有,我可以使用 udev 的“入门”指南吗?我真的不需要太多功能,只是在插入驱动器时通知我的应用程序的一种方式(以及我的应用程序安装驱动器的足够信息)。

Thanks

谢谢

Marlon

马龙

采纳答案by mvliet

The section "libudev - Monitoring Interface" of this document http://www.signal11.us/oss/udev/should get you started.
Instead of a while(1) loop and a sleep, just make a function with that stuff and then set up a periodic Qt timer to call it every half second or whatever.

本文档http://www.signal11.us/oss/udev/ 的“libudev - 监控接口”部分 应该可以帮助您入门。
而不是 while(1) 循环和睡眠,只需使用这些东西创建一个函数,然后设置一个周期性的 Qt 计时器以每半秒或其他时间调用它。