]> git.sur5r.net Git - bacula/bacula/blob - bacula/scripts/mtx-changer.in
Tweak some comments and formatting.
[bacula/bacula] / bacula / scripts / mtx-changer.in
1 #!/bin/sh
2 #
3 # Bacula interface to mtx autoloader
4 #
5 #  If you set in your Device resource
6 #
7 #  Changer Command = "path-to-this-script/mtx-changer %c %o %S %a %d"
8 #    you will have the following input to this script:
9 #
10 #  So Bacula will always call with all the following arguments, even though
11 #    in come cases, not all are used.
12 #
13 #  mtx-changer "changer-device" "command" "slot" "archive-device" "drive-index"
14 #                  $1              $2       $3        $4               $5
15 #
16 #  for example:
17 #
18 #  mtx-changer /dev/sg0 load 1 /dev/nst0 0 (on a Linux system)
19
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
22 #   is /dev/nst0.
23 #
24 #  The commands are:
25 #      Command            Function
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
32 #      transfer
33 #
34 #  Slots are numbered from 1 ...
35 #  Drives are numbered from 0 ...
36 #
37 #
38 #  If you need to an offline, refer to the drive as $4
39 #    e.g.   mt -f $4 offline
40 #
41 #  Many changers need an offline after the unload. Also many
42 #   changers need a sleep 60 after the mtx load.
43 #
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.
47 #
48
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 "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
54   exit 1
55 fi
56 . @scriptdir@/mtx-changer.conf
57
58 MTX=@MTX@
59
60 if test ${debug_log} -ne 0 ; then
61   touch @working_dir@/mtx.log
62 fi
63 dbgfile="@working_dir@/mtx.log"
64 debug() {
65     if test -f $dbgfile; then
66         echo "`date +\"%Y%m%d-%H:%M:%S\"` $*" >> $dbgfile
67     fi
68 }
69
70
71 #
72 # Create a temporary file
73 #
74 make_temp_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}"
80         exit 1
81      fi
82   fi
83 }
84
85 #
86 #  Create a temporary file for stderr
87 #
88 #  Note, this file is used because sometime mtx emits
89 #  unexpected error messages followed by the output
90 #  expected during success.
91 #  So we separate STDOUT and STDERR in
92 #  certain of the mtx commands. The contents of STDERR
93 #  is then printed after the STDOUT produced by mtx
94 #  thus we sometimes get better changer results. 
95 #
96 make_err_file() {
97   ERRFILE=`mktemp @working_dir@/mtx.err.XXXXXXXXXX`
98   if test x${ERRFILE} = x; then
99      ERRFILE="@working_dir@/mtx.err.$$"
100      if test -f ${ERRFILE}; then
101         echo "ERROR: Temp file security problem on: ${ERRFILE}"
102         exit 1
103      fi
104   fi
105 }
106
107
108 #
109 # The purpose of this function to wait a maximum 
110 #   time for the drive. It will
111 #   return as soon as the drive is ready, or after
112 #   waiting a maximum of 300 seconds.
113 # Note, this is very system dependent, so if you are
114 #   not running on Linux, you will probably need to
115 #   re-write it, or at least change the grep target.
116 #   We've attempted to get the appropriate OS grep targets
117 #   in the code at the top of this script.
118 #
119 wait_for_drive() {
120   i=0 
121   while [ $i -le 300 ]; do  # Wait max 300 seconds
122     if mt -f $1 status 2>&1 | grep "${ready}" >/dev/null 2>&1; then
123       break
124     fi
125     debug "Device $1 - not ready, retrying..."
126     sleep 1
127     i=`expr $i + 1`
128   done
129 }
130
131 # check parameter count on commandline
132 #
133 check_parm_count() {
134     pCount=$1
135     pCountNeed=$2
136     if test $pCount -lt $pCountNeed; then
137         echo "ERROR: usage: mtx-changer ctl-device command [slot archive-device drive-index]"
138         echo "  Insufficient number of arguments given."
139         if test $pCount -lt 2; then
140             echo "  Mimimum usage is first two arguments ..."
141         else
142             echo "  Command expected $pCountNeed arguments"
143         fi
144         exit 1
145     fi
146 }
147
148 # Check for special cases where only 2 arguments are needed, 
149 #  all others are a minimum of 5
150 #
151 case $2 in
152     list|listall)
153         check_parm_count $# 2
154         ;;
155     slots)
156         check_parm_count $# 2
157         ;;
158     transfer)
159         check_parm_count $# 4
160         ;;
161     *)
162         check_parm_count $# 5
163         ;;
164 esac
165
166
167 # Setup arguments
168 ctl=$1
169 cmd="$2"
170 slot=$3
171 device=$4
172 drive=$5
173
174 debug "Parms: $ctl $cmd $slot $device $drive"
175
176 case $cmd in 
177    unload)
178       debug "Doing mtx -f $ctl unload $slot $drive"
179
180       if test ${offline} -eq 1 ; then
181         mt -f $device offline
182       fi
183       if test ${offline_sleep} -ne 0 ; then
184         sleep ${offline_sleep}
185       fi
186       make_err_file
187       ${MTX} -f $ctl unload $slot $drive 2>${ERRFILE}
188       rtn=$?
189       cat ${ERRFILE}
190       rm -f ${ERRFILE} >/dev/null 2>&1
191       exit $rtn
192       ;;
193
194    load)
195       debug "Doing mtx -f $ctl load $slot $drive"
196       make_err_file
197       ${MTX} -f $ctl load $slot $drive 2>${ERRFILE}
198       rtn=$?
199       if test ${load_sleep} -ne 0 ; then
200         sleep ${load_sleep}
201       fi
202       wait_for_drive $device
203       cat ${ERRFILE}
204       rm -f ${ERRFILE} >/dev/null 2>&1
205       exit $rtn
206       ;;
207
208    list) 
209       debug "Doing mtx -f $ctl -- to list volumes"
210       make_temp_file
211       if test ${inventory} -ne 0 ; then
212         ${MTX} -f $ctl inventory
213       fi
214       ${MTX} -f $ctl status >${TMPFILE}
215       rtn=$?
216       if test ${vxa_packetloader} -ne 0 ; then
217         cat ${TMPFILE} | grep " *Storage Element [0-9]*:.*Full" | sed "s/ Storage Element //" | sed "s/Full :VolumeTag=//"
218       else
219         cat ${TMPFILE} | grep " Storage Element [0-9]*:.*Full" | awk "{print \$3 \$4}" | sed "s/Full *\(:VolumeTag=\)*//"
220       fi
221       cat ${TMPFILE} | grep "^Data Transfer Element [0-9]*:Full (Storage Element [0-9]" | awk '{printf "%s:%s\n",$7,$10}'
222       rm -f ${TMPFILE} >/dev/null 2>&1
223       exit $rtn
224       ;;
225
226    listall)
227 #  Drive content:         D:Drive num:F:Slot loaded:Volume Name
228 #  D:0:F:2:vol2        or D:Drive num:E
229 #  D:1:F:42:vol42   
230 #  D:3:E
231
232 #  Slot content:
233 #  S:1:F:vol1             S:Slot num:F:Volume Name
234 #  S:2:E               or S:Slot num:E
235 #  S:3:F:vol4
236
237 #  Import/Export tray slots:
238 #  I:10:F:vol10           I:Slot num:F:Volume Name
239 #  I:11:E              or I:Slot num:E
240 #  I:12:F:vol40
241  
242       debug "Doing mtx -f $ctl -- to list all"
243       make_temp_file
244       if test ${inventory} -ne 0 ; then
245         ${MTX} -f $ctl inventory
246       fi
247       ${MTX} -f $ctl status >${TMPFILE}
248       rtn=$?
249       # can be converted to awk+sed+cut, see below
250       perl -ne '
251 /Data Transfer Element (\d+):Empty/ && print "D:$1:E\n";
252 /Data Transfer Element (\d+):Full \(Storage Element (\d+) Loaded\)(:VolumeTag =\s*(.+))?/ && print "D:$1:F:$2:$4\n";
253 /Storage Element (\d+):Empty/ && print "S:$1:E\n";
254 /Storage Element (\d+):Full( :VolumeTag=(.+))?/ && print "S:$1:F:$3\n";
255 /Storage Element (\d+) IMPORT.EXPORT:Empty/ && print "I:$1:E\n";
256 /Storage Element (\d+) IMPORT.EXPORT:Full( :VolumeTag=(.+))?/ && print "I:$1:F:$3\n";' ${TMPFILE}
257       # If perl isn't installed, you can use by those commands
258 #cat ${TMPFILE} | grep "Data Transfer Element" | awk "{print \"D:\"\$4 \$7 \$9 \$10}" | sed "s/=/:/" | sed "s/Full/F:/" | sed "s/Empty/E/"
259 #cat ${TMPFILE} | grep -v "Data Transfer Element" | grep "Storage Element" | grep -v "IMPORT/EXPORT" | awk "{print \"S:\"\$3 \$4 \$5}" | sed "s/IMPORT\/EXPORT//" | sed "s/Full *:VolumeTag=/F:/" | sed "s/Empty/E/"
260 #cat ${TMPFILE} | grep -v "Data Transfer Element" | grep "Storage Element" | grep "IMPORT/EXPORT" | awk "{print \"I:\"\$3 \$4 \$5}" | sed "s/IMPORT\/EXPORT//" | sed "s/Full *:VolumeTag=/F:/" | sed "s/Empty/E/" 
261
262       rm -f ${TMPFILE} >/dev/null 2>&1
263       exit $rtn
264       ;;
265
266    transfer)
267       slotdest=$device
268       debug "Doing transfer from $slot to $slotdest"
269       ${MTX} -f $ctl transfer $slot $slotdest
270       rtn=$?
271       exit $rtn
272       ;;
273
274    loaded)
275       debug "Doing mtx -f $ctl $drive -- to find what is loaded"
276       make_temp_file
277       ${MTX} -f $ctl status >${TMPFILE}
278       rtn=$?
279       cat ${TMPFILE} | grep "^Data Transfer Element $drive:Full" | awk "{print \$7}"
280       cat ${TMPFILE} | grep "^Data Transfer Element $drive:Empty" | awk "{print 0}"
281       rm -f ${TMPFILE} >/dev/null 2>&1
282       exit $rtn
283       ;;
284
285    slots)
286       debug "Doing mtx -f $ctl -- to get count of slots"
287       ${MTX} -f $ctl status | grep " *Storage Changer" | awk "{print \$5}"
288       ;;
289 esac