If I connect a physical device, how can I ever know which device file belongs to it?

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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.

Method 3

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

Method 4

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x