Skip to content

Instantly share code, notes, and snippets.

@007revad
Created April 24, 2023 23:56
Show Gist options
  • Save 007revad/e7ca1c185f593b2d93cccf5bd0ccd0c2 to your computer and use it in GitHub Desktop.
Save 007revad/e7ca1c185f593b2d93cccf5bd0ccd0c2 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# shellcheck disable=SC1083,SC2054,SC2121,SC2207
if [[ $1 == "--debug" ]]; then
set -x
export PS4='`[[ $? == 0 ]] || echo "\e[1;31;40m($?)\e[m\n "`:.$LINENO:'
fi
getdriveinfo() {
# $1 is /sys/block/sata1 etc
# Skip USB drives
usb=$(grep "$(basename -- "$1")" /proc/mounts | grep usb | cut -d" " -f1-2)
if [[ ! $usb ]]; then
# Get drive model and firmware version
hdmodel=$(cat "$1/device/model")
hdmodel=$(printf "%s" "$hdmodel" | xargs) # trim leading and trailing white space
fwrev=$(cat "$1/device/rev")
fwrev=$(printf "%s" "$fwrev" | xargs) # trim leading and trailing white space
if [[ $hdmodel ]] && [[ $fwrev ]]; then
hdlist+=("${hdmodel},${fwrev}")
fi
fi
}
for d in /sys/block/*; do
# $d is /sys/block/sata1 or /sys/block/hd1 etc
case "$(basename -- "${d}")" in
sd*|hd*)
if [[ $d =~ [hs]d[a-z][a-z]?$ ]]; then
# Get drive model and firmware version
getdriveinfo "$d"
fi
;;
sata*|sas*)
if [[ $d =~ (sas|sata)[0-9][0-9]?[0-9]?$ ]]; then
# Get drive model and firmware version
getdriveinfo "$d"
fi
;;
esac
done
# Sort hdlist array into new hdds array to remove duplicates
if [[ ${#hdlist[@]} -gt "0" ]]; then
while IFS= read -r -d '' x; do
hdds+=("$x")
done < <(printf "%s\0" "${hdlist[@]}" | sort -uz)
fi
# Check hdds array isn't empty
if [[ ${#hdds[@]} -eq "0" ]]; then
echo -e "\nERROR No drives found!" && exit 2
else
echo -e "\nHDD/SSD models found: ${#hdds[@]}"
num="0"
while [[ $num -lt "${#hdds[@]}" ]]; do
echo "${hdds[num]}"
num=$((num +1))
done
echo
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment