Skip to content

Instantly share code, notes, and snippets.

@dapperfu
Created October 28, 2024 21:45
Show Gist options
  • Save dapperfu/0328192d829c5856815f380a4ac8a6bf to your computer and use it in GitHub Desktop.
Save dapperfu/0328192d829c5856815f380a4ac8a6bf to your computer and use it in GitHub Desktop.
Get zfs pool drive serial numbers if imported with '-d /dev/disk/by-id'
#!/bin/bash
# Written entirely by ChatGPT after 3 prompts.
# Check if the pool name was provided
if [ -z "$1" ]; then
echo "Usage: $0 <pool_name>"
exit 1
fi
POOL_NAME="$1"
# Check if the pool exists
if ! zpool list "$POOL_NAME" &> /dev/null; then
echo "Pool '$POOL_NAME' does not exist."
exit 1
fi
# Extract device IDs from zpool status output
DEVICE_IDS=$(zpool status "$POOL_NAME" | awk '/wwn-/ {print $1}')
# Iterate through device IDs and retrieve serial numbers
for DEVICE_ID in $DEVICE_IDS; do
# Construct the full path using /dev/disk/by-id/
DEVICE_PATH="/dev/disk/by-id/$DEVICE_ID"
# Get the serial number for each device
SERIAL=$(udevadm info --query=all --name="$DEVICE_PATH" | grep ID_SERIAL= | cut -d'=' -f2)
# Print the device and its serial number
if [ -n "$SERIAL" ]; then
echo "Device: $DEVICE_ID, Serial Number: $SERIAL"
else
echo "Device: $DEVICE_ID, Serial Number: Not Found"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment