Skip to content

Instantly share code, notes, and snippets.

@KiralyCraft
Last active May 30, 2024 07:26
Show Gist options
  • Save KiralyCraft/8ae3cd3548a518c051f02029476f8797 to your computer and use it in GitHub Desktop.
Save KiralyCraft/8ae3cd3548a518c051f02029476f8797 to your computer and use it in GitHub Desktop.
Show which PCI-e devices do block devices belong to
#!/bin/bash
# Get a list of all block devices
block_devices=$(lsblk -nd -o NAME)
# Loop through each block device
for device in $block_devices; do
# Run udevadm info and filter the output
pci_info=$(udevadm info --query=all --name=/dev/$device | grep -i pci)
# Check if we found a matching line
if [ ! -z "$pci_info" ]; then
# Extract the line starting with 'P:'
line=$(echo "$pci_info" | grep '^P:')
extracted_path=$(echo "$line" | grep -oP "^P: \/devices\/pci[0-9a-f:]+\/([0-9a-f:.]+\/)+" | sed 's/P: //')
theModel=$(udevadm info --query=all --path=/sys/$extracted_path | grep -i MODEL_FROM_DATABASE | sed 's/E: ID_MODEL_FROM_DATABASE=//')
echo "/dev/"$device" is attached to "$theModel
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment