Aug 282021
 

Dealing with a potentially problematic SATA controller, I came across a little issue – which disks were connected to which controller? Not a problem most people would have to deal with but I do have rather a lot of disks. What I wanted was a tool that would list the controllers (lspci) with disks (block devices) shown per controller (lsblk).

I couldn’t find on, so I knocked up a quick and nasty shell script to do the job.

This isn’t a proper product and probably has many bugs (in particular it doesn’t like disks that are members of a volume group), but it works well enough for my use case :-

» ./print-block-tree 
01:00.0 USB controller: Advanced Micro Devices, Inc. [AMD] X399 Series Chipset USB 3.1 xHCI Controller (rev 02)
  sr0:  PIONEER BD-RW_BDR-UD04 41443030303030303030303030303030 1024M
01:00.1 SATA controller: Advanced Micro Devices, Inc. [AMD] X399 Series Chipset SATA Controller (rev 02)
  sdi: 0x5000c50050ada74d ATA ST4000VN000-1H41 Z300H9GD 3.6T
  sdl: 0x500003992be00c53 ATA TOSHIBA_MG04ACA4 39DFK8S4FJKA 3.6T
  sdm: 0x500003992bf8077f ATA TOSHIBA_MG04ACA4 39CIK7DNFJKA 3.6T
  sdn: 0x500a075102fce9c7 ATA C300-CTFDDAC128M 00000000103402FCE9C7 119.2G
  sdo: 0x500003992bb80ede ATA TOSHIBA_MG04ACA4 39CAKCKDFJKA 3.6T
09:00.0 SATA controller: Marvell Technology Group Ltd. 88SE9235 PCIe 2.0 x2 4-port SATA 6 Gb/s Controller (rev 11)
  sdp: 0x5002538f71100d76 ATA Samsung_SSD_870 S5STNG0R101271L 3.6T
  sdq: 0x50000399ec700c31 ATA TOSHIBA_MG04ACA4 30BXKC00FJKA 3.6T
41:00.0 Non-Volatile memory controller: Samsung Electronics Co Ltd NVMe SSD Controller SM981/PM981/PM983
  nvme0n1:  96G
42:00.0 Serial Attached SCSI controller: Broadcom / LSI SAS2308 PCI-Express Fusion-MPT SAS-2 (rev 05)
  sdb: 0x500080dc00b4e2e9 ATA TOSHIBA-TR200 28RB76F7K46S 223.6G
  sdc: 0x500080dc00b4e3f6 ATA TOSHIBA-TR200 28RB76MOK46S 223.6G
  sdd: 0x500080dc009263fa ATA TOSHIBA-TR200 976B607GK46S 223.6G
  sde: 0x500080dc00926416 ATA TOSHIBA-TR200 976B6088K46S 223.6G
  sdf: 0x50025388a09508a9 ATA Samsung_SSD_850 S1SMNSAG216528K 119.2G
  sdg: 0x50025385a01c8379 ATA Samsung_SSD_840 S1ANNSAF214088T 119.2G
  sdh: 0x500080dc009263f4 ATA TOSHIBA-TR200 976B607AK46S 223.6G
  sda: 0x50025388a09508b4 ATA Samsung_SSD_850 S1SMNSAG216534V 119.2G
44:00.3 USB controller: Advanced Micro Devices, Inc. [AMD] Family 17h (Models 00h-0fh) USB 3.0 Host Controller
  sdj:  Generic- USB3.0_CRW_-SD 201404081410 59.5G

The script itself :-

#!/bin/sh
#
# Attempt at printing a "tree" of block devices

controllers=$(ls /dev/disk/by-path | awk -F- '{printf "%s-%s\n", $1, $2}' | uniq)
for c in $controllers
do
  rhs=$(echo ${c} | awk -F- '{print $2}')
  lspci -s ${rhs}
  blockdevices=$(ls -l /dev/disk/by-path/${c}* | grep -v part | awk '{print $NF}' | awk -F/ '{print $NF}' | uniq)
  for b in $blockdevices
  do
    exp=$(lsblk -no WWN,VENDOR,MODEL,SERIAL,SIZE /dev/${b} | head -1 | tr -s " ")
    if [ -n "${exp}" ]
    then
      echo "  ${b}: ${exp}"
    fi
  done
done
The Rower