3 # /bin/sh isn't always compatible so use /bin/bash
5 # Bacula interface to mtx autoloader
9 # If you set in your Device resource
11 # Changer Command = "path-to-this-script/mtx-changer %c %o %S %a %d"
12 # you will have the following input to this script:
14 # mtx-changer "changer-device" "command" "slot" "archive-device" "drive-index"
19 # mtx-changer /dev/sg0 load 1 /dev/nst0 0 (on a Linux system)
21 # If you need to an offline, refer to the drive as $4
22 # e.g. mt -f $4 offline
24 # Many changers need an offline after the unload. Also many
25 # changers need a sleep 60 after the mtx load.
27 # N.B. If you change the script, take care to return either
28 # the mtx exit code or a 0. If the script exits with a non-zero
29 # exit code, Bacula will assume the request failed.
32 # Sun sed/awk etc are not sufficient, working versions are in /usr/xpg4/bin
33 export PATH="/usr/local/bin:/usr/sfw/bin:/usr/xpg4/bin:/usr/bin"
41 TMPFILE=`mktemp ${TMPDIR}/mtx$1.XXXXXXXXXX 2> /dev/null`
42 if test $? -ne 0 || test x${TMPFILE} = x; then
43 TMPFILE="${TMPDIR}/mtx$1.$$"
44 if test -f ${TMPFILE}; then
45 echo "ERROR: Temp file security problem on: ${TMPFILE}"
55 # The purpose of this function to wait a maximum
56 # time for the drive. It will
57 # return as soon as the drive is ready, or after
58 # waiting a maximum of 180 seconds.
59 # Note, this is very system dependent, so if you are
60 # not running on Linux, you will probably need to
63 # If you have a FreeBSD system, you might want to change
64 # the $(seq 180) to $(jot 180) -- tip from Brian McDonald
67 for i in $(seq 180); do # Wait max 180 seconds
68 if ( mt -f $1 status | grep 0x0 ) >/dev/null 2>&1; then
69 #echo "Device $1 READY"
72 #echo "Device $1 - not ready, retry ${i}..."
78 if test $# -lt 2 ; then
79 echo "usage: mtx-changer ctl-device command slot archive-device drive"
80 echo " Insufficient number of arguments arguments given."
81 echo " Mimimum usage is first two arguments ..."
90 # If drive not given, default to 0
98 # Check for special cases where only 2 arguments are needed,
99 # all others are a minimum of 3
110 if test $# -lt 3; then
111 echo "usage: mtx-changer ctl-device command slot archive-device drive"
112 echo " Insufficient number of arguments arguments given."
113 echo " Mimimum usage is first three arguments ..."
122 # echo "Doing mtx -f $ctl unload $slot $drive"
124 # enable the following line if you need to eject the cartridge
125 #mt -f $device offline
126 mt -f $device rewoffl
127 if test x$slot = x; then
128 ${MTX} -f $ctl unload
130 ${MTX} -f $ctl unload $slot $drive
135 # echo "Doing mtx -f $ctl load $slot $drive"
136 ${MTX} -f $ctl load $slot $drive
139 # Increase the sleep time if you have a slow device
140 # or remove the sleep and add the following:
142 wait_for_drive $device
147 # echo "Requested list"
148 # Some tape changers lose track of their inventory (well, mine does) so
149 # do one before trying to get a status out of it.
150 ${MTX} -f $ctl inventory
151 ${MTX} -f $ctl status | grep " *Storage Element [0-9]*:.*Full" | awk "{print \$3 \$4}" | sed "s/Full *\(:VolumeTag=\)*//"
152 # Comment out the previous line and add a line here
153 # to print "fake" barcodes.
155 # If you have a VXA PacketLoader and the above does not work, try
156 # turning it off and enabling the following line.
157 # ${MTX} -f $ctl status | grep " *Storage Element [0-9]*:.*Full" | sed "s/*Storage Element //" | sed "s/Full :VolumeTag=//"
162 ${MTX} -f $ctl status >${TMPFILE}
164 cat ${TMPFILE} | grep "^Data Transfer Element $drive:Full" | awk "{print \$7}"
165 cat ${TMPFILE} | grep "^Data Transfer Element $drive:Empty" | awk "{print 0}"
171 # echo "Request slots"
172 ${MTX} -f $ctl status | grep " *Storage Changer" | awk "{print \$5}"