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 # The purpose of this function to wait a maximum
87 # time for the drive. It will
88 # return as soon as the drive is ready, or after
89 # waiting a maximum of 300 seconds.
90 # Note, this is very system dependent, so if you are
91 # not running on Linux, you will probably need to
92 # re-write it, or at least change the grep target.
93 # We've attempted to get the appropriate OS grep targets
94 # in the code at the top of this script.
98 while [ $i -le 300 ]; do # Wait max 300 seconds
99 if mt -f $1 status 2>&1 | grep "${ready}" >/dev/null 2>&1; then
102 debug "Device $1 - not ready, retrying..."
108 # check parameter count on commandline
113 if test $pCount -lt $pCountNeed; then
114 echo "ERROR: usage: mtx-changer ctl-device command [slot archive-device drive-index]"
115 echo " Insufficient number of arguments given."
116 if test $pCount -lt 2; then
117 echo " Mimimum usage is first two arguments ..."
119 echo " Command expected $pCountNeed arguments"
125 # Check for special cases where only 2 arguments are needed,
126 # all others are a minimum of 5
130 check_parm_count $# 2
133 check_parm_count $# 2
136 check_parm_count $# 4
139 check_parm_count $# 5
151 debug "Parms: $ctl $cmd $slot $device $drive"
155 debug "Doing mtx -f $ctl unload $slot $drive"
157 if test ${offline} -eq 1 ; then
158 mt -f $device offline
160 if test ${offline_sleep} -ne 0 ; then
161 sleep ${offline_sleep}
163 ${MTX} -f $ctl unload $slot $drive
167 debug "Doing mtx -f $ctl load $slot $drive"
168 ${MTX} -f $ctl load $slot $drive
170 if test ${load_sleep} -ne 0 ; then
173 wait_for_drive $device
178 debug "Doing mtx -f $ctl -- to list volumes"
180 if test ${inventory} -ne 0 ; then
181 ${MTX} -f $ctl inventory
183 ${MTX} -f $ctl status >${TMPFILE}
185 if test ${vxa_packetloader} -ne 0 ; then
186 cat ${TMPFILE} | grep " *Storage Element [0-9]*:.*Full" | sed "s/ Storage Element //" | sed "s/Full :VolumeTag=//"
188 cat ${TMPFILE} | grep " Storage Element [0-9]*:.*Full" | awk "{print \$3 \$4}" | sed "s/Full *\(:VolumeTag=\)*//"
190 cat ${TMPFILE} | grep "^Data Transfer Element [0-9]*:Full (Storage Element [0-9]" | awk '{printf "%s:%s\n",$7,$10}'
191 rm -f ${TMPFILE} >/dev/null 2>&1
196 # Drive content: D:Drive num:F:Slot loaded:Volume Name
197 # D:0:F:2:vol2 or D:Drive num:E
202 # S:1:F:vol1 S:Slot num:F:Volume Name
203 # S:2:E or S:Slot num:E
206 # Import/Export tray slots:
207 # I:10:F:vol10 I:Slot num:F:Volume Name
208 # I:11:E or I:Slot num:E
211 debug "Doing mtx -f $ctl -- to list all"
213 if test ${inventory} -ne 0 ; then
214 ${MTX} -f $ctl inventory
216 ${MTX} -f $ctl status >${TMPFILE}
218 # can be converted to awk+sed+cut, contributions are welcome
220 /Data Transfer Element (\d+):Empty/ && print "D:$1:E\n";
221 /Data Transfer Element (\d+):Full \(Storage Element (\d+) Loaded\)(:VolumeTag =\s*(.+))?/ && print "D:$1:F:$2:$4\n";
222 /Storage Element (\d+):Empty/ && print "S:$1:E\n";
223 /Storage Element (\d+):Full( :VolumeTag=(.+))?/ && print "S:$1:F:$3\n";
224 /Storage Element (\d+) IMPORT.EXPORT:Empty/ && print "I:$1:E\n";
225 /Storage Element (\d+) IMPORT.EXPORT:Full( :VolumeTag=(.+))?/ && print "I:$1:F:$3\n";' ${TMPFILE}
226 rm -f ${TMPFILE} >/dev/null 2>&1
232 debug "Doing transfer from $slot to $slotdest"
233 ${MTX} -f $ctl transfer $slot $slotdest
239 debug "Doing mtx -f $ctl $drive -- to find what is loaded"
241 ${MTX} -f $ctl status >${TMPFILE}
243 cat ${TMPFILE} | grep "^Data Transfer Element $drive:Full" | awk "{print \$7}"
244 cat ${TMPFILE} | grep "^Data Transfer Element $drive:Empty" | awk "{print 0}"
245 rm -f ${TMPFILE} >/dev/null 2>&1
250 debug "Doing mtx -f $ctl -- to get count of slots"
251 ${MTX} -f $ctl status | grep " *Storage Changer" | awk "{print \$5}"