| 1 |
#!/bin/ksh |
| 2 |
# |
| 3 |
# Bacula interface to mtx autoloader |
| 4 |
# |
| 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. |
| 8 |
# |
| 9 |
# |
| 10 |
# $Id$ |
| 11 |
# |
| 12 |
# If you set in your Device resource |
| 13 |
# |
| 14 |
# Changer Command = "path-to-this-script/mtx-changer %c %o %S %a %d |
| 15 |
# you will have the following input to this script: |
| 16 |
# |
| 17 |
# mtx-changer "changer-device" "command" "slot" "archive-device" "drive-index" |
| 18 |
# $1 $2 $3 $4 $5 |
| 19 |
# |
| 20 |
# for example: |
| 21 |
# |
| 22 |
# mtx-changer /dev/sg0 load 1 /dev/nst0 0 (on a Linux system) |
| 23 |
# |
| 24 |
# If you need to an offline, refer to the drive as $4 |
| 25 |
# e.g. mt -f $4 offline |
| 26 |
# |
| 27 |
# Many changers need an offline after the unload. Also many |
| 28 |
# changers need a sleep 60 after the mtx load. |
| 29 |
# |
| 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. |
| 33 |
# |
| 34 |
|
| 35 |
MTX=/lysator/bin/mtx |
| 36 |
LOCKDIR=/tmp |
| 37 |
|
| 38 |
TMPDIR=/tmp |
| 39 |
|
| 40 |
make_temp_file() |
| 41 |
{ |
| 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}" |
| 47 |
exit 1 |
| 48 |
fi |
| 49 |
fi |
| 50 |
} |
| 51 |
|
| 52 |
|
| 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 ..." |
| 57 |
exit 1 |
| 58 |
fi |
| 59 |
|
| 60 |
# Setup arguments |
| 61 |
ctl=$1 |
| 62 |
cmd="$2" |
| 63 |
slot=$3 |
| 64 |
device=$4 |
| 65 |
# If drive not given, default to 0 |
| 66 |
if test $# = 5 ; then |
| 67 |
drive=$5 |
| 68 |
else |
| 69 |
drive=0 |
| 70 |
fi |
| 71 |
|
| 72 |
wait_for_drive() { |
| 73 |
while ! mt -f $1 status >/dev/null 2>/dev/null; do |
| 74 |
# echo "Device $1 - not ready, retrying..." |
| 75 |
sleep 5 |
| 76 |
done |
| 77 |
} |
| 78 |
|
| 79 |
LOCKFILE="${LOCKDIR}/mtx-changer:`echo $ctl | tr / _`" |
| 80 |
|
| 81 |
changer_lock() { |
| 82 |
make_temp_file lock |
| 83 |
echo "$$" >${TMPFILE} |
| 84 |
|
| 85 |
while ! ln -n ${TMPFILE} $LOCKFILE 2>/dev/null; do |
| 86 |
echo "$0: changer lock busy, retrying in 30 seconds..." |
| 87 |
sleep 30 |
| 88 |
done |
| 89 |
|
| 90 |
rm ${TMPFILE} |
| 91 |
} |
| 92 |
|
| 93 |
changer_unlock() { |
| 94 |
LOCKPID="`cat $LOCKFILE 2>/dev/null`" |
| 95 |
if [ "$LOCKPID" != $$ ]; then |
| 96 |
echo "$0: Invalid lock file (${LOCKFILE}) - not owned by us!" |
| 97 |
exit 1 |
| 98 |
fi |
| 99 |
rm -f $LOCKFILE |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
# |
| 105 |
# Check for special cases where only 2 arguments are needed, |
| 106 |
# all others are a minimum of 3 |
| 107 |
case $cmd in |
| 108 |
loaded) |
| 109 |
;; |
| 110 |
unload) |
| 111 |
;; |
| 112 |
list) |
| 113 |
;; |
| 114 |
slots) |
| 115 |
;; |
| 116 |
*) |
| 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 ..." |
| 121 |
exit 1 |
| 122 |
fi |
| 123 |
;; |
| 124 |
esac |
| 125 |
|
| 126 |
changer_lock $ctl |
| 127 |
|
| 128 |
case $cmd in |
| 129 |
unload) |
| 130 |
# echo "Doing mtx -f $ctl unload $slot $drive" |
| 131 |
# |
| 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 |
| 136 |
rtn=$? |
| 137 |
else |
| 138 |
${MTX} -f $ctl unload $slot $drive |
| 139 |
rtn=$? |
| 140 |
fi |
| 141 |
;; |
| 142 |
|
| 143 |
load) |
| 144 |
# echo "Doing mtx -f $ctl load $slot $drive" |
| 145 |
${MTX} -f $ctl load $slot $drive |
| 146 |
rtn=$? |
| 147 |
|
| 148 |
wait_for_drive $device |
| 149 |
changer_unlock $ctl |
| 150 |
exit $rtn |
| 151 |
;; |
| 152 |
|
| 153 |
list) |
| 154 |
# echo "Requested list" |
| 155 |
${MTX} -f $ctl status | tr ':=' ' ' | nawk '($1 == "Storage" && $2 == "Element" && $4 == "Full") { printf "%s:%s\n", $3, $6 }' |
| 156 |
rtn=$? |
| 157 |
;; |
| 158 |
|
| 159 |
loaded) |
| 160 |
make_temp_file |
| 161 |
${MTX} -f $ctl status >${TMPFILE} |
| 162 |
rtn=$? |
| 163 |
cat ${TMPFILE} | grep "^Data Transfer Element $drive:Full" | awk "{print \$7}" |
| 164 |
cat ${TMPFILE} | grep "^Data Transfer Element $drive:Empty" | awk "{print 0}" |
| 165 |
rm -f ${TMPFILE} |
| 166 |
changer_unlock $ctl |
| 167 |
exit $rtn |
| 168 |
;; |
| 169 |
|
| 170 |
slots) |
| 171 |
# echo "Request slots" |
| 172 |
${MTX} -f $ctl status | grep " *Storage Changer" | awk "{print \$5}" |
| 173 |
rtn=$? |
| 174 |
;; |
| 175 |
esac |
| 176 |
|
| 177 |
changer_unlock $ctl |
| 178 |
exit $rtn |