3 # Bacula interface to mtx autoloader
5 # This script is not needed with Bacula version 1.38 or later
6 # since the Storage daemon automatically ensures that only one
7 # thread accesses the script at a time.
12 # If you set in your Device resource
14 # Changer Command = "path-to-this-script/mtx-changer %c %o %S %a %d
15 # you will have the following input to this script:
17 # mtx-changer "changer-device" "command" "slot" "archive-device" "drive-index"
22 # mtx-changer /dev/sg0 load 1 /dev/nst0 0 (on a Linux system)
24 # If you need to an offline, refer to the drive as $4
25 # e.g. mt -f $4 offline
27 # Many changers need an offline after the unload. Also many
28 # changers need a sleep 60 after the mtx load.
30 # N.B. If you change the script, take care to return either
31 # the mtx exit code or a 0. If the script exits with a non-zero
32 # exit code, Bacula will assume the request failed.
42 TMPFILE=`mktemp ${TMPDIR}/mtx$1.XXXXXXXXXX 2> /dev/null`
43 if test $? -ne 0 || test x${TMPFILE} = x; then
44 TMPFILE="${TMPDIR}/mtx$1.$$"
45 if test -f ${TMPFILE}; then
46 echo "ERROR: Temp file security problem on: ${TMPFILE}"
53 if test $# -lt 2 ; then
54 echo "usage: mtx-changer ctl-device command slot archive-device drive"
55 echo " Insufficient number of arguments arguments given."
56 echo " Mimimum usage is first two arguments ..."
65 # If drive not given, default to 0
73 while ! mt -f $1 status >/dev/null 2>/dev/null; do
74 # echo "Device $1 - not ready, retrying..."
79 LOCKFILE="${LOCKDIR}/mtx-changer:`echo $ctl | tr / _`"
85 while ! ln -n ${TMPFILE} $LOCKFILE 2>/dev/null; do
86 echo "$0: changer lock busy, retrying in 30 seconds..."
94 LOCKPID="`cat $LOCKFILE 2>/dev/null`"
95 if [ "$LOCKPID" != $$ ]; then
96 echo "$0: Invalid lock file (${LOCKFILE}) - not owned by us!"
105 # Check for special cases where only 2 arguments are needed,
106 # all others are a minimum of 3
117 if test $# -lt 3; then
118 echo "usage: mtx-changer ctl-device command slot archive-device drive"
119 echo " Insufficient number of arguments arguments given."
120 echo " Mimimum usage is first three arguments ..."
130 # echo "Doing mtx -f $ctl unload $slot $drive"
132 # enable the following line if you need to eject the cartridge
133 mt -f $device offline
134 if test x$slot = x; then
135 ${MTX} -f $ctl unload
138 ${MTX} -f $ctl unload $slot $drive
144 # echo "Doing mtx -f $ctl load $slot $drive"
145 ${MTX} -f $ctl load $slot $drive
148 wait_for_drive $device
154 # echo "Requested list"
155 ${MTX} -f $ctl status | tr ':=' ' ' | nawk '($1 == "Storage" && $2 == "Element" && $4 == "Full") { printf "%s:%s\n", $3, $6 }'
161 ${MTX} -f $ctl status >${TMPFILE}
163 cat ${TMPFILE} | grep "^Data Transfer Element $drive:Full" | awk "{print \$7}"
164 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}"