简体   繁体   English

Linux:如何将块设备映射到USB设备?

[英]Linux: How to map a blockdevice to a USB-device?

if I plugin a USB memory stick, I see a new folder in /sys/bus/usb/devices ... thus a new USB-device. 如果我插入USB记忆棒,我会在/ sys / bus / usb / devices中看到一个新文件夹...因此是一个新的USB设备。

Also I see a new folder in /sys/block ... thus a new block-device. 此外,我在/ sys / block中看到一个新文件夹...因此是一个新的块设备。

My question is: How can I get a waterproof mapping between those two devices? 我的问题是:如何在这两个设备之间获得防水映射? Means: If I get a new device in /sys/bus/usb/devices, how can I programatically (fi by checking /sys/...) find out which block device is mapped/related to this usb-device and vice-versa?! 意思是:如果我在/ sys / bus / usb / devices中获得了一个新设备,我怎么能以编程方式(通过检查/ sys / ...来找出哪个块设备映射/与这个usb设备相关)反之亦然?

The information in /sys is organized in multiple ways (by driver, by bus, etc.), and there are many symbolic links to go from one hierarchy to another. /sys的信息以多种方式组织(通过驱动程序,通过总线等),并且有许多符号链接可以从一个层次结构转到另一个层次结构。

Case in point (example seen on kernel 2.6.26): starting from the block device in /sys/block/sdc , the symbolic link /sys/block/sdc/device points inside the per-device-type hierarchy. 例证(在内核2.6.26上看到的例子):从/sys/block/sdc的块设备开始,符号链接/sys/block/sdc/device指向每个设备类型层次结构内部。 You can see that it's an USB device because the target of the link is something like 您可以看到它是USB设备,因为链接的目标是类似的

../../devices/pci0000:00/0000:00:1d.7/usb8/8-2/8-2:1.0/host9/target9:0:0/9:0:0:0

Conversely, USB devices are listed in /sys/bus/usb/devices , and we can see that 8-2:1.0 is a disk-like device because /sys/bus/usb/devices/8-2:1.0/driver links to usb-storage . 相反,USB设备列在/sys/bus/usb/devices ,我们可以看到8-2:1.0是类似磁盘的设备,因为/sys/bus/usb/devices/8-2:1.0/driver链接到usb-storage To find out what the associated block device is, it seems we need to go down to the directory /sys/bus/usb/devices/8-2:1.0/host9/target9:0:0/9:0:0:0 which contains a symbolic link block:sdc whose target is /sys/block/sdc . 要找出关联的块设备是什么,似乎我们需要转到目录/sys/bus/usb/devices/8-2:1.0/host9/target9:0:0/9:0:0:0其中包含一个符号链接block:sdc其目标是/sys/block/sdc

ADDED : Caution: the exact structure of /sys changes from kernel version to kernel version. 添加 :注意: /sys的确切结构从内核版本更改为内核版本。 For example, with kernel 2.6.32, /sys/block/sdc/device points directly into the /dev/bus/scsi without going through the USB hop. 例如,对于内核2.6.32, /sys/block/sdc/device直接指向/dev/bus/scsi而不通过USB hop。


A different approach is to call the udevadm info command. 另一种方法是调用udevadm info命令。 udevadm info -p /sys/block/sdc --query=… gives information on a device based on its /sys entry, while udevadm info -n sdc --query=… gives information on the device /dev/sdc . udevadm info -p /sys/block/sdc --query=…基于其/sys条目提供有关设备的udevadm info -n sdc --query=… ,而udevadm info -n sdc --query=…提供有关设备/dev/sdc

The information includes bus information, for example udevadm info -p /sys/block/sdc --query=env shows 该信息包括总线信息,例如udevadm info -p /sys/block/sdc --query=env shows

ID_BUS=usb
ID_PATH=pci-0000:00:1d.7-usb-0:2:1.0-scsi-0:0:0:0

The udev documentation may have more information of interest to you. udev文档可能包含您感兴趣的更多信息。


A final word of caution: there are all kinds of complex cases that may make whatever you do not so waterproof. 最后要注意的是:有各种复杂的情况可能会使你不那么防水。 How will your program deal with a single USB device that is an array of disks that are assigned multiple block devices? 您的程序将如何处理单个USB设备,该设备是分配了多个块设备的磁盘阵列? Conversely, how will your program deal with a RAID array assembled from multiple devices (perhaps some of them USB and some of them not)? 相反,您的程序将如何处理从多个设备组装的RAID阵列(可能其中一些是USB,有些则不是)? Do you care about other removable media types such as Firewire and e-SATA? 您是否关心其他可移动媒体类型,如Firewire和e-SATA? etc. You won't be able to predict all corner cases, so make sure to fail gracefully. 您将无法预测所有角落情况,因此请确保优雅地失败。

As far as I found out, it's possible to access udev information via "libudev" library. 据我所知,可以通过“libudev”库访问udev信息。 There's also a good sample on the net available: http://www.signal11.us/oss/udev/ 网上还有一个很好的样本: http//www.signal11.us/oss/udev/

I was able to modify it to read out all "/dev/sd*" devices and get their Vendor-ID, Product-ID as well as Serial number. 我能够修改它以读出所有“/ dev / sd *”设备,并获得他们的供应商ID,产品ID和序列号。 I think this solution is kernel/linux distribution independant enough. 我认为这个解决方案是内核/ linux分发独立的。 But I still have to verify this. 但我仍然要验证这一点。

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

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