Tugger the SLUGger!SLUG Mailing List Archives

Re: [SLUG] usb flash drives

On Tue, 20 Jul 2004, Shaun Oliver wrote:

> hi,
> I've just bought an emation usb flash drive
> 256 mb
> when one plugs it into the system, where is it usually mounted?
> or rather where abouts would I expect to mount it,
> I have it listed at /dev/sda but I ain't sure if it's 1 2 3 or 4
> any help would be greatly appreciated.

Hi Shaun,

Each one is different... but most are /dev/sda or /dev/sda1.

We have that problem at CSE trying to let students plug in their (much
varying) USB flash drives.

So, here's roughly the steps that are done to find the right place to
mount (assuming you know its /dev/sda[1]):

1) Check /proc/partitions
   If size of sda is smaller than sda1, etc, then you probably want
   /dev/sda, otherwise probably /dev/sda1, BUT
2) some devices have an extended partition there, so if the size of
   /dev/sda1 is "1", then thats probably an extended one and you want

That's actually the easy bit - its trickier to figure out that it is in
fact /dev/sda (i.e. generically, where there could be many devices plugged
in/out). So since I found it hard to find info on this, here is some on
that too (kudos actually to Neil Brown for this stuff):

- Check /proc/scsi/usb-storage-*/* to see which devices are attached, and
  which scsi host they are on.
- Go through /proc/scsi/scsi to find that host and any channel/id/lun info
  for it.
- For all /dev/sd? devices, find any that are on that scsi host
- Some devices (particularly multi-format card readers) provide each card
  reader on a different scsi LUN, so for these to automatically work, you
  may need CONFIG_SCSI_MULTI_LUN=y in the kernel config.
  -> if there are multiple LUNs for the same host, get the user to tell us
     which one they want.
  -> When scanning for scsi host, also check for same scsi lun
- Then apply earlier details to figure out which partition you want, if

So, how do you know which /dev/sd? matches which scsi device (referenced
with host/bus/channel/lun (4 numbers).

We only look at the host and lun, there are 2 ioctl's that will give us
these = 0x5386 for the host, and 0x5382 for the lun. In shell+perl:

 for dev in /dev/sd?; do
   scsihost=$(perl -e '$a="        "; open( F, "<'$dev'") && ioctl( F, 0x5386, $a) && print unpack("L",$a)."\n"' )
   scsilun=$(perl -e '$a="        "; open( F, "<'$dev'") && ioctl( F, 0x5382, $a) && (@l = unpack("CC",$a) ) && print @l[1] . "\n"')
   # check if host and lun are the ones you are looking for


 - Simon

[1] If devfs, it's more like: scsi/host0/bus0/target0/lun0/disc or part[1-4]