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