Skip to content

Instantly share code, notes, and snippets.

@provegard
Created December 8, 2011 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save provegard/1448415 to your computer and use it in GitHub Desktop.
Save provegard/1448415 to your computer and use it in GitHub Desktop.
Lists Linux block devices and for each one the controller it is connected to.
#!/bin/bash
# Lists Linux block devices and for each one the controller
# it is connected to.
set -o pipefail
for i in /sys/block/sd*; do
# Find the path that contains the PCI ID of the controller.
link=$(readlink $i)
# Assume that the PCI ID of the controller is the path part
# right before the /host... part.
parts=($(echo $link | sed -e 's,/host.*,,' | tr "/" "\n"))
# The PCI ID of the controller should now be the last item
# in the array.
pciid=${parts[-1]}
# Get the device description from the PCI ID.
ctrl=$(lspci -s $pciid 2>/dev/null | sed -e 's/.*: //')
# Print the result if lspci suceeeded (it doesn't for a block
# device connected to USB).
test $? -eq 0 && echo $i: $ctrl
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment