查找哪个驱动器对应于哪个Linux的USB大容量存储设备 [英] Find which drive corresponds to which USB mass storage device in Linux
问题描述
我有连接到Ubuntu Linux操作系统的电脑(Ubuntu的10.04.1,内核2.6.32-25-386)几个USB大容量存储闪存驱动器,我需要告诉他们除了编程(从庆典如果可能的话,但我不怕编译任) - 我需要找到哪些块设备对应于物理的设备(例如的/ dev / SDB1
- >设备USB端口1;在我的情况下,一台设备〜一个卷)。
I have several USB mass storage flash drives connected to a Ubuntu Linux computer (Ubuntu 10.04.1, kernel 2.6.32-25-386), and I need to tell them apart programatically (from bash if possible, but I'm not afraid of compiling either) - I need to find which block device corresponds to which physical device (e.g. /dev/sdb1
-> device in USB port 1; in my case, one device ~ one volume).
在换句话说,我知道我有插入USB端口三种硬件设备;在系统作为一个USB大容量存储设备(如见过的lsusb)他们每个人的出现,作为一个块设备创建(的/ dev / SDB1
)和UUID挂载( /媒体/ 1234-5678
)。
In other words, I know that I have three hardware devices plugged into USB ports; each of them shows up in the system as a USB mass storage device (as seen with lsusb), is created as a block device (/dev/sdb1
) and automounted by UUID (/media/1234-5678
).
USB device block device mountpoint
USB device in port 2.2 <-> /dev/sdb1 <-> /media/1234-5678
我的不的试图找到块设备和挂载点之间的关系; 我试图找到块设备和USB设备之间的关系,有没有办法?
I'm not trying to find the relationship between block device and mountpoint; I'm trying to find the relationship between block device and USB device, is there a way?
为什么呢?将有在磁盘上写一些与完成未predictable时间。我需要给经营者一些提示,如你现在可以删除端口2盘(这是左二)。我发现哪个物理端口对应特定的机器上的端口号,并从发现的挂载点块设备简单;现在我被困映射逻辑USB端口块设备。
Why? There will be some writes on the disks, with unpredictable time of completion. I need to give the operator some indication like "you can now remove the disk in port 2 (which is second from the left)". I have found which physical port corresponds to which port number on that specific machine, and finding block devices from mountpoints is simple; now I'm stuck mapping the logical USB ports to block devices.
我可以看到的的lsusb磁盘:
I can see the disks with lsusb :
Bus 001 Device 058: ID 067b:2517 Prolific Technology, Inc. Mass Storage Device
Bus 001 Device 060: ID 067b:2517 Prolific Technology, Inc. Mass Storage Device
Bus 001 Device 061: ID 067b:2517 Prolific Technology, Inc. Mass Storage Device
和我可以看到他们安装(其UUID):
and I can see them mounted (by their UUID):
/dev/sdb1 on /media/BC88-15C4 type vfat
/dev/sdc1 on /media/AE54-65AA type vfat
/dev/sdd1 on /media/58D2-FED1 type vfat
现在,所有的驱动器都来自同一厂家同一型号,所以我不能区分他们,我不能保证他们会以特定的顺序被插入。
Now, all the drives are the same model from the same manufacturer, so I can't distinguish them by that, and I can't guarantee they'll be plugged in a particular order.
我已经找到 / SYS /巴士/ USB /设备
(USB设备的列表),但它似乎是我从的lsusb获得相同的数据 - 我没有看到一个映射磁盘那里。
I have found /sys/bus/usb/devices
(a list of USB devices), but it seems to be the same data that I get from lsusb - I don't see a mapping to disks there.
还有 / SYS /块/ SDB
和 / SYS /块/ SDB / SDB1
(块设备和它的第一个分区;同样,对于 SDC
和 SDD
),但同样,我没有看到映射设备
There's also /sys/block/sdb
and /sys/block/sdb/sdb1
(the block device and its first partition; similarly for sdc
and sdd
), but again, I see no mapping to devices.
推荐答案
我不知道在内核版本这是实现的,但 / SYS /块/ *
项是符号链接到设备。
I'm not sure in which kernel version this was implemented, but the /sys/block/*
entries are symlinks to the devices.
在换句话说, / SYS /块/ SDB
符号链接到不同的目录,它的名字包含了USB设备ID。
In other words, /sys/block/sdb
symlinks to a different directory, and its name contains the USB device ID.
$ file /sys/block/sdb
/sys/block/sdb: symbolic link to `../devices/pci0000:00/0000:00:02.1/usb1/1-1/1-1.1/1-1.1:1.0/host31/target31:0:0/31:0:0:0/block/sdb'
USB version and port here---^^^^^
的 1-1.1
是最有趣的部分,表示 USB1端口1.device 1
。当插入到集线器,另一个层面增加: 1-2.3.1
,表示 USB1端口2.port 3.device 1
。
The 1-1.1
is the interesting part, denoting usb1-port 1.device 1
. When plugged into a hub, another level is added: 1-2.3.1
, denoting usb1-port 2.port 3.device 1
.
伪code:
get partition name # e.g. /dev/sdb1
get disk name # that would be /dev/sdb
get your basename # sdb
see where /sys/block/$your_basename points to # e.g. ../devices/blah/blah/1-2.1/blah
get the longest substring matching "\d-\d+(.\d+)*" # e.g. 1-2.1
that is the device id you want
/sys/bus/usb/devices/$device_id/ has all kinds of information about it
the ID corresponds to hardware USB ports
这篇关于查找哪个驱动器对应于哪个Linux的USB大容量存储设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!