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


i have several usb mass storage flash drives connected ubuntu linux computer (ubuntu 10.04.1, kernel 2.6.32-25-386), , need tell them apart programatically (from bash if possible, i'm not afraid of compiling either) - need find block device corresponds physical device (e.g. /dev/sdb1 -> device in usb port 1; in case, 1 device ~ 1 volume).

in other words, know have 3 hardware devices plugged usb ports; each of them shows in system usb mass storage device (as seen lsusb), created block device (/dev/sdb1) , automounted uuid (/media/1234-5678).

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

i'm not trying find relationship between block device , mountpoint; i'm trying find relationship between block device , usb device, there way?

why? there writes on disks, unpredictable time of completion. need give operator indication "you can remove disk in port 2 (which second left)". have found physical port corresponds port number on specific machine, , finding block devices mountpoints simple; i'm stuck mapping logical usb ports block devices.

i can see disks 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 can see them mounted (by 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, drives same model same manufacturer, can't distinguish them that, , can't guarantee they'll plugged in particular order.

i have found /sys/bus/usb/devices (a list of usb devices), seems same data lsusb - don't see mapping disks there.

there's /sys/block/sdb , /sys/block/sdb/sdb1 (the block device , first partition; sdc , sdd), again, see no mapping devices.

i'm not sure in kernel version implemented, /sys/block/* entries symlinks devices.

in other words, /sys/block/sdb symlinks different directory, , name contains usb device id.

$ file /sys/block/sdb /sys/block/sdb: symbolic link `../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 , port here---^^^^^ 

the 1-1.1 interesting part, denoting usb1-port 1.device 1. when plugged hub, level added: 1-2.3.1, denoting usb1-port 2.port 3.device 1.

pseudocode:

get partition name # e.g. /dev/sdb1 disk name # /dev/sdb basename # sdb see /sys/block/$your_basename points # e.g. ../devices/blah/blah/1-2.1/blah longest substring matching "\d-\d+(.\d+)*"  # e.g. 1-2.1 device id want /sys/bus/usb/devices/$device_id/ has kinds of information id corresponds hardware usb ports 

working example script in bash.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -