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?
33 # listall list all info
36 # Slots are numbered from 1 ...
37 # Drives are numbered from 0 ...
40 # If you need to an offline, refer to the drive as $4
41 # e.g. mt -f $4 offline
43 # Many changers need an offline after the unload. Also many
44 # changers need a sleep 60 after the mtx load.
46 # N.B. If you change the script, take care to return either
47 # the mtx exit code or a 0. If the script exits with a non-zero
48 # exit code, Bacula will assume the request failed.
51 # source our conf file
52 . @scriptdir@/mtx-changer.conf
56 if test ${debug_log} -ne 0 ; then
57 touch @working_dir@/mtx.log
59 dbgfile="@working_dir@/mtx.log"
61 if test -f $dbgfile; then
62 echo "`date +\"%Y%m%d-%H:%M:%S\"` $*" >> $dbgfile
68 # Create a temporary file
71 TMPFILE=`mktemp @working_dir@/mtx.XXXXXXXXXX`
72 if test x${TMPFILE} = x; then
73 TMPFILE="@working_dir@/mtx.$$"
74 if test -f ${TMPFILE}; then
75 echo "Temp file security problem on: ${TMPFILE}"
82 # The purpose of this function to wait a maximum
83 # time for the drive. It will
84 # return as soon as the drive is ready, or after
85 # waiting a maximum of 300 seconds.
86 # Note, this is very system dependent, so if you are
87 # not running on Linux, you will probably need to
88 # re-write it, or at least change the grep target.
89 # We've attempted to get the appropriate OS grep targets
90 # in the code at the top of this script.
94 while [ $i -le 300 ]; do # Wait max 300 seconds
95 if mt -f $1 status 2>&1 | grep "${ready}" >/dev/null 2>&1; then
98 debug "Device $1 - not ready, retrying..."
104 # check parameter count on commandline
109 if test $pCount -lt $pCountNeed; then
110 echo "usage: mtx-changer ctl-device command [slot archive-device drive-index]"
111 echo " Insufficient number of arguments given."
112 if test $pCount -lt 2; then
113 echo " Mimimum usage is first two arguments ..."
115 echo " Command expected $pCountNeed arguments"
121 # Check for special cases where only 2 arguments are needed,
122 # all others are a minimum of 5
126 check_parm_count $# 2
129 check_parm_count $# 2
132 check_parm_count $# 4
135 check_parm_count $# 5
147 debug "Parms: $ctl $cmd $slot $device $drive"
151 debug "Doing mtx -f $ctl unload $slot $drive"
153 if test ${offline} -eq 1 ; then
154 mt -f $device offline
156 if test ${offline_sleep} -ne 0 ; then
157 sleep ${offline_sleep}
159 ${MTX} -f $ctl unload $slot $drive
163 debug "Doing mtx -f $ctl load $slot $drive"
164 ${MTX} -f $ctl load $slot $drive
166 if test ${load_sleep} -ne 0 ; then
169 wait_for_drive $device
174 debug "Doing mtx -f $ctl -- to list volumes"
176 if test ${inventory} -ne 0 ; then
177 ${MTX} -f $ctl inventory
179 ${MTX} -f $ctl status >${TMPFILE}
181 if test ${vxa_packetloader} -ne 0 ; then
182 cat ${TMPFILE} | grep " *Storage Element [0-9]*:.*Full" | sed "s/ Storage Element //" | sed "s/Full :VolumeTag=//"
184 cat ${TMPFILE} | grep " Storage Element [0-9]*:.*Full" | awk "{print \$3 \$4}" | sed "s/Full *\(:VolumeTag=\)*//"
186 cat ${TMPFILE} | grep "^Data Transfer Element [0-9]*:Full (Storage Element [0-9]" | awk '{printf "%s:%s\n",$7,$10}'
187 rm -f ${TMPFILE} >/dev/null 2>&1
192 # Drive content: D:Drive num:F:Slot loaded:Volume Name
193 # D:0:F:2:vol2 or D:Drive num:E
198 # S:1:F:vol1 S:Slot num:F:Volume Name
199 # S:2:E or S:Slot num:E
202 # Import/Export tray slots:
203 # I:10:F:vol10 I:Slot num:F:Volume Name
204 # I:11:E or I:Slot num:E
207 debug "Doing mtx -f $ctl -- to list all"
209 if test ${inventory} -ne 0 ; then
210 ${MTX} -f $ctl inventory
212 ${MTX} -f $ctl status >${TMPFILE}
214 # can be converted to awk+sed+cut, contributions are welcome
216 /Data Transfer Element (\d+):Empty/ && print "D:$1:E\n";
217 /Data Transfer Element (\d+):Full \(Storage Element (\d+) Loaded\)(:VolumeTag =\s*(.+))?/ && print "D:$1:F:$2:$4\n";
218 /Storage Element (\d+):Empty/ && print "S:$1:E\n";
219 /Storage Element (\d+):Full( :VolumeTag=(.+))?/ && print "S:$1:F:$3\n";
220 /Storage Element (\d+) IMPORT.EXPORT:Empty/ && print "I:$1:E\n";
221 /Storage Element (\d+) IMPORT.EXPORT:Full( :VolumeTag=(.+))?/ && print "I:$1:F:$3\n";' ${TMPFILE}
222 rm -f ${TMPFILE} >/dev/null 2>&1
228 debug "Doing transfer from $slot to $slotdest"
229 ${MTX} -f $ctl transfer $slot $slotdest
235 debug "Doing mtx -f $ctl $drive -- to find what is loaded"
237 ${MTX} -f $ctl status >${TMPFILE}
239 cat ${TMPFILE} | grep "^Data Transfer Element $drive:Full" | awk "{print \$7}"
240 cat ${TMPFILE} | grep "^Data Transfer Element $drive:Empty" | awk "{print 0}"
241 rm -f ${TMPFILE} >/dev/null 2>&1
246 debug "Doing mtx -f $ctl -- to get count of slots"
247 ${MTX} -f $ctl status | grep " *Storage Changer" | awk "{print \$5}"