3 # Bacula interface to mtx autoloader
7 # If you set in your Device resource
9 # Changer Command = "path-to-this-script/mtx-changer %c %o %S %a %d"
10 # you will have the following input to this script:
12 # So Bacula will always call with all the following arguments, even though
13 # in come cases, not all are used.
15 # mtx-changer "changer-device" "command" "slot" "archive-device" "drive-index"
20 # mtx-changer /dev/sg0 load 1 /dev/nst0 0 (on a Linux system)
22 # will request to load the first cartidge into drive 0, where
23 # the SCSI control channel is /dev/sg0, and the read/write device
28 # unload unload a given slot
29 # load load a given slot
30 # loaded which slot is loaded?
31 # list list Volume names (requires barcode reader)
32 # slots how many slots total?
34 # Slots are numbered from 1 ...
35 # Drives are numbered from 0 ...
38 # If you need to an offline, refer to the drive as $4
39 # e.g. mt -f $4 offline
41 # Many changers need an offline after the unload. Also many
42 # changers need a sleep 60 after the mtx load.
44 # N.B. If you change the script, take care to return either
45 # the mtx exit code or a 0. If the script exits with a non-zero
46 # exit code, Bacula will assume the request failed.
52 # SunOS No Additional Sense
53 # FreeBSD Current Driver State: at rest.
55 # Note Debian has a different mt than the standard Linux version.
56 # When no tape is in the drive it waits 2 minutes.
57 # When a tape is in the drive, it prints user unfriendly output.
58 # Note, with Ubuntu Gusty (8.04), there are two versions of mt,
59 # so we attempt to figure out which one.
65 ready="No Additional Sense"
68 ready="Current Driver State: at rest."
72 if test -f /etc/debian_version ; then
73 mt --version|grep "mt-st"
74 if test $? -eq 1 ; then
84 # to turn on logging, uncomment the following line
85 #touch @working_dir@/mtx.log
87 dbgfile="@working_dir@/mtx.log"
89 if test -f $dbgfile; then
90 echo "`date +\"%Y%m%d-%H:%M:%S\"` $*" >> $dbgfile
96 # Create a temporary file
99 TMPFILE=`mktemp @working_dir@/mtx.XXXXXXXXXX`
100 if test x${TMPFILE} = x; then
101 TMPFILE="@working_dir@/mtx.$$"
102 if test -f ${TMPFILE}; then
103 echo "Temp file security problem on: ${TMPFILE}"
110 # The purpose of this function to wait a maximum
111 # time for the drive. It will
112 # return as soon as the drive is ready, or after
113 # waiting a maximum of 300 seconds.
114 # Note, this is very system dependent, so if you are
115 # not running on Linux, you will probably need to
116 # re-write it, or at least change the grep target.
117 # We've attempted to get the appropriate OS grep targets
118 # in the code at the top of this script.
122 while [ $i -le 300 ]; do # Wait max 300 seconds
123 if mt -f $1 status 2>&1 | grep "${ready}" >/dev/null 2>&1; then
126 debug "Device $1 - not ready, retrying..."
132 # check parameter count on commandline
137 if test $pCount -lt $pCountNeed; then
138 echo "usage: mtx-changer ctl-device command [slot archive-device drive-index]"
139 echo " Insufficient number of arguments given."
140 if test $pCount -lt 2; then
141 echo " Mimimum usage is first two arguments ..."
143 echo " Command expected $pCountNeed arguments"
149 # Check for special cases where only 2 arguments are needed,
150 # all others are a minimum of 5
154 check_parm_count $# 2
157 check_parm_count $# 2
160 check_parm_count $# 5
172 debug "Parms: $ctl $cmd $slot $device $drive"
176 debug "Doing mtx -f $ctl unload $slot $drive"
178 # enable the following line if you need to eject the cartridge
179 # mt -f $device offline
181 ${MTX} -f $ctl unload $slot $drive
185 debug "Doing mtx -f $ctl load $slot $drive"
186 ${MTX} -f $ctl load $slot $drive
189 # Increase the sleep time if you have a slow device
190 # or remove the sleep and add the following:
192 wait_for_drive $device
197 debug "Doing mtx -f $ctl -- to list volumes"
199 # Enable the following if you are using barcodes and need an inventory
200 # ${MTX} -f $ctl inventory
201 ${MTX} -f $ctl status >${TMPFILE}
203 cat ${TMPFILE} | grep " Storage Element [0-9]*:.*Full" | awk "{print \$3 \$4}" | sed "s/Full *\(:VolumeTag=\)*//"
205 # If you have a VXA PacketLoader and the above does not work, try
206 # turning it off and enabling the following line.
207 # cat ${TMPFILE} | grep " *Storage Element [0-9]*:.*Full" | sed "s/ Storage Element //" | sed "s/Full :VolumeTag=//"
209 cat ${TMPFILE} | grep "^Data Transfer Element [0-9]*:Full (Storage Element [0-9]" | awk '{printf "%s:%s\n",$7,$10}'
210 rm -f ${TMPFILE} >/dev/null 2>&1
215 debug "Doing mtx -f $ctl $drive -- to find what is loaded"
217 ${MTX} -f $ctl status >${TMPFILE}
219 cat ${TMPFILE} | grep "^Data Transfer Element $drive:Full" | awk "{print \$7}"
220 cat ${TMPFILE} | grep "^Data Transfer Element $drive:Empty" | awk "{print 0}"
221 rm -f ${TMPFILE} >/dev/null 2>&1
226 debug "Doing mtx -f $ctl -- to get count of slots"
227 ${MTX} -f $ctl status | grep " *Storage Changer" | awk "{print \$5}"