13

Say I plug in several USB drives which don't get automatically mounted. How can I find out which device file belongs to which physical device, so I can mount it for example?

I'm running Mac OS X but I rather like an answer that works on all (or at least the most popular) Unix systems. I had this problem with Linux in the past.

14

Using udev:

You can get useful information querying udev (on systems that use it - almost all desktop-type Linuxes for sure). For instance, if you want to know which attached drive is associated with /dev/sdb, you can use:

udevadm info --query=property --name=sdb

It will show you a list of properties of that device, including the serial (ID_SERIAL_SHORT). Having that information, you can look at the output of lsusb -v and find out things like the manufacturer and product name.

A shorter path to do this would be

udevadm info --query=property --name=sdb | grep "\(MODEL_ID\|VENDOR_ID\)"

and see the line with matching $ID_VENDOR_ID:$ID_MODEL_ID in the much shorter output of lsusb.

Another useful option is udevadm monitor. Use it if you'd like know which device node is created at the point of attaching the device. So first run

 udevadm monitor --udev --subsystem-match=block

And then connect the device. You'll see the device names of the detected block devices (disks/partitions) printed at the end of each output line.

A practical example shell function:

Here's a function you can put in your .bashrc (or .zshrc) :

listusbdisks () 
{
    [[ "x$1" == "x-v" ]] && shift && local VERBOSE=-v
    for dsk in ${@-/dev/sd?}
    do
        /sbin/udevadm info --query=path --name="$dsk" | grep --colour=auto -q usb || continue
        echo "===== device $dsk is:"
        ( eval $(/sbin/udevadm info --query=property --name="$dsk" | grep "\(MODEL\|VENDOR\)_ID")
          [ "$ID_VENDOR_ID:$ID_MODEL_ID" == ":" ] && echo "Unknown" || \
            lsusb $VERBOSE -d "$ID_VENDOR_ID:$ID_MODEL_ID"
        )
        grep -q "$dsk" /proc/mounts && echo "----- DEVICE IS MOUNTED ----"
        echo
    done
}

Use it like this :

  • listusbdisks - to recognize all /dev/sdx devices;
  • listusbdisks sdb or listusbdisks /dev/sdb or listusbdisks sdb sdc - to get info about certain devices only;
  • listusbdisks -v [optional devices as above] - to show verbose outputs of lsusb

[Edit]: Added some functionality like querying many devices, checking mounts and control verbosity of lsusb.

  • True enough as far as it goes, but not all systems use udev. – Caleb Aug 4 '11 at 20:22
  • @Caleb True... Actually, lsusb is also an optional program. But both are extremely common. – rozcietrzewiacz Aug 4 '11 at 20:27
  • @rozcietrzewiacz The question is primarily about OSX, but udev is intrinsically specific to Linux. – Gilles Aug 4 '11 at 21:39
  • @Gil Well, true. I've no experience with OSX, so I just updated my answer not to be misleading. – rozcietrzewiacz Aug 4 '11 at 21:52
  • What I generally do in such case is, I plug the device in and immediately run dmesg. But this won't be helpful if device is already plugged in and if there are more devices plugged after the first one. :) – Abhishek A Sep 2 '11 at 13:08
3

There is no universal answer. On Linux the simplest way is probably to just fire up the gnome disk utility and it will show all detected disks, whether they are mounted or not. From the command line, you can consult the output of blkid or udevadm info --export-db.

3

In Mac OS X you can try to use diskutil list.

1

With respect to all Unixes, I don't think this is feasible.

The usual external buses today do not use a fixed numbering scheme for their ports, like IDE did. So, the only remaining data sources for the mapping you need are the drives' manufacturer names, their serial numbers or the partitions' UIDs.

Manufacturer names are not necessarily unique on a given machine (suppose you connect two external drives from the same series - they will identify identically). Serial numbers are not "visible from the outside", as are partition UIDs.

So, about the only universal solution I can think of is to label the physical drives with the serial number and use the udevadm command given in rozcietrzewiacz's answer, if UDEV is available.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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