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

后端 未结 4 1302
暗喜
暗喜 2021-01-06 01:14

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

相关标签:
4条回答
  • 2021-01-06 01:45

    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

    0 讨论(0)
  • 2021-01-06 01:45

    Can't you use disk labels? http://ubuntuforums.org/showthread.php?t=322973

    0 讨论(0)
  • 2021-01-06 01:49

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

    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---^^^^^
    

    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.

    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.

    0 讨论(0)
  • 2021-01-06 01:49

    Here is how I do it.

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

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

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

    0 讨论(0)
提交回复
热议问题