Skip to content

Instantly share code, notes, and snippets.

@lpaseen
Last active February 18, 2024 19:41
Show Gist options
  • Save lpaseen/cc7114ccc5a4c3ab499ac8e659470f4e to your computer and use it in GitHub Desktop.
Save lpaseen/cc7114ccc5a4c3ab499ac8e659470f4e to your computer and use it in GitHub Desktop.
Script to Erase all tapes loaded in a tape loader.
#!/bin/bash
#
# Description:
# Erase all tapes loaded in a tape loader.
# If FULL is given as parameter a full erase is done otherwise it just do a quick one.
# BUGS:
# It should count how many drives and do that part automagically
# but it's hard to pair up drives with loaders when other tape drives exists also.
# Haven't tested FULL
#
# Copyright: Peter Sjöberg <peters-src at techwiz dot ca>
# License: GPL
#
# History:
# 2004/06/15; PS - Added handling of CLN labled cleaning tapes
#
# have to hardcode the values because of possible more then one autoloader and 1 tapes drives
# Put in full scsi target id to not get effected by changes in scsi devices.
CHANGER=/dev/scsi/sgh4-0c0i0l0
TAPE0=/dev/scsi/nsth4-0c0i4l0
TAPE1=/dev/scsi/nsth4-0c0i5l0
TYPE=${1:-Quick}
MTX=mtx
MT=mtst
[ "$TYPE" = "FULL" ] && SLEEP=60 || SLEEP=1
if [ -z "$2" ]
then
#wait for changer to be ready
retry=120
until $MTX -f $CHANGER status &>/dev/null
do
let stillwaiting=$retry%10
[ $stillwaiting -eq 0 ] && echo Waiting $retry seconds for changer to be ready
let retry=$retry-1
if [ $retry -gt 0 ]
then
sleep 1
else
echo "Problem reading changer status, aborting"
exit 1
fi
done
mtxinfo=$($MTX -f $CHANGER status 2>&1 |grep -v '=CLN'|grep VolumeTag|wc -l)
if [ "$TYPE" = "FoRCE" ]
then
TYPE="Quick"
mtxinfo=0
fi
if [ $mtxinfo -gt 0 ]
then
echo "Tape barcode labels found, aborting"
exit 2
fi
# Empty the drives (need to know where I'm at and to much work trying to reuse current state)
maxtry=4 # emergency break, don't loop forever if something goes wrong
while [ -n "$($MTX -f $CHANGER status|tr -s ' '|grep -i 'Data Transfer'|grep -iv Empty|head -1|cut -f1 -d:|cut -f4 '-d ')" -a $maxtry -gt 0 ]
do
drive=$($MTX -f $CHANGER status|tr -s ' '|grep -i 'Data Transfer'|grep -iv Empty|head -1|cut -f1 -d:|cut -f4 "-d ")
slot=$($MTX -f $CHANGER status|tr -s ' '|grep -v '=CLN'|grep -i 'Storage Element'|grep -i Empty|head -1|cut -f1 -d:|cut -f4 "-d ")
# $MT -f $TAPEDEV offline
# sleep $STAB
$MTX -f $CHANGER unload $slot $drive
let maxtry=$maxtry-1
done
#
# Should have some break in the while statements
# but with several hours possible wait it's hard
#
tapes=$($MTX -f $CHANGER status|grep 'Storage Element'|grep -v '=CLN'|grep ':Full'|wc -l)
echo $(date +"%Y/%m/%d-%H:%M:%S") Starting to ${TYPE}ly erase $tapes tapes in slot $($MTX -f $CHANGER status|grep 'Storage Element'|grep -v '=CLN'|grep ':Full'|tr ':' ' '|awk '{print $3}')
[ $tapes -lt 1 -o $tapes -gt 99 ] && tapes=1
for slot in $($MTX -f $CHANGER status|grep 'Storage Element'|grep -v '=CLN'|grep ':Full'|tr ':' ' '|awk '{print $3}')
do
# Find an empty drive
drive=$($MTX -f $CHANGER status|tr -s ' '|grep -i 'Data Transfer'|grep -i Empty|head -1|cut -f1 -d:|cut -f4 "-d ")
while [ -z "$drive" ]
do
sleep $SLEEP #
drive=$($MTX -f $CHANGER status|tr -s ' '|grep -i 'Data Transfer'|grep -i Empty|head -1|cut -f1 -d:|cut -f4 "-d ")
done
TAPEDEV=$(eval echo \$TAPE$drive)
echo $(date +"%Y/%m/%d-%H:%M:%S") Loading slot $slot to drive $drive
$MTX -f $CHANGER load $slot $drive
echo $(date +"%Y/%m/%d-%H:%M:%S") $TYPE erase drive $drive
$0 $TYPE $TAPEDEV $CHANGER $slot $drive &
done
# Wait for all to finish
NoDrives=$($MTX -f $CHANGER status|tr -s ' '|grep -i 'Data Transfer'|wc -l)
while [ $($MTX -f $CHANGER status|tr -s ' '|grep -i 'Data Transfer'|grep -i Empty|wc -l) -ne $NoDrives ]
do
sleep 1
done
echo $(date +"%Y/%m/%d-%H:%M:%S") All tapes ${TYPE}ly erased
else
TYPE=$1
TAPEDEV=$2
CHANGER=$3
slot=$4
drive=$5
STAB=2 # How many seconds to wait after commands, needed to get proper state.
while ! $MT -f $TAPEDEV status|grep -q ONLINE
do
sleep $SLEEP
done
# Always write weof so if a tape previous used at lower capacity is readjusted to max
$MT -f $TAPEDEV weof
sleep $STAB
if [ "$TYPE" = "FULL" ]
then
$MT -f $TAPEDEV rewind
sleep $STAB
$MT -f $TAPEDEV erase
sleep $STAB
$MT -f $TAPEDEV rewind
sleep $STAB
$MT -f $TAPEDEV weof
sleep $STAB
$MT -f $TAPEDEV rewind
sleep $STAB
fi
# $MT -f $TAPEDEV offline
# sleep $STAB
$MTX -f $CHANGER unload $slot $drive
sleep $STAB
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment