Re: Which remvable drive is connected to which USB port

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Edwards, David (JTS)
Date: Wednesday, October 10, 2007 - 9:34 pm

Hi again.

Just a wrap up to this thread.

From what I can see, the only way to do this is to grep through dmesg.
The following script returns the disk attached to a physical usb port.

Ports seem to be named like so:
/dev/usb4 "addr 4".

On my box, this is the 3rd physical port on a 4-port external USB hub.

-------------
#!/bin/sh

if [ $# -lt 2 ]; then
  echo "Usage: $0 usbdev \"usb addr\" (eg: $0 /dev/usb4 \"addr 4\")"
  echo "       Use dmesg when plugging the device to work out which
device"
  echo "       and addr a physical USB port belongs to."
  exit 1
fi

DEV=$1
ADDR=$2

UMASS=`usbdevs -f $DEV -d | grep -A 1 "$ADDR" | tail -1 | grep umass`
if [ -z $UMASS ]; then
  echo Unable to find a mass storage device on $DEV \"$ADDR\"
  exit 1
fi

SCSIBUS=`/sbin/dmesg | grep scsibus | grep $UMASS | tail -1 | awk
'{print $1}'`
if [ -z $SCSIBUS ]; then
  echo Unable to find a scsibus id for $UMASS
  exit 2
fi

USBDISK=`/sbin/dmesg | grep $SCSIBUS | tail -1 | awk '{print $1}'`
if [ -z $USBDISK ]; then
  echo Unable to find a disk attached to $UMASS $SCSIBUS
  exit 3
fi

if [ `/sbin/dmesg | grep $SCSIBUS | tail -1 | grep detached | wc -l` -gt
0 ]; then
  echo Disk $USBDISK has been detached
  exit 4
fi

echo $USBDISK is attached to $SCSIBUS on $UMASS
-----------------

Disk naming seems to be consistent after you first plug the device in.

So:
The first disk plugged into a port (say "addr 2") gets sd1 (if your SATA
disk is sd0)
The second disk plugged into another port (say "addr 3") gets sd2, etc

If you unplug the first and the second and then plug the second in again
first (to the same port of course "addr 3" :-) it still seems to get
sd2.  But I doubt you can rely on this.  Also at boot time, it's unclear
which disk device will be allocated to which physical port.

I'm not happy with the fact that the script uses dmesg output.  dmesg
uses a ring buffer and that can fill very quickly (say if you unplug a
usb disk while something is writing to it) which will break the above
script totally.  However, currently I don't have the time (or probably
the expertise) to go through the source to work out a better way to do
it.

As usual, YMMV..

ciao
dave
---
Dave Edwards
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
OpenBSD 4.2-current | ASUS P5VD2-X and E2140 problems, sebastian.rother, (Mon Oct 8, 8:44 am)
Re: Which remvable drive is connected to which USB port, Edwards, David (JTS), (Wed Oct 10, 9:34 pm)
Re: Which remvable drive is connected to which USB port, Nick Guenther, (Thu Oct 11, 3:31 am)
Re: Which remvable drive is connected to which USB port, Edwards, David (JTS), (Thu Oct 11, 3:31 pm)
Re: : Which remvable drive is connected to which USB port, Raimo Niskanen, (Fri Oct 12, 12:50 am)
Re: : Which remvable drive is connected to which USB port, Otto Moerbeek, (Fri Oct 12, 1:23 am)
Re: : : Which remvable drive is connected to which USB port, Stuart Henderson, (Fri Oct 12, 3:16 am)
Re: : Which remvable drive is connected to which USB port, Edwards, David (JTS), (Sun Oct 14, 4:48 pm)
Re: : Which remvable drive is connected to which USB port, Otto Moerbeek, (Mon Oct 15, 2:16 am)
Re: : Which remvable drive is connected to which USB port, Owain Ainsworth, (Mon Oct 15, 2:47 pm)
Re: : Which remvable drive is connected to which USB port, Edwards, David (JTS), (Mon Oct 15, 8:02 pm)
Re: : Which remvable drive is connected to which USB port, Douglas A. Tutty, (Tue Oct 16, 8:27 am)