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