ddb5301698c7515e500e5453c3993493b5a49913
[debian/amanda] / changer-src / chg-multi.sh
1 #! @SHELL@
2 #
3 # Amanda, The Advanced Maryland Automatic Network Disk Archiver
4 # Copyright (c) 1991-1999 University of Maryland at College Park
5 # All Rights Reserved.
6 #
7 # Permission to use, copy, modify, distribute, and sell this software and its
8 # documentation for any purpose is hereby granted without fee, provided that
9 # the above copyright notice appear in all copies and that both that
10 # copyright notice and this permission notice appear in supporting
11 # documentation, and that the name of U.M. not be used in advertising or
12 # publicity pertaining to distribution of the software without specific,
13 # written prior permission.  U.M. makes no representations about the
14 # suitability of this software for any purpose.  It is provided "as is"
15 # without express or implied warranty.
16 #
17 # U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
19 # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21 # OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 #
24 # Author: James da Silva, Systems Design and Analysis Group
25 #                          Computer Science Department
26 #                          University of Maryland at College Park
27 #
28
29 #
30 # chg-multi.sh - generic tape changer script
31 #
32
33 # source utility functions and values from configure
34 prefix=@prefix@
35 exec_prefix=@exec_prefix@
36 amlibexecdir=@amlibexecdir@
37 . ${amlibexecdir}/chg-lib.sh
38
39 pname="chg-multi"
40
41 if [ -d "@AMANDA_DBGDIR@" ]; then
42         logfile=@AMANDA_DBGDIR@/changer.debug
43 else
44         logfile=/dev/null
45 fi
46
47 echo `_ "arguments ->"` "$@" >> $logfile
48
49 ourconf=`amgetconf changerfile`
50
51 if ! error=try_find_mt; then
52     echo <none> $error
53     exit 2
54 fi
55
56 EXPR=expr
57 # EXPR=/usr/local/bin/expr # in case you need a more powerful expr...
58
59 # read in some config parameters
60
61 if [ ! -f "$ourconf" ]; then
62         answer=`_ '<none> %s: %s does not exist' "$pname" "$ourconf"`
63         echo `_ 'Exit ->'` $answer >> $logfile
64         echo $answer
65         exit 2
66 fi
67
68 firstslot=`awk '$1 == "firstslot" {print $2}' $ourconf 2>/dev/null`
69 if [ -z "$firstslot" ]; then
70         answer=`_ '<none> %s: firstslot not specified in %s' "$pname" "$ourconf"`
71         echo `_ 'Exit ->'` $answer >> $logfile
72         echo $answer
73         exit 2
74 fi
75
76 lastslot=`awk '$1 == "lastslot" {print $2}' $ourconf 2>/dev/null`
77 if [ -z "$lastslot" ]; then
78         answer=`_ '<none> %s: lastslot not specified in %s' "$pname" "$ourconf"`
79         echo `_ 'Exit ->'` $answer >> $logfile
80         echo $answer
81         exit 2
82 fi
83
84 nslots=`$EXPR $lastslot - $firstslot + 1`
85
86 gravity=`awk '$1 == "gravity" {print $2}' $ourconf 2>/dev/null`
87 if [ -z "$gravity" ]; then
88         answer=`_ '<none> %s: gravity not specified in %s' "$pname" "$ourconf"`
89         echo `_ 'Exit ->'` $answer >> $logfile
90         echo $answer
91         exit 2
92 fi
93
94 needeject=`awk '$1 == "needeject" {print $2}' $ourconf 2>/dev/null`
95 if [ -z "$needeject" ]; then
96         answer=`_ '<none> %s: needeject not specified in %s' "$pname" "$ourconf"`
97         echo `_ 'Exit ->'` $answer >> $logfile
98         echo $answer
99         exit 2
100 fi
101
102 multieject=`awk '$1 == "multieject" {print $2}' $ourconf 2>/dev/null`
103 if [ -z "$multieject" ]; then
104         echo `_ 'Note: setting multieject to a default of zero'` >> $logfile
105         multieject=0
106 fi
107
108 ejectdelay=`awk '$1 == "ejectdelay" {print $2}' $ourconf 2>/dev/null`
109 if [ -z "$ejectdelay" ]; then
110         echo `_ 'Note: setting ejectdelay to a default of zero'` >> $logfile
111         ejectdelay=0
112 fi
113
114 posteject=`awk '$1 == "posteject" {print $2}' $ourconf 2>/dev/null`
115 if [ -z "$posteject" ]; then
116         echo `_ 'Note: setting posteject to a default of "true"'` >> $logfile
117         posteject=true
118 fi
119
120 ourstate=`awk '$1 == "statefile" {print $2}' $ourconf 2>/dev/null`
121 if [ -z "$ourstate" ]; then
122         answer=`_ '<none> %s: statefile not specified in %s' "$pname" "$ourconf"`
123         echo `_ 'Exit ->'` $answer >> $logfile
124         echo $answer
125         exit 2
126 fi
127
128 # needeject and multieject are incompatible
129 if [ $needeject -eq 1 ] && [ $multieject -eq 1 ] ; then
130         answer=`_ '<none> %s: needeject and multieject cannot be both enabled in %s' "$pname" "$ourconf"`
131         echo `_ 'Exit ->'` $answer >> $logfile
132         echo $answer
133         exit 2
134 fi
135
136 # read in state: only curslot and curloaded at the present time
137
138 curslot=`awk '$1 == "curslot" {print $2}' $ourstate 2>/dev/null`
139 if [ -z "$curslot" ]; then
140         curslot=$firstslot
141 fi
142
143 curloaded=`awk '$1 == "curloaded" {print $2}' $ourstate 2>/dev/null`
144 if [ -z "$curloaded" ]; then
145         curloaded=0
146 fi
147
148
149 # process the command-line
150
151 # control vars to avoid code duplication: not all shells have functions!
152 usage=0
153 checkgravity=0
154 ejectslot=0
155 loadslot=0
156 slotempty=0
157 ejectonly=0
158
159 if [ $# -ge 1 ]; then command=$1; else command="-usage"; fi
160
161 case "$command" in
162
163 -info) # return basic information about changer
164
165         backwards=`$EXPR 1 - $gravity`
166         answer="$curslot $nslots $backwards"
167         echo `_ 'Exit ->'` $answer >> $logfile
168         echo $answer
169         exit 0
170         ;;
171
172 -reset) # reset changer. Actually, we only reset changer state. We
173         # trust that the operator has reloaded a stack and reset the
174         # hardware. In most cases, we do not want to actually do
175         # anything: if the operator has done something with the
176         # hardware, we have no way to know what the actual current
177         # slot is. If the hardware state has not changed, and what is
178         # really wanted is to load the first slot, use "slot first"
179         # instead 
180
181         checkgravity=0
182         loadslot=1
183         newslot=$firstslot
184         curslot=$firstslot
185         # XXX put changer-specific reset here, if applicable
186         ;;
187
188 -eject) # eject tape if loaded. Note that if multieject is set, this
189         # only can make sense if the position is last and gravity 1
190
191         checkgravity=0
192         loadslot=0
193         newslot=$curslot
194         ejectslot=1
195         ejectonly=1
196         if [ $multieject -eq 1 ] && \
197             ([ $gravity -eq 0 ] || [ $curslot -ne $lastslot ]) ; then 
198                 # Can't do this: if we eject, the stacker is going to
199                 # load the next tape, and our state will be botched
200                 answer=`_ '%s %s: Cannot use -eject with multieject/nogravity/notlastslot' "$curslot" "$pname"`
201                 echo `_ 'Exit ->'` $answer >> $logfile
202                 echo $answer
203                 exit 1
204         fi    
205         if [ $curloaded -eq 0 ]; then
206                 answer=`_ '%s %s: slot already empty' "$curslot" "$pname"`
207                 echo `_ 'Exit ->'` $answer >> $logfile
208                 echo $answer
209                 exit 1
210         fi
211         ;;
212
213 -slot)  # change to slot
214
215         checkgravity=1
216         loadslot=1
217
218         slotparm=$2
219         case "$slotparm" in
220         [0-9]*) 
221                 newslot=$slotparm
222                 if [ $newslot -gt $lastslot ] || \
223                      [ $newslot -lt $firstslot ] ; then
224                         answer=`_ '%s %s: no slot %s: legal range is %s ... %s' "$newslot" "$pname" "$newslot" "$firstslot" "$lastslot"`
225                         echo `_ 'Exit ->'` $answer >> $logfile
226                         echo $answer
227                         exit 1
228                 fi
229                 ;;
230         current)
231                 newslot=$curslot
232                 ;;
233         first)
234                 newslot=$firstslot
235                 ;;
236         last)
237                 newslot=$lastslot
238                 ;;
239         next|advance)
240                 newslot=`$EXPR $curslot + 1`
241                 if [ $newslot -gt $lastslot ]; then
242                         newslot=$firstslot
243                 fi
244                 if [ $slotparm = advance ]; then
245                         loadslot=0
246                 fi
247                 ;;
248         prev)
249                 newslot=`$EXPR $curslot - 1`
250                 if [ $newslot -lt $firstslot ]; then
251                         newslot=$lastslot
252                 fi
253                 ;;
254         *)
255                 answer=`_ '<none> %s: bad slot name "%s"' "$pname" "$slotparm"`
256                 echo `_ 'Exit ->'` $answer >> $logfile
257                 echo $answer
258                 exit 1
259                 ;;
260         esac
261         ;;
262 *)
263         usage=1
264         ;;
265 esac
266
267
268 if [ $usage -eq 1 ]; then
269         answer=`_ '<none> usage: %s {-reset | -slot [<slot-number>|current|next|prev|advance] | -info | -eject}' "$pname"`
270         echo `_ 'Exit ->'` $answer >> $logfile
271         echo $answer
272         exit 2
273 fi
274
275
276 # check for legal move
277
278 if [ $checkgravity -eq 1 ] && [ $gravity -ne 0 ] ; then
279         if [ $newslot -lt $curslot ] || [ "$slotparm" = "prev" ] ; then
280                 answer=`_ '%s %s: cannot go backwards in gravity stacker' "$newslot" "$pname"`
281                 echo `_ 'Exit ->'` $answer >> $logfile
282                 echo $answer
283                 exit 1
284         fi
285 fi
286
287 # Do the 'mt offline' style of stacker control if applicable
288 if [ $multieject -eq 1 ] && [ $loadslot -eq 1 ] && [ $newslot -ne $curslot ]
289 then
290         # XXX put changer-specific load command here, if applicable
291
292         curloaded=0             # unless something goes wrong
293         slotempty=0
294
295         while [ $curslot -ne $newslot ]; do
296             device=`awk '$1 == "slot" && $2 == '$curslot' {print $3}' $ourconf 2>/dev/null`
297             if [ "$device" = "" ]; then
298                 answer=`_ '%s %s: slot %s device not specified in %s' "$curslot" "$pname" "$curslot" "$ourconf"`
299                 echo `_ 'Exit ->'` $answer >> $logfile
300                 echo $answer
301                 exit 2
302             fi
303             echo `_ '     -> offline'` "$device" >> $logfile
304             if ! try_eject_device $device; then
305                 answer=`_ '%s %s: %s: unable to change to slot %s' "$newslot" "$pname" "$device" "$curslot"`
306                 echo `_ 'Exit ->'` $answer >> $logfile
307                 echo $answer
308                 exit 2
309             fi
310             [ $ejectdelay -gt 0 ] && sleep $ejectdelay
311             echo `_ '     -> running'` $posteject $device >> $logfile
312             $posteject $device >> $logfile 2>&1
313             status=$?
314             if [ $status -ne 0 ]; then
315                 answer=`_ '%s %s: %s %s failed: %s' "$newslot" "$pname" "$posteject" "$device" "$status"`
316                 echo `_ 'Exit ->'` $answer >> $logfile
317                 echo $answer
318                 exit 2
319             fi
320             curslot=`$EXPR $curslot + 1`
321             if [ $curslot -gt $lastslot ] ; then
322                 curslot=$firstslot
323             fi
324         done
325 fi
326
327 if [ $ejectonly -eq 1 ] \
328      || ([ $needeject -eq 1 ] \
329             && [ $loadslot -eq 1 ] \
330             && [ $curloaded -eq 1 ] \
331             && [ $newslot -ne $curslot ])
332 then
333         # XXX put changer-specific load command here, if applicable
334
335         curloaded=0             # unless something goes wrong
336         slotempty=0
337
338         # try to unload the current device
339         device=`awk '$1 == "slot" && $2 == '$curslot' {print $3}' $ourconf 2>/dev/null`
340         if [ "$device" = "" ]; then
341                 answer=`_ '%s %s: slot %s device not specified in %s' "$curslot" "$pname" "$curslot" "$ourconf"`
342                 echo `_ 'Exit ->'` $answer >> $logfile
343                 echo $answer
344                 exit 2
345         fi
346         echo `_ '     -> offline'` $device >> $logfile
347         try_eject_device $device
348         if [ $? -ne 0 ]; then
349                 #
350                 # XXX if the changer-specific eject command can distinguish
351                 # betweeen "slot empty" and more serious errors, return 1
352                 # for the first case, 2 for the second case.  Generically,
353                 # we just presume an error signifies an empty slot.
354                 #
355                 slotempty=1
356         else
357                 [ $ejectonly -eq 0 ] && [ $ejectdelay -gt 0 ] && sleep $ejectdelay
358                 echo `_ '     -> running '` $posteject $device >> $logfile
359                 $posteject $device >> $logfile 2>&1
360                 status=$?
361                 if [ $status -ne 0 ]; then
362                         answer=`_ '%s %s: %s %s failed: %s' "$newslot" "$pname" "$posteject" "$device" "$status"`
363                         echo `_ 'Exit ->'` $answer >> $logfile
364                         echo $answer
365                         exit 2
366                 fi
367         fi
368 fi
369
370 if [ $loadslot -eq 1 ]; then    # load the tape from the slot
371
372         # XXX put changer-specific load command here, if applicable
373
374         curloaded=1             # unless something goes wrong
375         slotempty=0
376         curslot=$newslot
377
378         # try to rewind the device
379         device=`awk '$1 == "slot" && $2 == '$curslot' {print $3}' $ourconf 2>/dev/null`
380         if [ "$device" = "" ]; then
381                 answer=`_ '%s %s: slot %s device not specified in %s' "$curslot" "$pname" "$curslot" "$ourconf"`
382                 echo `_ 'Exit ->'` $answer >> $logfile
383                 echo $answer
384                 exit 2
385         fi
386         amdevcheck_status $device
387         if [ $? -ne 0 ]; then
388                 #
389                 # XXX if the changer-specific load command can distinguish
390                 # betweeen "slot empty" and more serious errors, return 1
391                 # for the first case, 2 for the second case.  Generically,
392                 # we just presume an error signifies an empty slot.
393                 #
394                 slotempty=1
395                 curloaded=0
396         fi
397 fi
398
399 # update state
400
401 echo `_ '# multi-changer state cache: DO NOT EDIT!'` >  $ourstate
402 echo curslot $newslot                            >> $ourstate
403 echo curloaded $curloaded                        >> $ourstate
404
405 # return slot info
406
407 if [ $slotempty -eq 1 ]; then
408         answer=`_ '%s %s: slot is empty' "$newslot" "$pname"`
409         echo `_ 'Exit ->'` $answer >> $logfile
410         echo $answer
411         exit 1
412 fi
413
414 if [ "$command" = -slot -a "$slotparm" = advance ]; then
415         device=/dev/null
416 fi
417
418 answer="$newslot $device"
419 echo `_ 'Exit ->'` $answer >> $logfile
420 echo $answer
421 exit 0