How to match ata4.00 to the apropriate /dev/sdX or actual physical disk?

Solution 1:

  • You should get more information from dmesg:

    dmesg | grep ata
    

    Something like:

    [    2.345126] ata2.00: ATA-8: WDC WD20EARX-00PASB0, 51.0AB51, max UDMA/133
    

    That would be my Wester Digital hard drive.


  • Here's a way if you have multiple drives of one model (when above won't help):

    1. Find a mapping from SCSI host to the ata* ID:

      $ egrep "^[0-9]{1,}" /sys/class/scsi_host/host*/unique_id
      /sys/class/scsi_host/host0/unique_id:1
      /sys/class/scsi_host/host1/unique_id:2
      /sys/class/scsi_host/host2/unique_id:3
      /sys/class/scsi_host/host3/unique_id:4
      
    2. Find a mapping from the SCSI host to the sd* ID:

      $ ls -l /sys/block/sd*
      ... /sys/block/sda -> ../devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda
      ... /sys/block/sdb -> ../devices/pci0000:00/0000:00:1f.2/host1/target1:0:0/1:0:0:0/block/sdb
      

    Now you can link those two results via the host* identifier. So if in that case I'm getting problems with ata2, I'd look for the unique_id 2 → host1, and then which sd* is host1 → sdb.


  • I also tried to come up with a one-liner. No idea if that's robust. You first have to set the ata* as a variable:

    FAIL=ata1
    

    then run:

    echo "$FAIL -> $(ls -l /sys/block/ | grep $(grep "^$(echo $FAIL | cut -c 4-)" /sys/class/scsi_host/host*/unique_id | sed "s/.*\(host[0-9]\{1,\}\).*/\1/") | awk '{print $8}')"
    

    which should return something like:

    ata1 -> sda
    

Solution 2:

In some of my servers I've two or more devices on one scsi_host. I don't know if it is correct but I assumed that it can be distingushed by target and it works for me:

ls -l /sys/block/sd*
lrwxrwxrwx 1 root root 0 2012-02-08 08:15 /sys/block/sda -> ../devices/pci0000:00/0000:00:01.0/0000:01:00.0/host3/target3:0:0/3:0:0:0/block/sda
lrwxrwxrwx 1 root root 0 2012-02-08 08:15 /sys/block/sdb -> ../devices/pci0000:00/0000:00:1f.2/host6/target6:0:0/6:0:0:0/block/sdb
lrwxrwxrwx 1 root root 0 2012-02-08 08:15 /sys/block/sdc -> ../devices/pci0000:00/0000:00:1f.2/host7/target7:0:0/7:0:0:0/block/sdc
lrwxrwxrwx 1 root root 0 2012-02-08 08:15 /sys/block/sdd -> ../devices/pci0000:00/0000:00:1f.2/host8/target8:0:0/8:0:0:0/block/sdd
lrwxrwxrwx 1 root root 0 2012-02-08 08:15 /sys/block/sde -> ../devices/pci0000:00/0000:00:1f.2/host9/target9:0:0/9:0:0:0/block/sde
lrwxrwxrwx 1 root root 0 2012-02-08 08:15 /sys/block/sdf -> ../devices/pci0000:00/0000:00:1f.2/host10/target10:0:0/10:0:0:0/block/sdf
lrwxrwxrwx 1 root root 0 2012-02-08 08:15 /sys/block/sdg -> ../devices/pci0000:00/0000:00:1f.2/host11/target11:0:0/11:0:0:0/block/sdg
lrwxrwxrwx 1 root root 0 2012-02-08 08:15 /sys/block/sdh -> ../devices/pci0000:00/0000:00:1c.4/0000:04:00.0/host13/target13:0:0/13:0:0:0/block/sdh
lrwxrwxrwx 1 root root 0 2012-02-08 08:15 /sys/block/sdi -> ../devices/pci0000:00/0000:00:1c.4/0000:04:00.0/host13/target13:0:1/13:0:1:0/block/sdi

Simple bash script:

#!/bin/bash

#inspired by http://askubuntu.com/questions/64351/how-to-match-ata4-00-to-the-apropriate-dev-sdx-or-actual-physical-disk

for d in /sys/block/sd*
do
  s=`basename $d`
  h=`ls -l $d | egrep -o "host[0-9]+"`
  t=`ls -l $d | egrep -o "target[0-9:]*"`
  a2=`echo $t | egrep -o "[0-9]:[0-9]$" | sed 's/://'`
  a=`cat /sys/class/scsi_host/$h/unique_id`
  echo "$s -> ata$a.$a2"
done

and it's output:

sda -> ata4.00
sdb -> ata7.00
sdc -> ata8.00
sdd -> ata9.00
sde -> ata10.00
sdf -> ata11.00
sdg -> ata12.00
sdh -> ata14.00
sdi -> ata14.01