Skip to content

Instantly share code, notes, and snippets.

@tsposato
Last active May 21, 2025 01:45
Show Gist options
  • Save tsposato/2c77e9aeea5ed3d5c5f1f4a712a3a341 to your computer and use it in GitHub Desktop.
Save tsposato/2c77e9aeea5ed3d5c5f1f4a712a3a341 to your computer and use it in GitHub Desktop.
TrueNAS Scale - Disk List Info
#!/bin/bash
# Get zpool status output
zpool_output=$(zpool status)
declare -A entry_to_pool
current_pool=""
# Match only proper GPTIDs (full UUIDs) or valid device names
while IFS= read -r line; do
if [[ $line =~ ^\ *pool:\ (.+)$ ]]; then
current_pool="${BASH_REMATCH[1]}"
elif [[ $line =~ ([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}) ]]; then
entry="${BASH_REMATCH[1]}"
entry_to_pool["$entry"]="$current_pool"
# Match likely device names only (sdX, nvme0n1pY, etc.)
elif [[ $line =~ ([s]d[a-z][0-9]+|nvme[0-9]+n[0-9]+p[0-9]+) ]]; then
entry="${BASH_REMATCH[1]}"
entry_to_pool["$entry"]="$current_pool"
fi
done <<< "$zpool_output"
# Print header
printf "%-40s %-10s %-35s %s\n" "GPTID/PART" "Device" "Serial" "Pool"
# Sort entries by pool
for entry in "${!entry_to_pool[@]}"; do
printf "%s|%s\n" "${entry_to_pool[$entry]}" "$entry"
done | sort | while IFS='|' read -r pool entry; do
by_partuuid="/dev/disk/by-partuuid/$entry"
if [[ -e "$by_partuuid" ]]; then
dev=$(readlink -f "$by_partuuid")
elif [[ -e "/dev/$entry" ]]; then
dev="/dev/$entry"
else
dev=""
fi
if [[ -n "$dev" ]]; then
devname=$(basename "$dev")
serial=$(udevadm info --query=all --name="$dev" | awk -F= '/ID_SERIAL=/ {print $2}' | head -n1)
serial="${serial:-N/A}"
else
devname="N/A"
serial="N/A"
fi
printf "%-40s %-10s %-35s %s\n" "$entry" "$devname" "$serial" "$pool"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment