Imported Upstream version 2.6.1
[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 if [ -f "$ourstate" -a ! -r "$ourstate" ]; then
129         answer=`_ "<none> %s: Can't read the statefile %s" "$pname" "$ourstate"`
130         echo `_ 'Exit ->'` $answer >> $logfile
131         echo $answer
132         exit 2
133 fi
134
135 if [ -f "$ourstate" -a ! -w "$ourstate" ]; then
136         answer=`_ "<none> %s: Can't write the statefile %s" "$pname" "$ourstate"`
137         echo `_ 'Exit ->'` $answer >> $logfile
138         echo $answer
139         exit 2
140 fi
141
142 dirstate=`dirname $ourstate`
143 if [ ! -e "$dirstate" ]; then
144         answer=`_ "<none> %s: Directory %s doesn't exist" "$pname" "$dirstate"`
145         echo `_ 'Exit ->'` $answer >> $logfile
146         echo $answer
147         exit 2
148 fi
149
150 if [ ! -d "$dirstate" ]; then
151         answer=`_ '<none> %s: %s must be a directory' "$pname" "$dirstate"`
152         echo `_ 'Exit ->'` $answer >> $logfile
153         echo $answer
154         exit 2
155 fi
156
157 if [ ! -w "$dirstate" ]; then
158         answer=`_ "<none> %s: Can't write to %s directory" "$pname" "$dirstate"`
159         echo `_ 'Exit ->'` $answer >> $logfile
160         echo $answer
161         exit 2
162 fi
163
164 # needeject and multieject are incompatible
165 if [ $needeject -eq 1 ] && [ $multieject -eq 1 ] ; then
166         answer=`_ '<none> %s: needeject and multieject cannot be both enabled in %s' "$pname" "$ourconf"`
167         echo `_ 'Exit ->'` $answer >> $logfile
168         echo $answer
169         exit 2
170 fi
171
172 # read in state: only curslot and curloaded at the present time
173
174 curslot=`awk '$1 == "curslot" {print $2}' $ourstate 2>/dev/null`
175 if [ -z "$curslot" ]; then
176         curslot=$firstslot
177 fi
178
179 curloaded=`awk '$1 == "curloaded" {print $2}' $ourstate 2>/dev/null`
180 if [ -z "$curloaded" ]; then
181         curloaded=0
182 fi
183
184
185 # process the command-line
186
187 # control vars to avoid code duplication: not all shells have functions!
188 usage=0
189 checkgravity=0
190 ejectslot=0
191 loadslot=0
192 slotempty=0
193 ejectonly=0
194
195 if [ $# -ge 1 ]; then command=$1; else command="-usage"; fi
196
197 case "$command" in
198
199 -info) # return basic information about changer
200
201         backwards=`$EXPR 1 - $gravity`
202         answer="$curslot $nslots $backwards"
203         echo `_ 'Exit ->'` $answer >> $logfile
204         echo $answer
205         exit 0
206         ;;
207
208 -reset) # reset changer. Actually, we only reset changer state. We
209         # trust that the operator has reloaded a stack and reset the
210         # hardware. In most cases, we do not want to actually do
211         # anything: if the operator has done something with the
212         # hardware, we have no way to know what the actual current
213         # slot is. If the hardware state has not changed, and what is
214         # really wanted is to load the first slot, use "slot first"
215         # instead 
216
217         checkgravity=0
218         loadslot=1
219         newslot=$firstslot
220         curslot=$firstslot
221         # XXX put changer-specific reset here, if applicable
222         ;;
223
224 -eject) # eject tape if loaded. Note that if multieject is set, this
225         # only can make sense if the position is last and gravity 1
226
227         checkgravity=0
228         loadslot=0
229         newslot=$curslot
230         ejectslot=1
231         ejectonly=1
232         if [ $multieject -eq 1 ] && \
233             ([ $gravity -eq 0 ] || [ $curslot -ne $lastslot ]) ; then 
234                 # Can't do this: if we eject, the stacker is going to
235                 # load the next tape, and our state will be botched
236                 answer=`_ '%s %s: Cannot use -eject with multieject/nogravity/notlastslot' "$curslot" "$pname"`
237                 echo `_ 'Exit ->'` $answer >> $logfile
238                 echo $answer
239                 exit 1
240         fi    
241         if [ $curloaded -eq 0 ]; then
242                 answer=`_ '%s %s: slot already empty' "$curslot" "$pname"`
243                 echo `_ 'Exit ->'` $answer >> $logfile
244                 echo $answer
245                 exit 1
246         fi
247         ;;
248
249 -slot)  # change to slot
250
251         checkgravity=1
252         loadslot=1
253
254         slotparm=$2
255         case "$slotparm" in
256         [0-9]*) 
257                 newslot=$slotparm
258                 if [ $newslot -gt $lastslot ] || \
259                      [ $newslot -lt $firstslot ] ; then
260                         answer=`_ '%s %s: no slot %s: legal range is %s ... %s' "$newslot" "$pname" "$newslot" "$firstslot" "$lastslot"`
261                         echo `_ 'Exit ->'` $answer >> $logfile
262                         echo $answer
263                         exit 1
264                 fi
265                 ;;
266         current)
267                 newslot=$curslot
268                 ;;
269         first)
270                 newslot=$firstslot
271                 ;;
272         last)
273                 newslot=$lastslot
274                 ;;
275         next|advance)
276                 newslot=`$EXPR $curslot + 1`
277                 if [ $newslot -gt $lastslot ]; then
278                         newslot=$firstslot
279                 fi
280                 if [ $slotparm = advance ]; then
281                         loadslot=0
282                 fi
283                 ;;
284         prev)
285                 newslot=`$EXPR $curslot - 1`
286                 if [ $newslot -lt $firstslot ]; then
287                         newslot=$lastslot
288                 fi
289                 ;;
290         *)
291                 answer=`_ '<none> %s: bad slot name "%s"' "$pname" "$slotparm"`
292                 echo `_ 'Exit ->'` $answer >> $logfile
293                 echo $answer
294                 exit 1
295                 ;;
296         esac
297         ;;
298 *)
299         usage=1
300         ;;
301 esac
302
303
304 if [ $usage -eq 1 ]; then
305         answer=`_ '<none> usage: %s {-reset | -slot [<slot-number>|current|next|prev|advance] | -info | -eject}' "$pname"`
306         echo `_ 'Exit ->'` $answer >> $logfile
307         echo $answer
308         exit 2
309 fi
310
311
312 # check for legal move
313
314 if [ $checkgravity -eq 1 ] && [ $gravity -ne 0 ] ; then
315         if [ $newslot -lt $curslot ] || [ "$slotparm" = "prev" ] ; then
316                 answer=`_ '%s %s: cannot go backwards in gravity stacker' "$newslot" "$pname"`
317                 echo `_ 'Exit ->'` $answer >> $logfile
318                 echo $answer
319                 exit 1
320         fi
321 fi
322
323 # Do the 'mt offline' style of stacker control if applicable
324 if [ $multieject -eq 1 ] && [ $loadslot -eq 1 ] && [ $newslot -ne $curslot ]
325 then
326         # XXX put changer-specific load command here, if applicable
327
328         curloaded=0             # unless something goes wrong
329         slotempty=0
330
331         while [ $curslot -ne $newslot ]; do
332             device=`awk '$1 == "slot" && $2 == '$curslot' {print $3}' $ourconf 2>/dev/null`
333             if [ "$device" = "" ]; then
334                 answer=`_ '%s %s: slot %s device not specified in %s' "$curslot" "$pname" "$curslot" "$ourconf"`
335                 echo `_ 'Exit ->'` $answer >> $logfile
336                 echo $answer
337                 exit 2
338             fi
339             echo `_ '     -> offline'` "$device" >> $logfile
340             if ! try_eject_device $device; then
341                 answer=`_ '%s %s: %s: unable to change to slot %s' "$newslot" "$pname" "$device" "$curslot"`
342                 echo `_ 'Exit ->'` $answer >> $logfile
343                 echo $answer
344                 exit 2
345             fi
346             [ $ejectdelay -gt 0 ] && sleep $ejectdelay
347             echo `_ '     -> running'` $posteject $device >> $logfile
348             $posteject $device >> $logfile 2>&1
349             status=$?
350             if [ $status -ne 0 ]; then
351                 answer=`_ '%s %s: %s %s failed: %s' "$newslot" "$pname" "$posteject" "$device" "$status"`
352                 echo `_ 'Exit ->'` $answer >> $logfile
353                 echo $answer
354                 exit 2
355             fi
356             curslot=`$EXPR $curslot + 1`
357             if [ $curslot -gt $lastslot ] ; then
358                 curslot=$firstslot
359             fi
360         done
361 fi
362
363 if [ $ejectonly -eq 1 ] \
364      || ([ $needeject -eq 1 ] \
365             && [ $loadslot -eq 1 ] \
366             && [ $curloaded -eq 1 ] \
367             && [ $newslot -ne $curslot ])
368 then
369         # XXX put changer-specific load command here, if applicable
370
371         curloaded=0             # unless something goes wrong
372         slotempty=0
373
374         # try to unload the current device
375         device=`awk '$1 == "slot" && $2 == '$curslot' {print $3}' $ourconf 2>/dev/null`
376         if [ "$device" = "" ]; then
377                 answer=`_ '%s %s: slot %s device not specified in %s' "$curslot" "$pname" "$curslot" "$ourconf"`
378                 echo `_ 'Exit ->'` $answer >> $logfile
379                 echo $answer
380                 exit 2
381         fi
382         echo `_ '     -> offline'` $device >> $logfile
383         try_eject_device $device
384         if [ $? -ne 0 ]; then
385                 #
386                 # XXX if the changer-specific eject command can distinguish
387                 # betweeen "slot empty" and more serious errors, return 1
388                 # for the first case, 2 for the second case.  Generically,
389                 # we just presume an error signifies an empty slot.
390                 #
391                 slotempty=1
392         else
393                 [ $ejectonly -eq 0 ] && [ $ejectdelay -gt 0 ] && sleep $ejectdelay
394                 echo `_ '     -> running '` $posteject $device >> $logfile
395                 $posteject $device >> $logfile 2>&1
396                 status=$?
397                 if [ $status -ne 0 ]; then
398                         answer=`_ '%s %s: %s %s failed: %s' "$newslot" "$pname" "$posteject" "$device" "$status"`
399                         echo `_ 'Exit ->'` $answer >> $logfile
400                         echo $answer
401                         exit 2
402                 fi
403         fi
404 fi
405
406 if [ $loadslot -eq 1 ]; then    # load the tape from the slot
407
408         # XXX put changer-specific load command here, if applicable
409
410         curloaded=1             # unless something goes wrong
411         slotempty=0
412         curslot=$newslot
413
414         # try to rewind the device
415         device=`awk '$1 == "slot" && $2 == '$curslot' {print $3}' $ourconf 2>/dev/null`
416         if [ "$device" = "" ]; then
417                 answer=`_ '%s %s: slot %s device not specified in %s' "$curslot" "$pname" "$curslot" "$ourconf"`
418                 echo `_ 'Exit ->'` $answer >> $logfile
419                 echo $answer
420                 exit 2
421         fi
422         amdevcheck_status $device
423         if [ $? -ne 0 ]; then
424                 #
425                 # XXX if the changer-specific load command can distinguish
426                 # betweeen "slot empty" and more serious errors, return 1
427                 # for the first case, 2 for the second case.  Generically,
428                 # we just presume an error signifies an empty slot.
429                 #
430                 slotempty=1
431                 curloaded=0
432         fi
433 fi
434
435 # update state
436
437 echo `_ '# multi-changer state cache: DO NOT EDIT!'` >  $ourstate
438 echo curslot $newslot                            >> $ourstate
439 echo curloaded $curloaded                        >> $ourstate
440
441 # return slot info
442
443 if [ $slotempty -eq 1 ]; then
444         answer=`_ '%s %s: slot is empty: %s' "$newslot" "$pname" "$amdevcheck_message"`
445         echo `_ 'Exit ->'` $answer >> $logfile
446         echo $answer
447         exit 1
448 fi
449
450 if [ "$command" = -slot -a "$slotparm" = advance ]; then
451         device=/dev/null
452 fi
453
454 answer="$newslot $device"
455 echo `_ 'Exit ->'` $answer >> $logfile
456 echo $answer
457 exit 0