linux(ubuntu)下怎样检测U盘的插入

zzxap 2011-05-18 09:21:56
rtt
linux(ubuntu)下怎样检测U盘的插入
u盘插入那个文件会发生变化,如何知道U盘是哪个盘符?然后读取它
...全文
3268 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
袁保康 2013-06-20
  • 打赏
  • 举报
回复
引用 26 楼 Bokutake 的回复:
1. check if /proc/scsi/usb-storage-# exist or not 2. if exist. check if /proc/scsi/usb-storage-#/# exist or not 3. if exist. check the file /proc/scsi/usb-storage-#/# if it says Attach: Yes 4. if Yes. the # can be converted to device name: 0 = sda, 1=sdb, 2=sdc 5. But I don't know which USB port it connects to. and you don't need to know also. for example: /proc/scsi/usb-storage-0/0 says Attach: No, then /dev/sda is not attached /proc/scsi/usb-storage-1/1 says Attach: Yes, then it is attached, you can mount it. If you plug in usb storage with brandname A first, the it is usb-storage-0, then you unplug it, but that directory still exists, and now you plug in Brandname B, the storage-1 directory will be created, and storage-0 is still there. 更高级的方法是用libudev访问内核的udev,udev_monitor可以用来监视设备插入/拔出事件。 http://www.kernel.org/pub/linux/utils/kernel/hotplug/libudev/
这个是针对2.4内核的,2.6以后就不能用这个方法了。
yanzi1225627 2012-08-15
  • 打赏
  • 举报
回复
udev是基于sysfs文件系统的,现在大多数arm
开发板都是yaffs2文件系统的,所以hotplug还是最佳选择。整个消息传送是内核---》hotplug-----》udev-----》dbus 你说哪个底层??
qq120848369 2011-05-19
  • 打赏
  • 举报
回复
Linux不支持NTFS,要安装驱动的,FAT32可以识别。
xunxun 2011-05-19
  • 打赏
  • 举报
回复
如果26l的方法失败的话,可以尝试dbus
还是推荐使用udev,dbus较udev底层些。
辰岡墨竹 2011-05-19
  • 打赏
  • 举报
回复
1. check if /proc/scsi/usb-storage-# exist or not
2. if exist. check if /proc/scsi/usb-storage-#/# exist or not
3. if exist. check the file /proc/scsi/usb-storage-#/# if it says Attach: Yes
4. if Yes. the # can be converted to device name:
0 = sda, 1=sdb, 2=sdc
5. But I don't know which USB port it connects to. and you don't need to know also.

for example: /proc/scsi/usb-storage-0/0 says Attach: No, then /dev/sda is not attached
/proc/scsi/usb-storage-1/1 says Attach: Yes, then it is attached, you can mount it.
If you plug in usb storage with brandname A first, the it is usb-storage-0, then you unplug it, but that directory still exists, and now you plug in Brandname B, the storage-1 directory will be created, and storage-0 is still there.

更高级的方法是用libudev访问内核的udev,udev_monitor可以用来监视设备插入/拔出事件。
http://www.kernel.org/pub/linux/utils/kernel/hotplug/libudev/
bobo_包子 2011-05-19
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 qq120848369 的回复:]

C/C++ code
Linux不支持NTFS,要安装驱动的,FAT32可以识别。
[/Quote]

安装个啥就能支持了 具体google
jieao111 2011-05-18
  • 打赏
  • 举报
回复
[Quote=引用楼主 zzxap 的回复:]
rtt
linux(ubuntu)下怎样检测U盘的插入
u盘插入那个文件会发生变化,如何知道U盘是哪个盘符?然后读取它
[/Quote]
其实就是usb的枚举,什么os都差不多
jieao111 2011-05-18
  • 打赏
  • 举报
回复
到sysfs里找找吧,,顺便给给链接http://blog.csdn.net/fudan_abc/category/332148.aspx
zzxap 2011-05-18
  • 打赏
  • 举报
回复
那还是找不到U盘啊
qq120848369 2011-05-18
  • 打赏
  • 举报
回复
fdisk -l 看文件系统是FAT32的。。
zzxap 2011-05-18
  • 打赏
  • 举报
回复
命令: lsusb
Bus 007 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 006 Device 002: ID 0a81:0101 Chesen Electronics Corp. Keyboard
Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 002: ID 093a:2510 Pixart Imaging, Inc. Hama Optical Mouse
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 148f:3370 Ralink Technology, Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

这个里面怎么知道那个是U盘,使用程序判断的,非肉眼
zzxap 2011-05-18
  • 打赏
  • 举报
回复
fdisk -l 查看不了u盘
zzxap 2011-05-18
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 qq120848369 的回复:]
fdisk -l 就可以查看到设备文件名,然后直接挂载到某个目录下就可以了。

我的移动硬盘是NTFS的,系统识别不了。。。
[/Quote]
fdisk -l 列出那么多
如何知道哪个是U盘?
qq120848369 2011-05-18
  • 打赏
  • 举报
回复
fdisk -l 就可以查看到设备文件名,然后直接挂载到某个目录下就可以了。

我的移动硬盘是NTFS的,系统识别不了。。。
zzxap 2011-05-18
  • 打赏
  • 举报
回复
我要查看插入的盘符是什么,然后才能读取里面的文件
qq120848369 2011-05-18
  • 打赏
  • 举报
回复
查看文件系统应该可以查到吧,只是木有挂载点.
zzxap 2011-05-18
  • 打赏
  • 举报
回复
/proc/scsi/usb-storage-0/0 vi这个文件怎么是空的
zzxap 2011-05-18
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 p569354158 的回复:]
用C语言实现吗?
[/Quote]
是的 libusb
lucky-lucky 2011-05-18
  • 打赏
  • 举报
回复
用C语言实现吗?
enyblock 2011-05-18
  • 打赏
  • 举报
回复
命令: lsusb
Bus 007 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 006 Device 002: ID 0a81:0101 Chesen Electronics Corp. Keyboard
Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 002: ID 093a:2510 Pixart Imaging, Inc. Hama Optical Mouse
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 148f:3370 Ralink Technology, Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

这个里面可以查看
加载更多回复(7)

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧