]> git.sur5r.net Git - bacula/bacula/blob - bacula/scripts/mtx-changer.in
Add Phil Stracchino's fix for Qt5
[bacula/bacula] / bacula / scripts / mtx-changer.in
1 #!/bin/sh
2 #
3 #   Bacula(R) - The Network Backup Solution
4 #
5 #   Copyright (C) 2000-2016 Kern Sibbald
6 #
7 #   The original author of Bacula is Kern Sibbald, with contributions
8 #   from many others, a complete list can be found in the file AUTHORS.
9 #
10 #   You may use this file and others of this release according to the
11 #   license defined in the LICENSE file, which includes the Affero General
12 #   Public License, v3.0 ("AGPLv3") and some additional permissions and
13 #   terms pursuant to its AGPLv3 Section 7.
14 #
15 #   This notice must be preserved when any source code is 
16 #   conveyed and/or propagated.
17 #
18 #   Bacula(R) is a registered trademark of Kern Sibbald.
19 #
20 #  If you set in your Device resource
21 #
22 #  Changer Command = "path-to-this-script/mtx-changer %c %o %S %a %d"
23 #    you will have the following input to this script:
24 #
25 #  So Bacula will always call with all the following arguments, even though
26 #    in come cases, not all are used.
27 #
28 #  mtx-changer "changer-device" "command" "slot" "archive-device" "drive-index"
29 #                  $1              $2       $3        $4               $5
30 #
31 #  for example:
32 #
33 #  mtx-changer /dev/sg0 load 1 /dev/nst0 0 (on a Linux system)
34
35 #  will request to load the first cartidge into drive 0, where
36 #   the SCSI control channel is /dev/sg0, and the read/write device
37 #   is /dev/nst0.
38 #
39 #  The commands are:
40 #      Command            Function
41 #      unload             unload a given slot
42 #      load               load a given slot
43 #      loaded             which slot is loaded?
44 #      list               list Volume names (requires barcode reader)
45 #      slots              how many slots total?
46 #      listall            list all info
47 #      transfer
48 #
49 #  Slots are numbered from 1 ...
50 #  Drives are numbered from 0 ...
51 #
52 #
53 #  If you need to an offline, refer to the drive as $4
54 #    e.g.   mt -f $4 offline
55 #
56 #  Many changers need an offline after the unload. Also many
57 #   changers need a sleep 60 after the mtx load.
58 #
59 #  N.B. If you change the script, take care to return either 
60 #   the mtx exit code or a 0. If the script exits with a non-zero
61 #   exit code, Bacula will assume the request failed.
62 #
63
64 # myversion must be the same as version in mtx-changer.conf
65 myversion=2
66
67 # source our conf file
68 if test ! -f @scriptdir@/mtx-changer.conf ; then
69   echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
70   echo "ERROR: @scriptdir@/mtx-changer.conf file not found!!!!"
71   echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
72   exit 1
73 fi
74 . @scriptdir@/mtx-changer.conf
75
76 if test "${version}" != "${myversion}" ; then
77   echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
78   echo "ERROR: @scriptdir@/mtx-changer.conf has wrong version. Wanted ${myversion}, got ${version} !!!"
79   echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
80   exit 1
81 fi
82
83 MTX=@MTX@
84
85 if test ${debug_log} -ne 0 ; then
86   touch @working_dir@/mtx.log
87 fi
88 dbgfile="@working_dir@/mtx.log"
89 debug() {
90     if test -f $dbgfile -a ${debug_level} -ge $1; then
91         echo "`date +%m%d-%H:%M:%S.%N|cut -c1-16` ${chgr_id} $2" >> $dbgfile
92     fi
93 }
94
95
96 #
97 # Create a temporary file
98 #
99 make_temp_file() {
100   TMPFILE=`mktemp @working_dir@/mtx.XXXXXXXXXX`
101   if test x${TMPFILE} = x; then
102      TMPFILE="@working_dir@/mtx.$$"
103      if test -f ${TMPFILE}; then
104         echo "ERROR: Temp file security problem on: ${TMPFILE}"
105         exit 1
106      fi
107   fi
108 }
109
110 #
111 #  Create a temporary file for stderr
112 #
113 #  Note, this file is used because sometime mtx emits
114 #  unexpected error messages followed by the output
115 #  expected during success.
116 #  So we separate STDOUT and STDERR in
117 #  certain of the mtx commands. The contents of STDERR
118 #  is then printed after the STDOUT produced by mtx
119 #  thus we sometimes get better changer results. 
120 #
121 make_err_file() {
122   ERRFILE=`mktemp @working_dir@/mtx.err.XXXXXXXXXX`
123   if test x${ERRFILE} = x; then
124      ERRFILE="@working_dir@/mtx.err.$$"
125      if test -f ${ERRFILE}; then
126         echo "ERROR: Temp file security problem on: ${ERRFILE}"
127         exit 1
128      fi
129   fi
130 }
131
132
133 #
134 # The purpose of this function to wait a maximum 
135 #   time for the drive. It will
136 #   return as soon as the drive is ready, or after
137 #   waiting a maximum of 300 seconds.
138 # Note, this is very system dependent, so if you are
139 #   not running on Linux, you will probably need to
140 #   re-write it, or at least change the grep target.
141 #   We've attempted to get the appropriate OS grep targets
142 #   in the code at the top of this script.
143 #
144 wait_for_drive() {
145   i=0 
146   while [ $i -le 300 ]; do  # Wait max 300 seconds
147     if mt -f $1 status 2>&1 | grep "${ready}" >/dev/null 2>&1; then
148       break
149     fi
150     debug $dbglvl "Device $1 - not ready, retrying..."
151     sleep 1
152     i=`expr $i + 1`
153   done
154 }
155
156 # check parameter count on commandline
157 #
158 check_parm_count() {
159     pCount=$1
160     pCountNeed=$2
161     if test $pCount -lt $pCountNeed; then
162         echo "ERROR: usage: mtx-changer ctl-device command [slot archive-device drive-index]"
163         echo "  Insufficient number of arguments given."
164         if test $pCount -lt 2; then
165             echo "  Mimimum usage is first two arguments ..."
166         else
167             echo "  Command expected $pCountNeed arguments"
168         fi
169         exit 1
170     fi
171 }
172
173 # Check for special cases where only 2 arguments are needed, 
174 #  all others are a minimum of 5
175 #
176 case $2 in
177     list|listall)
178         check_parm_count $# 2
179         ;;
180     slots)
181         check_parm_count $# 2
182         ;;
183     transfer)
184         check_parm_count $# 4
185         ;;
186     *)
187         check_parm_count $# 5
188         ;;
189 esac
190
191
192 # Setup arguments
193 ctl=$1
194 cmd="$2"
195 slot=$3
196 device=$4
197 drive=$5
198
199 debug $dbglvl "Parms: $ctl $cmd $slot $device $drive"
200
201 case $cmd in 
202    unload)
203
204       if test ${offline} -eq 1 ; then
205         mt -f $device offline
206       fi
207       if test ${offline_sleep} -ne 0 ; then
208         sleep ${offline_sleep}
209       fi
210       make_err_file
211       for i in 1 2 3 4 5 ; do
212          debug $idbglvl "Doing mtx -f $ctl unload slot=$slot drv=$drive"
213          ${MTX} -f $ctl unload $slot $drive 2>${ERRFILE}
214          rtn=$?
215          if test $rtn -eq 0 ; then
216             break
217          fi
218          grep "Error Code=" ${ERRFILE} 2>/dev/null 1>/dev/null
219          if test $? -ne 0 ; then
220             break
221          fi
222          sleep $i
223       done
224       cat ${ERRFILE}
225       rm -f ${ERRFILE} >/dev/null 2>&1
226       if test $rtn -ne 0 ; then
227          debug $idbglvl "FAIL: mtx -f $ctl unload slot=$slot drv=$drive"
228       fi
229       exit $rtn
230       ;;
231
232    load)
233       make_err_file
234       for i in 1 2 3 4 5 ; do
235          debug $idbglvl "Doing mtx -f $ctl load slot=$slot drv=$drive"
236          ${MTX} -f $ctl load $slot $drive 2>${ERRFILE}
237          rtn=$?
238          if test $rtn -eq 0 ; then
239             break
240          fi
241          grep "Error Code=" ${ERRFILE} 2>/dev/null 1>/dev/null
242          if test $? -ne 0 ; then
243             break
244          fi
245          sleep $i
246       done
247       if test ${load_sleep} -ne 0 ; then
248         sleep ${load_sleep}
249       fi
250       wait_for_drive $device
251       cat ${ERRFILE}
252       rm -f ${ERRFILE} >/dev/null 2>&1
253       if test $rtn -ne 0 ; then
254          debug $idbglvl "FAIL: mtx -f $ctl load slot=$slot drv=$drive"
255       fi
256       exit $rtn
257       ;;
258
259    list) 
260       make_temp_file
261       if test ${inventory} -ne 0 ; then
262         ${MTX} -f $ctl inventory
263       fi
264       debug $dbglvl "Doing mtx -f $ctl list"
265       ${MTX} -f $ctl status >${TMPFILE}
266       rtn=$?
267       if test ${vxa_packetloader} -ne 0 ; then
268         cat ${TMPFILE} | grep " *Storage Element [0-9]*:.*Full" | sed "s/ Storage Element //" | sed "s/Full :VolumeTag=//"
269       else
270         cat ${TMPFILE} | grep " Storage Element [0-9]*:.*Full" | awk "{print \$3 \$4}" | sed "s/Full *\(:VolumeTag=\)*//"
271       fi
272       cat ${TMPFILE} | grep "^Data Transfer Element [0-9]*:Full (Storage Element [0-9]" | awk '{printf "%s:%s\n",$7,$10}'
273       rm -f ${TMPFILE} >/dev/null 2>&1
274       if test $rtn -ne 0 ; then
275          debug $idbglvl "FAIL: mtx -f $ctl list"
276       fi
277       exit $rtn
278       ;;
279
280    listall)
281 #  Drive content:         D:Drive num:F:Slot loaded:Volume Name
282 #  D:0:F:2:vol2        or D:Drive num:E
283 #  D:1:F:42:vol42   
284 #  D:3:E
285
286 #  Slot content:
287 #  S:1:F:vol1             S:Slot num:F:Volume Name
288 #  S:2:E               or S:Slot num:E
289 #  S:3:F:vol4
290
291 #  Import/Export tray slots:
292 #  I:10:F:vol10           I:Slot num:F:Volume Name
293 #  I:11:E              or I:Slot num:E
294 #  I:12:F:vol40
295  
296       make_temp_file
297       if test ${inventory} -ne 0 ; then
298         ${MTX} -f $ctl inventory
299       fi
300       debug $dbglvl "Doing mtx -f $ctl -- to list all"
301       ${MTX} -f $ctl status >${TMPFILE}
302       rtn=$?
303       # can be converted to awk+sed+cut, see below
304       perl -ne '
305 /Data Transfer Element (\d+):Empty/ && print "D:$1:E\n";
306 /Data Transfer Element (\d+):Full \(Storage Element (\d+) Loaded\)(:VolumeTag =\s*(.+))?/ && print "D:$1:F:$2:$4\n";
307 /Storage Element (\d+):Empty/ && print "S:$1:E\n";
308 /Storage Element (\d+):Full( :VolumeTag=(.+))?/ && print "S:$1:F:$3\n";
309 /Storage Element (\d+) IMPORT.EXPORT:Empty/ && print "I:$1:E\n";
310 /Storage Element (\d+) IMPORT.EXPORT:Full( :VolumeTag=(.+))?/ && print "I:$1:F:$3\n";' ${TMPFILE}
311       # If perl isn't installed, you can use by those commands
312 #cat ${TMPFILE} | grep "Data Transfer Element" | awk "{print \"D:\"\$4 \$7 \$9 \$10}" | sed "s/=/:/" | sed "s/Full/F:/" | sed "s/Empty/E/"
313 #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/"
314 #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/" 
315
316       rm -f ${TMPFILE} >/dev/null 2>&1
317       exit $rtn
318       ;;
319
320    transfer)
321       slotdest=$device
322       debug $dbglvl "Doing transfer from $slot to $slotdest"
323       ${MTX} -f $ctl transfer $slot $slotdest
324       rtn=$?
325       if test $rtn -ne 0 ; then
326          debug $idbglvl "FAIL: mtx -f $ctl transfer from=$slot to=$slotdest"
327       fi
328       exit $rtn
329       ;;
330
331    loaded)
332       make_temp_file
333       debug $idbglvl "Doing mtx -f $ctl $drive -- to find what is loaded"
334       ${MTX} -f $ctl status >${TMPFILE}
335       rtn=$?
336       cat ${TMPFILE} | grep "^Data Transfer Element $drive:Full" | awk "{print \$7}"
337       cat ${TMPFILE} | grep "^Data Transfer Element $drive:Empty" | awk "{print 0}"
338       rm -f ${TMPFILE} >/dev/null 2>&1
339       if test $rtn -ne 0 ; then
340          debug $idbglvl "FAIL: mtx -f $ctl loaded drv=$drive"
341       fi
342       exit $rtn
343       ;;
344
345    slots)
346       debug $dbglvl "Doing mtx -f $ctl -- to get count of slots"
347       ${MTX} -f $ctl status | grep " *Storage Changer" | awk "{print \$5}"
348       rtn=$?
349       if test $rtn -ne 0 ; then
350          debug $idbglvl "FAIL: mtx -f $ctl slots"
351       fi
352       ;;
353 esac