Last active
May 30, 2024 07:26
-
-
Save KiralyCraft/8ae3cd3548a518c051f02029476f8797 to your computer and use it in GitHub Desktop.
Show which PCI-e devices do block devices belong to
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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