Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Apologies if this is a duplicate, however.

I have 2 usb mass storage devices, and I need to be able to work out which physical device maps to a block device.

They are not both always present, so I can't just rely on a fixed block device path.

I've tried to determine the device path from lsusb, but that just provides the device info.

Bus 001 Device 016: ID 0781:5406 SanDisk Corp. Cruzer Micro 1/2/4GB Flash Drive
Bus 001 Device 015: ID 4971:ce23 SimpleTech

sudo fdisk -l is not able to read any partition as its a truecrypt encrypted volume.

Is there some way to detect which physical device is mapped to a block device ?

I'm not able to mount the filesystem until I know which is which.

have I approached this problem in the wrong way ?

Any suggestions ?

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

Are the physical devices both the same size? Are they the exact same brand as well?

If they are different sizes, fdisk -l should show you the size of the entire disk in the header along with the block device name, which should help you.

You can also check the output of dmesg. When a USB device is connected, dmesg will display the process it undergoes to bring it online, as well as showing you the block device it assigned. Here's a sample output:

Aug 14 16:35:02 xen kernel: [2526561.710931] usb 5-5: new high speed USB device using ehci_hcd and address 4
Aug 14 16:35:02 xen kernel: [2526561.859909] usb 5-5: configuration #1 chosen from 1 choice
Aug 14 16:35:02 xen kernel: [2526562.138808] usbcore: registered new interface driver libusual
Aug 14 16:35:02 xen kernel: [2526562.162072] Initializing USB Mass Storage driver...
Aug 14 16:35:02 xen kernel: [2526562.163248] scsi4 : SCSI emulation for USB Mass Storage devices
Aug 14 16:35:02 xen kernel: [2526562.163452] usbcore: registered new interface driver usb-storage
Aug 14 16:35:02 xen kernel: [2526562.163455] USB Mass Storage support registered.
Aug 14 16:35:07 xen kernel: [2526567.161157] scsi 4:0:0:0: Direct-Access     WDC WD50 00AAKS-00A7B0         PQ: 0 ANSI: 2 CCS
Aug 14 16:35:07 xen kernel: [2526567.171712] sd 4:0:0:0: [sdb] 976773168 512-byte hardware sectors (500108 MB)
Aug 14 16:35:07 xen kernel: [2526567.172736] sd 4:0:0:0: [sdb] Write Protect is off
Aug 14 16:35:07 xen kernel: [2526567.173733] sd 4:0:0:0: [sdb] 976773168 512-byte hardware sectors (500108 MB)
Aug 14 16:35:07 xen kernel: [2526567.174606] sd 4:0:0:0: [sdb] Write Protect is off
Aug 14 16:35:13 xen kernel: [2526567.174616]  sdb: sdb1
Aug 14 16:35:13 xen kernel: [2526572.854493] sd 4:0:0:0: [sdb] Attached SCSI disk
Aug 14 16:35:13 xen kernel: [2526572.854544] sd 4:0:0:0: Attached scsi generic sg2 type 0

As you can see, the kernel assigned /dev/sdb to the USB device. Hope this helps!

share|improve this answer
    
I think i'll write a small wrapper script around dmesg to provide the most recent mapping for the USB devices. Thankyou for your help –  Bovril Aug 23 '10 at 15:13
add comment

Once you connect a device you can visit /dev/disk and go from there knowing that the symlinks there get created dynamically:

[user@host disk]$ pwd
/dev/disk
[user@host disk]$ ll -R
.:
total 0
drwxr-xr-x 2 root root  80 Jul 30 09:54 by-id
drwxr-xr-x 2 root root  80 Jul 30 09:54 by-label
drwxr-xr-x 2 root root 160 Jul 30 09:54 by-path
drwxr-xr-x 2 root root  80 Jul 30 09:54 by-uuid

./by-id:
total 0
lrwxrwxrwx 1 root root  9 Jul 30 09:54 usb-WD_10EAVS_External_57442D574341553436313337373137 -> ../../sdb
lrwxrwxrwx 1 root root 10 Jul 30 09:54 usb-WD_10EAVS_External_57442D574341553436313337373137-part1 -> ../../sdb1

./by-label:
total 0
lrwxrwxrwx 1 root root 10 Jul 30 09:54 backup -> ../../sdb1
lrwxrwxrwx 1 root root 10 Mar  3 18:51 boot -> ../../sda1

./by-path:
total 0
lrwxrwxrwx 1 root root  9 Jul 30 09:54 pci-0000:00:1d.7-usb-0:6:1.0-scsi-0:0:0:0 -> ../../sdb
lrwxrwxrwx 1 root root 10 Jul 30 09:54 pci-0000:00:1d.7-usb-0:6:1.0-scsi-0:0:0:0-part1 -> ../../sdb1
lrwxrwxrwx 1 root root  9 Mar  3 18:51 pci-0000:00:1f.1-ide-0:0 -> ../../hda
lrwxrwxrwx 1 root root  9 Mar  3 18:51 pci-0000:02:0e.0-scsi-0:2:0:0 -> ../../sda
lrwxrwxrwx 1 root root 10 Mar  3 18:51 pci-0000:02:0e.0-scsi-0:2:0:0-part1 -> ../../sda1
lrwxrwxrwx 1 root root 10 Mar  3 18:51 pci-0000:02:0e.0-scsi-0:2:0:0-part2 -> ../../sda2

./by-uuid:
total 0
lrwxrwxrwx 1 root root 10 Mar  3 18:51 72734cea-d59d-443b-8fdd-3e7a0e2c7731 -> ../../sda1
lrwxrwxrwx 1 root root 10 Jul 30 09:54 efcb8d1e-3f46-4021-bc55-22a85846429b -> ../../sdb1
[user@host disk]$ 

Hope this helps.

share|improve this answer
add comment

You can query sysfs to report the vendor and model of the drive:

$ cat /sys/block/sdd/device/{vendor,model}
DSE     
MicroDrive 1GB  

$ cat /sys/block/sdc/device/{vendor,model}
JetFlash
Transcend 4GB   

If you want to go further than that, you could add udev rules (assuming your linux distro has udev) to map your different drives to different device names, or to provide a different symlink based on the drive model

EG, here has some examples.

I could do something like this to create a /dev/transcend symlink, with partition numbers appended where appropriate, to whatever real device is actually created when I insert my Transcend stick:

KERNEL=="sd*", SUBSYSTEMS=="scsi", ATTRS{model}==\ 
 "Transcend 4GB", SYMLINK+="transcend%n"

Of course, if I put in multiple devices of the same model, there would be a collision, but with a bit of work you can get around that

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.