]> git.sur5r.net Git - bacula/bacula/blob - bacula/scripts/mtx-changer.in
Check for existence of mtx-changer.conf in mtx-changer script
[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 "Temp file security problem on: ${TMPFILE}"
80         exit 1
81      fi
82   fi
83 }
84
85 #
86 # The purpose of this function to wait a maximum 
87 #   time for the drive. It will
88 #   return as soon as the drive is ready, or after
89 #   waiting a maximum of 300 seconds.
90 # Note, this is very system dependent, so if you are
91 #   not running on Linux, you will probably need to
92 #   re-write it, or at least change the grep target.
93 #   We've attempted to get the appropriate OS grep targets
94 #   in the code at the top of this script.
95 #
96 wait_for_drive() {
97   i=0 
98   while [ $i -le 300 ]; do  # Wait max 300 seconds
99     if mt -f $1 status 2>&1 | grep "${ready}" >/dev/null 2>&1; then
100       break
101     fi
102     debug "Device $1 - not ready, retrying..."
103     sleep 1
104     i=`expr $i + 1`
105   done
106 }
107
108 # check parameter count on commandline
109 #
110 check_parm_count() {
111     pCount=$1
112     pCountNeed=$2
113     if test $pCount -lt $pCountNeed; then
114         echo "usage: mtx-changer ctl-device command [slot archive-device drive-index]"
115         echo "  Insufficient number of arguments given."
116         if test $pCount -lt 2; then
117             echo "  Mimimum usage is first two arguments ..."
118         else
119             echo "  Command expected $pCountNeed arguments"
120         fi
121         exit 1
122     fi
123 }
124
125 # Check for special cases where only 2 arguments are needed, 
126 #  all others are a minimum of 5
127 #
128 case $2 in
129     list|listall)
130         check_parm_count $# 2
131         ;;
132     slots)
133         check_parm_count $# 2
134         ;;
135     transfer)
136         check_parm_count $# 4
137         ;;
138     *)
139         check_parm_count $# 5
140         ;;
141 esac
142
143
144 # Setup arguments
145 ctl=$1
146 cmd="$2"
147 slot=$3
148 device=$4
149 drive=$5
150
151 debug "Parms: $ctl $cmd $slot $device $drive"
152
153 case $cmd in 
154    unload)
155       debug "Doing mtx -f $ctl unload $slot $drive"
156
157       if test ${offline} -eq 1 ; then
158         mt -f $device offline
159       fi
160       if test ${offline_sleep} -ne 0 ; then
161         sleep ${offline_sleep}
162       fi
163       ${MTX} -f $ctl unload $slot $drive
164       ;;
165
166    load)
167       debug "Doing mtx -f $ctl load $slot $drive"
168       ${MTX} -f $ctl load $slot $drive
169       rtn=$?
170       if test ${load_sleep} -ne 0 ; then
171         sleep ${load_sleep}
172       fi
173       wait_for_drive $device
174       exit $rtn
175       ;;
176
177    list) 
178       debug "Doing mtx -f $ctl -- to list volumes"
179       make_temp_file
180       if test ${inventory} -ne 0 ; then
181         ${MTX} -f $ctl inventory
182       fi
183       ${MTX} -f $ctl status >${TMPFILE}
184       rtn=$?
185       if test ${vxa_packetloader} -ne 0 ; then
186         cat ${TMPFILE} | grep " *Storage Element [0-9]*:.*Full" | sed "s/ Storage Element //" | sed "s/Full :VolumeTag=//"
187       else
188         cat ${TMPFILE} | grep " Storage Element [0-9]*:.*Full" | awk "{print \$3 \$4}" | sed "s/Full *\(:VolumeTag=\)*//"
189       fi
190       cat ${TMPFILE} | grep "^Data Transfer Element [0-9]*:Full (Storage Element [0-9]" | awk '{printf "%s:%s\n",$7,$10}'
191       rm -f ${TMPFILE} >/dev/null 2>&1
192       exit $rtn
193       ;;
194
195    listall)
196 #  Drive content:         D:Drive num:F:Slot loaded:Volume Name
197 #  D:0:F:2:vol2        or D:Drive num:E
198 #  D:1:F:42:vol42   
199 #  D:3:E
200
201 #  Slot content:
202 #  S:1:F:vol1             S:Slot num:F:Volume Name
203 #  S:2:E               or S:Slot num:E
204 #  S:3:F:vol4
205
206 #  Import/Export tray slots:
207 #  I:10:F:vol10           I:Slot num:F:Volume Name
208 #  I:11:E              or I:Slot num:E
209 #  I:12:F:vol40
210  
211       debug "Doing mtx -f $ctl -- to list all"
212       make_temp_file
213       if test ${inventory} -ne 0 ; then
214         ${MTX} -f $ctl inventory
215       fi
216       ${MTX} -f $ctl status >${TMPFILE}
217       rtn=$?
218       # can be converted to awk+sed+cut, contributions are welcome
219       perl -ne '
220 /Data Transfer Element (\d+):Empty/ && print "D:$1:E\n";
221 /Data Transfer Element (\d+):Full \(Storage Element (\d+) Loaded\)(:VolumeTag =\s*(.+))?/ && print "D:$1:F:$2:$4\n";
222 /Storage Element (\d+):Empty/ && print "S:$1:E\n";
223 /Storage Element (\d+):Full( :VolumeTag=(.+))?/ && print "S:$1:F:$3\n";
224 /Storage Element (\d+) IMPORT.EXPORT:Empty/ && print "I:$1:E\n";
225 /Storage Element (\d+) IMPORT.EXPORT:Full( :VolumeTag=(.+))?/ && print "I:$1:F:$3\n";' ${TMPFILE}
226       rm -f ${TMPFILE} >/dev/null 2>&1
227       exit $rtn
228       ;;
229
230    transfer)
231       slotdest=$device
232       debug "Doing transfer from $slot to $slotdest"
233       ${MTX} -f $ctl transfer $slot $slotdest
234       rtn=$?
235       exit $rtn
236       ;;
237
238    loaded)
239       debug "Doing mtx -f $ctl $drive -- to find what is loaded"
240       make_temp_file
241       ${MTX} -f $ctl status >${TMPFILE}
242       rtn=$?
243       cat ${TMPFILE} | grep "^Data Transfer Element $drive:Full" | awk "{print \$7}"
244       cat ${TMPFILE} | grep "^Data Transfer Element $drive:Empty" | awk "{print 0}"
245       rm -f ${TMPFILE} >/dev/null 2>&1
246       exit $rtn
247       ;;
248
249    slots)
250       debug "Doing mtx -f $ctl -- to get count of slots"
251       ${MTX} -f $ctl status | grep " *Storage Changer" | awk "{print \$5}"
252       ;;
253 esac