3 # Bacula interface to mtx autoloader
5 # If you set in your Device resource
7 # Changer Command = "path-to-this-script/mtx-changer %c %o %S %a %d"
8 # you will have the following input to this script:
10 # So Bacula will always call with all the following arguments, even though
11 # in come cases, not all are used.
13 # mtx-changer "changer-device" "command" "slot" "archive-device" "drive-index"
18 # mtx-changer /dev/sg0 load 1 /dev/nst0 0 (on a Linux system)
20 # will request to load the first cartidge into drive 0, where
21 # the SCSI control channel is /dev/sg0, and the read/write device
26 # unload unload a given slot
27 # load load a given slot
28 # loaded which slot is loaded?
29 # list list Volume names (requires barcode reader)
30 # slots how many slots total?
31 # listall list all info
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.
49 # source our conf file
50 if test ! -f @scriptdir@/mtx-changer.conf ; then
51 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
52 echo "ERROR: @scriptdir@/mtx-changer.conf file not found!!!!"
53 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
56 . @scriptdir@/mtx-changer.conf
60 if test ${debug_log} -ne 0 ; then
61 touch @working_dir@/mtx.log
63 dbgfile="@working_dir@/mtx.log"
65 if test -f $dbgfile; then
66 echo "`date +\"%Y%m%d-%H:%M:%S\"` $*" >> $dbgfile
72 # Create a temporary file
75 TMPFILE=`mktemp @working_dir@/mtx.XXXXXXXXXX`
76 if test x${TMPFILE} = x; then
77 TMPFILE="@working_dir@/mtx.$$"
78 if test -f ${TMPFILE}; then
79 echo "ERROR: Temp file security problem on: ${TMPFILE}"
86 # Create a temporary file for stderr
88 # Note, this file is used because sometime mtx emits
89 # unexpected error messages followed by the output
90 # expected during success.
91 # So we separate STDOUT and STDERR in
92 # certain of the mtx commands. The contents of STDERR
93 # is then printed after the STDOUT produced by mtx
94 # thus we sometimes get better changer results.
97 ERRFILE=`mktemp @working_dir@/mtx.err.XXXXXXXXXX`
98 if test x${ERRFILE} = x; then
99 ERRFILE="@working_dir@/mtx.err.$$"
100 if test -f ${ERRFILE}; then
101 echo "ERROR: Temp file security problem on: ${ERRFILE}"
109 # The purpose of this function to wait a maximum
110 # time for the drive. It will
111 # return as soon as the drive is ready, or after
112 # waiting a maximum of 300 seconds.
113 # Note, this is very system dependent, so if you are
114 # not running on Linux, you will probably need to
115 # re-write it, or at least change the grep target.
116 # We've attempted to get the appropriate OS grep targets
117 # in the code at the top of this script.
121 while [ $i -le 300 ]; do # Wait max 300 seconds
122 if mt -f $1 status 2>&1 | grep "${ready}" >/dev/null 2>&1; then
125 debug "Device $1 - not ready, retrying..."
131 # check parameter count on commandline
136 if test $pCount -lt $pCountNeed; then
137 echo "ERROR: usage: mtx-changer ctl-device command [slot archive-device drive-index]"
138 echo " Insufficient number of arguments given."
139 if test $pCount -lt 2; then
140 echo " Mimimum usage is first two arguments ..."
142 echo " Command expected $pCountNeed arguments"
148 # Check for special cases where only 2 arguments are needed,
149 # all others are a minimum of 5
153 check_parm_count $# 2
156 check_parm_count $# 2
159 check_parm_count $# 4
162 check_parm_count $# 5
174 debug "Parms: $ctl $cmd $slot $device $drive"
178 debug "Doing mtx -f $ctl unload $slot $drive"
180 if test ${offline} -eq 1 ; then
181 mt -f $device offline
183 if test ${offline_sleep} -ne 0 ; then
184 sleep ${offline_sleep}
187 ${MTX} -f $ctl unload $slot $drive 2>${ERRFILE}
190 rm -f ${ERRFILE} >/dev/null 2>&1
195 debug "Doing mtx -f $ctl load $slot $drive"
197 ${MTX} -f $ctl load $slot $drive 2>${ERRFILE}
199 if test ${load_sleep} -ne 0 ; then
202 wait_for_drive $device
204 rm -f ${ERRFILE} >/dev/null 2>&1
209 debug "Doing mtx -f $ctl -- to list volumes"
211 if test ${inventory} -ne 0 ; then
212 ${MTX} -f $ctl inventory
214 ${MTX} -f $ctl status >${TMPFILE}
216 if test ${vxa_packetloader} -ne 0 ; then
217 cat ${TMPFILE} | grep " *Storage Element [0-9]*:.*Full" | sed "s/ Storage Element //" | sed "s/Full :VolumeTag=//"
219 cat ${TMPFILE} | grep " Storage Element [0-9]*:.*Full" | awk "{print \$3 \$4}" | sed "s/Full *\(:VolumeTag=\)*//"
221 cat ${TMPFILE} | grep "^Data Transfer Element [0-9]*:Full (Storage Element [0-9]" | awk '{printf "%s:%s\n",$7,$10}'
222 rm -f ${TMPFILE} >/dev/null 2>&1
227 # Drive content: D:Drive num:F:Slot loaded:Volume Name
228 # D:0:F:2:vol2 or D:Drive num:E
233 # S:1:F:vol1 S:Slot num:F:Volume Name
234 # S:2:E or S:Slot num:E
237 # Import/Export tray slots:
238 # I:10:F:vol10 I:Slot num:F:Volume Name
239 # I:11:E or I:Slot num:E
242 debug "Doing mtx -f $ctl -- to list all"
244 if test ${inventory} -ne 0 ; then
245 ${MTX} -f $ctl inventory
247 ${MTX} -f $ctl status >${TMPFILE}
249 # can be converted to awk+sed+cut, see below
251 /Data Transfer Element (\d+):Empty/ && print "D:$1:E\n";
252 /Data Transfer Element (\d+):Full \(Storage Element (\d+) Loaded\)(:VolumeTag =\s*(.+))?/ && print "D:$1:F:$2:$4\n";
253 /Storage Element (\d+):Empty/ && print "S:$1:E\n";
254 /Storage Element (\d+):Full( :VolumeTag=(.+))?/ && print "S:$1:F:$3\n";
255 /Storage Element (\d+) IMPORT.EXPORT:Empty/ && print "I:$1:E\n";
256 /Storage Element (\d+) IMPORT.EXPORT:Full( :VolumeTag=(.+))?/ && print "I:$1:F:$3\n";' ${TMPFILE}
257 # If perl isn't installed, you can use by those commands
258 #cat ${TMPFILE} | grep "Data Transfer Element" | awk "{print \"D:\"\$4 \$7 \$9 \$10}" | sed "s/=/:/" | sed "s/Full/F:/" | sed "s/Empty/E/"
259 #cat ${TMPFILE} | grep -v "Data Transfer Element" | grep "Storage Element" | grep -v "IMPORT/EXPORT" | awk "{print \"S:\"\$3 \$4 \$5}" | sed "s/IMPORT\/EXPORT//" | sed "s/Full *:VolumeTag=/F:/" | sed "s/Empty/E/"
260 #cat ${TMPFILE} | grep -v "Data Transfer Element" | grep "Storage Element" | grep "IMPORT/EXPORT" | awk "{print \"I:\"\$3 \$4 \$5}" | sed "s/IMPORT\/EXPORT//" | sed "s/Full *:VolumeTag=/F:/" | sed "s/Empty/E/"
262 rm -f ${TMPFILE} >/dev/null 2>&1
268 debug "Doing transfer from $slot to $slotdest"
269 ${MTX} -f $ctl transfer $slot $slotdest
275 debug "Doing mtx -f $ctl $drive -- to find what is loaded"
277 ${MTX} -f $ctl status >${TMPFILE}
279 cat ${TMPFILE} | grep "^Data Transfer Element $drive:Full" | awk "{print \$7}"
280 cat ${TMPFILE} | grep "^Data Transfer Element $drive:Empty" | awk "{print 0}"
281 rm -f ${TMPFILE} >/dev/null 2>&1
286 debug "Doing mtx -f $ctl -- to get count of slots"
287 ${MTX} -f $ctl status | grep " *Storage Changer" | awk "{print \$5}"