首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

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

2013-06-26 
linux(ubuntu)下怎样检测U盘的插入本帖最后由 zzxap 于 2011-05-18 09:28:02 编辑rttlinux(ubuntu)下怎样

linux(ubuntu)下怎样检测U盘的插入
本帖最后由 zzxap 于 2011-05-18 09:28:02 编辑 rtt
linux(ubuntu)下怎样检测U盘的插入
u盘插入那个文件会发生变化,如何知道U盘是哪个盘符?然后读取它
[解决办法]
/proc/scsi/usb-storage-0/0
Attached: Yes or No
[解决办法]
命令: 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

这个里面可以查看
[解决办法]
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/

热点排行