How to retrieve tape drive serial numbers with sg_inq

This small piece of code let you retrieve what serial numbers a tape has has. Quite convenient when configuring or replacing a lot of tape drives

#!/usr/bin/ksh
OS=`uname`
if [ $OS != “Linux” ]
then
echo “Script will not work on non-linux variants”
exit 1
fi
if [ ! -f /usr/bin/sg_inq ]
then
echo “sg_inq command not found. Do you have the sg3_util package installed ?”
exit 1
fi
tpconfig -l | grep drive | grep “/dev/n” | awk ‘{ print $8 ” “$9 }’ | while read NAME DEVICE
do
echo -n “Drive $NAME, $DEVICE”
/usr/bin/sg_inq $DEVICE | grep “serial”
done

Output will look like this:
# ./get_device_serial_number

Drive 0315-2F, /dev/nst5 Unit serial number: HU19477MA8
Drive 0011-2F, /dev/nst0 Unit serial number: HU10623E8J
Drive 0116-2F, /dev/nst4 Unit serial number: HU1052790W
Drive 10115-TN, /dev/nst1sg_inq: error opening file: /dev/nst1: Device or resource busy

A “Device or resource busy” mean the tape drives is busy (e.g using SSO) – this will prevent sg_inq from reading the tape drive serial number. Wait for current tape operation to stop and re-run the script.

(Visited 680 times, 1 visits today)