1#!/bin/sh
2#
3# Bacula interface to mtx autoloader
4#
5#   Created  JAN/23/02 by Ludwig Jaffe
6#
7#  Works with the HP A4853 DLT Library
8#         and the Storagetek Timberwolf 9730 DLT Library
9#
10#TAPEDRIVE0 holds the device/name of your 1st and only DLT drive (Bacula supports only 1 drive currently)
11#
12#Read TAPEDRIVE from command line parameters
13
14TMPDIR=/tmp
15
16make_temp_file()
17{
18  TMPFILE=`mktemp ${TMPDIR}/mtx$1.XXXXXXXXXX 2> /dev/null`
19  if test $? -ne 0 || test x${TMPFILE} = x; then
20     TMPFILE="${TMPDIR}/mtx$1.$$"
21     if test -f ${TMPFILE}; then
22        echo "ERROR: Temp file security problem on: ${TMPFILE}"
23        exit 1
24     fi
25  fi
26}
27
28
29
30if [ -z "$4" ] ; then
31  TAPEDRIVE0=/dev/st0
32else
33  TAPEDRIVE0=$4
34fi
35
36#Delay in seconds the tape needs to load the tape. Needed to stop bacula from using the tape too early.
37TAPEDELAY=65   #The StorageTek Timberwolf 9730 with DLT7000 needs approx. 50 seconds to load. 65 sec gives safety
38MTXCHVERBOSE=1
39if [ -z "$1" ] ; then
40  echo ""
41  echo "The mtx-changer script for bacula"
42  echo "---------------------------------"
43  echo ""
44  echo "usage: mtx-changer <changer-device> <command> [slot] [devicename of tapedrive]"
45  echo "       mtx-changer"
46  echo ""
47  echo "Valid commands:"
48  echo ""
49  echo "unload          Unloads a tape into the slot"
50  echo "                from where it was loaded."
51  echo "load <slot>     Loads a tape from the slot <slot>"
52  echo "list            Lists full storage slots"
53  echo "loaded          Gives slot from where the tape was loaded."
54  echo "                0 means the tape drive is empty."
55  echo "slots           Gives Number of aviable slots."
56  echo ""
57  echo "Example:"
58  echo "  mtx-changer /dev/changer load 1   loads a tape from slot1"
59  echo ""
60  exit
61fi
62
63
64case "$2" in
65   unload)
66#     At first do mt -f /dev/st0 offline to unload the tape because HP A4853 aka Timberwolf9730
67#     refuses to unload the tape from the drive if the DLT streamer did not unloaded it!!!
68#
69      #Check if you want to fool me
70      if [ $MTXCHVERBOSE -eq 1 ] ; then echo "mtx-changer: Checking if drive is loaded before we unload. I Request loaded" ; fi
71      make_temp_file
72      mtx -f $1 status >${TMPFILE}
73      rm -f /tmp/mtxloaded
74      cat ${TMPFILE} | grep "^Data Transfer Element 0:Full" | awk "{print \$7}" > /tmp/mtxloaded
75      rm -f ${TMPFILE}
76      read LOADEDVOL </tmp/mtxloaded
77      if [ -z "$LOADEDVOL" ] ; then
78         LOADEDVOL=0
79         echo "mtx-changer:  *** Don't fool me! *** The Drive $TAPEDRIVE0 is empty."
80         echo ""
81      else
82        if [ $MTXCHVERBOSE -eq 1 ] ; then echo "mtx-changer: Doing mt -f $TAPEDRIVE0 offline to rewind and unload the tape!" ; fi
83        mt -f $TAPEDRIVE0 offline
84        if [ $MTXCHVERBOSE -eq 1 ] ; then echo "mtx-changer: Doing mtx -f $1 $2"  ; fi
85        mtx -f $1 $2
86      fi
87      ;;
88
89   load)
90      #Let's check if drive is loaded before we load it
91      if [ $MTXCHVERBOSE -eq 1 ] ; then echo "mtx-changer: Checking if drive is loaded before we load. I Request loaded" ; fi
92      make_temp_file
93      mtx -f $1 status >${TMPFILE}
94      rm -f /tmp/mtxloaded
95      cat ${TMPFILE}| grep "^Data Transfer Element 0:Full" | awk "{print \$7}" > /tmp/mtxloaded
96      rm -f ${TMPFILE}
97      read LOADEDVOL </tmp/mtxloaded
98      if [ -z "$LOADEDVOL" ] ; then
99         LOADEDVOL=0
100         echo "mtx-changer: The Drive $TAPEDRIVE0 is empty."
101      else
102         #Check if you want to fool me
103         if [ $LOADEDVOL -eq $3 ] ; then
104           echo "mtx-changer: *** Don't fool me! *** Tape $LOADEDVOL is already in drive $TAPEDRIVE0!"
105           echo ""
106           exit
107         fi
108         echo "mtx-changer: The Drive $TAPEDRIVE0 is loaded with the tape from slot $LOADEDVOL"
109         echo "mtx-changer: Unloading..."
110         if [ $MTXCHVERBOSE -eq 1 ] ; then echo "mtx-changer: Doing mt -f $TAPEDRIVE0 offline to rewind and unload the tape!" ; fi
111         mt -f $TAPEDRIVE0 offline
112         if [ $MTXCHVERBOSE -eq 1 ] ; then echo "mtx-changer: Doing mtx -f $1 unload"  ; fi
113         mtx -f $1 unload
114      fi
115      #rm -f /tmp/mtxloaded
116      #It is now insured that the drive is empty
117      #
118      #Now we can load the drive as desired
119      if [ $MTXCHVERBOSE -eq 1 ] ; then echo "mtx-changer: Doing mtx -f $1 $2 $3" ; fi
120      mtx -f $1 $2 $3
121      if [ $? -eq 0 ] ; then
122        #Wait until the tape is fully loaded in the dlt drive. It takes about 50 seconds. For safety wait 65 seconds
123        echo "Successfully loaded tape $3 into drive $TAPEDRIVE0."
124        echo "Waiting $TAPEDELAY seconds for the drive to fully load the tape ..."
125        sleep $TAPEDELAY
126        echo "$TAPEDELAY seconds are gone. The tape should be fully loaded now."
127        echo "Loading finished." ;
128        exit 0
129      else
130        echo "ERROR loading tape $3 into drive $TAPEDRIVE0. Maybe, slot $3 is empty!"
131        exit $?
132      fi
133      ;;
134
135   list)
136      if [ $MTXCHVERBOSE -eq 1 ] ; then echo "mtx-changer: Requested list"; fi
137#old      mtx -f $1 status | grep "^[     ]*Storage Element [0-9]*:.*Full" | awk "{print \$3}" | sed "s/:.*$/ /g" | tr -d "[\r\n]"
138#from mtx bacula 1.29:
139#     mtx -f $1 status | grep " *Storage Element [0-9]*:.*Full" | awk "{print \$3}" | sed "s/:.*$/ /g" | tr -d "[\r\n]"
140      mtx -f $1 status | grep " *Storage Element [0-9]*:.*Full" | awk "{print \$3 \$4}" | sed "s/Full *\(:VolumeTag=\)*//"
141
142      ;;
143
144   loaded)
145      if [ $MTXCHVERBOSE -eq 1 ] ; then echo "mtx-changer: Request loaded" ; fi
146      make_temp_file
147      mtx -f $1 status >${TMPFILE}
148      cat ${TMPFILE} | grep "^Data Transfer Element 0:Full" | awk "{print \$7}"
149      cat ${TMPFILE} | grep "^Data Transfer Element 0:Empty" | awk "{print 0}"
150      rm -f ${TMPFILE}
151      ;;
152
153   slots)
154      if [ $MTXCHVERBOSE -eq 1 ] ; then echo "mtx-changer: Request slots" ; fi
155      mtx -f $1 status | grep "[  ]Storage Changer" | awk "{print \$5}"
156      ;;
157esac
158