在Linux中查找哪个驱动器对应哪个USB大容量存储设备

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3881449/
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-04 23:37:04  来源:igfitidea点击:

Find which drive corresponds to which USB mass storage device in Linux

linuxbashusb-mass-storageusb

提问by Piskvor left the building

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 physicaldevice (e.g. /dev/sdb1-> device in USB port 1; in my case, one device ~ one volume).

我有几个 USB 大容量闪存驱动器连接到 Ubuntu Linux 计算机(Ubuntu 10.04.1,内核 2.6.32-25-386),我需要以编程方式区分它们(如果可能,从 bash,但我不害怕编译) - 我需要找到哪个块设备对应于哪个物理设备(例如/dev/sdb1-> USB 端口 1 中的设备;在我的情况下,一个设备 ~ 一个卷)。

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 端口;它们中的每一个都在系统中显示为 USB 大容量存储设备(如 lsusb 所示),创建为块设备 ( /dev/sdb1) 并由 UUID ( /media/1234-5678)自动挂载。

USB device               block device    mountpoint
USB device in port 2.2 <-> /dev/sdb1   <-> /media/1234-5678

I'm nottrying 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?

不是要找出块设备和挂载点之间的关系;我正在尝试找到块设备和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.

为什么?磁盘上会有一些写入,完成时间不可预测。我需要给操作员一些指示,例如“您现在可以移除端口 2(从左数第二个)中的磁盘”。我已经找到了那个特定机器上哪个物理端口对应哪个端口号,并且从挂载点查找块设备很简单;现在我被困在将逻辑 USB 端口映射到块设备。

I can see the disks with lsusb :

我可以看到带有 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

and I can see them mounted (by their UUID):

我可以看到它们安装(通过它们的 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.

现在,所有驱动器都是来自同一制造商的相同型号,所以我无法区分它们,我不能保证它们会按特定顺序插入。

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/bus/usb/devices(USB 设备列表),但它似乎与我从 lsusb 获得的数据相同 - 我没有看到那里的磁盘映射。

There's also /sys/block/sdband /sys/block/sdb/sdb1(the block device and its first partition; similarly for sdcand sdd), but again, I see no mapping to devices.

还有/sys/block/sdband /sys/block/sdb/sdb1(块设备和它的第一个分区;sdc和 and类似sdd),但同样,我没有看到到设备的映射。

采纳答案by Piskvor left the building

I'm not sure in which kernel version this was implemented, but the /sys/block/*entries are symlinks to the devices.

我不确定这是在哪个内核版本中实现的,但/sys/block/*条目是设备的符号链接。

In other words, /sys/block/sdbsymlinks to a different directory, and its name contains the USB device ID.

换句话说,/sys/block/sdb指向不同目录的符号链接,其名称包含 USB 设备 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---^^^^^

The 1-1.1is 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.

1-1.1是有趣的部分,表示usb1-port 1.device 1。当插入集线器时,会添加另一个级别:1-2.3.1,表示usb1-port 2.port 3.device 1

Pseudocode:

伪代码:

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

Working example script in bash.

bash 中的工作示例脚本

回答by ochrist

回答by genfoch01

Here is how I do it.

这是我如何做到的。

lsusb -vshows all the devices disks get an iserial number take note of them

lsusb -v显示所有设备磁盘都有一个序列号 记下它们

 ls -l /dev/disk | grep [iserial] 

Everything in /dev/diskis a symlink so follow the symlink to see the device.

中的所有内容都是/dev/disk符号链接,因此请按照符号链接查看设备。

回答by Karter

I use the path:

我使用的路径:

/sys/bus/usb/drivers/usb-storage/4-1:1.0/host4/target4:0:0/4:0:0:0/block/sda

so you can see usb bus 4, port 1 is connected with a usb storage /dev/sda

所以你可以看到usb总线4,端口1连接了一个usb存储/dev/sda