Imported Upstream version 2.5.2p1
[debian/amanda] / changer-src / chg-multi.sh.in
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 prefix=@prefix@
34 exec_prefix=@exec_prefix@
35 sbindir=@sbindir@
36 libexecdir=@libexecdir@
37
38 pname="chg-multi"
39
40 PATH=$sbindir:$libexecdir:/usr/bin:/bin:/usr/sbin:/sbin:/usr/ucb
41 export PATH
42
43 if [ -d "@AMANDA_DBGDIR@" ]; then
44         logfile=@AMANDA_DBGDIR@/changer.debug
45 else
46         logfile=/dev/null
47 fi
48
49 # support simultaneous development with and without gettext
50 _() {
51         fmt="$1"
52         shift
53         printf "$fmt" $*
54 }
55
56 echo `_ "arguments ->"` "$@" >> $logfile
57
58 USE_VERSION_SUFFIXES="@USE_VERSION_SUFFIXES@"
59 if test "$USE_VERSION_SUFFIXES" = "yes"; then
60         SUF="-@VERSION@"
61 else
62         SUF=
63 fi
64
65 ourconf=`amgetconf$SUF changerfile`
66
67 if [ -x $sbindir/ammt$SUF ]; then
68         MT=$sbindir/ammt$SUF
69         MTF=-f
70 elif [ -x "@MT@" ]; then
71         MT=@MT@
72         MTF=@MT_FILE_FLAG@
73 else
74         answer=`_ '<none> %s: mt program not found' "$myname"`
75         echo `_ 'Exit ->'` $answer >> $logfile
76         echo $answer
77         exit 1
78 fi
79 echo "MT ->" $MT $MTF >> $logfile
80
81 EXPR=expr
82 # EXPR=/usr/local/bin/expr # in case you need a more powerful expr...
83
84 # read in some config parameters
85
86 if [ ! -f "$ourconf" ]; then
87         answer=`_ '<none> %s: %s does not exist' "$pname" "$ourconf"`
88         echo `_ 'Exit ->'` $answer >> $logfile
89         echo $answer
90         exit 2
91 fi
92
93 firstslot=`awk '$1 == "firstslot" {print $2}' $ourconf 2>/dev/null`
94 if [ -z "$firstslot" ]; then
95         answer=`_ '<none> %s: firstslot not specified in %s' "$pname" "$ourconf"`
96         echo `_ 'Exit ->'` $answer >> $logfile
97         echo $answer
98         exit 2
99 fi
100
101 lastslot=`awk '$1 == "lastslot" {print $2}' $ourconf 2>/dev/null`
102 if [ -z "$lastslot" ]; then
103         answer=`_ '<none> %s: lastslot not specified in %s' "$pname" "$ourconf"`
104         echo `_ 'Exit ->'` $answer >> $logfile
105         echo $answer
106         exit 2
107 fi
108
109 nslots=`$EXPR $lastslot - $firstslot + 1`
110
111 gravity=`awk '$1 == "gravity" {print $2}' $ourconf 2>/dev/null`
112 if [ -z "$gravity" ]; then
113         answer=`_ '<none> %s: gravity not specified in %s' "$pname" "$ourconf"`
114         echo `_ 'Exit ->'` $answer >> $logfile
115         echo $answer
116         exit 2
117 fi
118
119 needeject=`awk '$1 == "needeject" {print $2}' $ourconf 2>/dev/null`
120 if [ -z "$needeject" ]; then
121         answer=`_ '<none> %s: needeject not specified in %s' "$pname" "$ourconf"`
122         echo `_ 'Exit ->'` $answer >> $logfile
123         echo $answer
124         exit 2
125 fi
126
127 multieject=`awk '$1 == "multieject" {print $2}' $ourconf 2>/dev/null`
128 if [ -z "$multieject" ]; then
129         echo `_ 'Note: setting multieject to a default of zero'` >> $logfile
130         multieject=0
131 fi
132
133 ejectdelay=`awk '$1 == "ejectdelay" {print $2}' $ourconf 2>/dev/null`
134 if [ -z "$ejectdelay" ]; then
135         echo `_ 'Note: setting ejectdelay to a default of zero'` >> $logfile
136         ejectdelay=0
137 fi
138
139 posteject=`awk '$1 == "posteject" {print $2}' $ourconf 2>/dev/null`
140 if [ -z "$posteject" ]; then
141         echo `_ 'Note: setting posteject to a default of "true"'` >> $logfile
142         posteject=true
143 fi
144
145 ourstate=`awk '$1 == "statefile" {print $2}' $ourconf 2>/dev/null`
146 if [ -z "$ourstate" ]; then
147         answer=`_ '<none> %s: statefile not specified in %s' "$pname" "$ourconf"`
148         echo `_ 'Exit ->'` $answer >> $logfile
149         echo $answer
150         exit 2
151 fi
152
153 # needeject and multieject are incompatible
154 if [ $needeject -eq 1 ] && [ $multieject -eq 1 ] ; then
155         answer=`_ '<none> %s: needeject and multieject cannot be both enabled in %s' "$pname" "$ourconf"`
156         echo `_ 'Exit ->'` $answer >> $logfile
157         echo $answer
158         exit 2
159 fi
160
161 # read in state: only curslot and curloaded at the present time
162
163 curslot=`awk '$1 == "curslot" {print $2}' $ourstate 2>/dev/null`
164 if [ -z "$curslot" ]; then
165         curslot=$firstslot
166 fi
167
168 curloaded=`awk '$1 == "curloaded" {print $2}' $ourstate 2>/dev/null`
169 if [ -z "$curloaded" ]; then
170         curloaded=0
171 fi
172
173
174 # process the command-line
175
176 # control vars to avoid code duplication: not all shells have functions!
177 usage=0
178 checkgravity=0
179 ejectslot=0
180 loadslot=0
181 slotempty=0
182 ejectonly=0
183
184 if [ $# -ge 1 ]; then command=$1; else command="-usage"; fi
185
186 case "$command" in
187
188 -info) # return basic information about changer
189
190         backwards=`$EXPR 1 - $gravity`
191         answer="$curslot $nslots $backwards"
192         echo `_ 'Exit ->'` $answer >> $logfile
193         echo $answer
194         exit 0
195         ;;
196
197 -reset) # reset changer. Actually, we only reset changer state. We
198         # trust that the operator has reloaded a stack and reset the
199         # hardware. In most cases, we do not want to actually do
200         # anything: if the operator has done something with the
201         # hardware, we have no way to know what the actual current
202         # slot is. If the hardware state has not changed, and what is
203         # really wanted is to load the first slot, use "slot first"
204         # instead 
205
206         checkgravity=0
207         loadslot=1
208         newslot=$firstslot
209         curslot=$firstslot
210         # XXX put changer-specific reset here, if applicable
211         ;;
212
213 -eject) # eject tape if loaded. Note that if multieject is set, this
214         # only can make sense if the position is last and gravity 1
215
216         checkgravity=0
217         loadslot=0
218         newslot=$curslot
219         ejectslot=1
220         ejectonly=1
221         if [ $multieject -eq 1 ] && \
222             ([ $gravity -eq 0 ] || [ $curslot -ne $lastslot ]) ; then 
223                 # Can't do this: if we eject, the stacker is going to
224                 # load the next tape, and our state will be botched
225                 answer=`_ '%s %s: Cannot use -eject with multieject/nogravity/notlastslot' "$curslot" "$pname"`
226                 echo `_ 'Exit ->'` $answer >> $logfile
227                 echo $answer
228                 exit 1
229         fi    
230         if [ $curloaded -eq 0 ]; then
231                 answer=`_ '%s %s: slot already empty' "$curslot" "$pname"`
232                 echo `_ 'Exit ->'` $answer >> $logfile
233                 echo $answer
234                 exit 1
235         fi
236         ;;
237
238 -slot)  # change to slot
239
240         checkgravity=1
241         loadslot=1
242
243         slotparm=$2
244         case "$slotparm" in
245         [0-9]*) 
246                 newslot=$slotparm
247                 if [ $newslot -gt $lastslot ] || \
248                      [ $newslot -lt $firstslot ] ; then
249                         answer=`_ '%s %s: no slot %s: legal range is %s ... %s' "$newslot" "$pname" "$newslot" "$firstslot" "$lastslot"`
250                         echo `_ 'Exit ->'` $answer >> $logfile
251                         echo $answer
252                         exit 1
253                 fi
254                 ;;
255         current)
256                 newslot=$curslot
257                 ;;
258         first)
259                 newslot=$firstslot
260                 ;;
261         last)
262                 newslot=$lastslot
263                 ;;
264         next|advance)
265                 newslot=`$EXPR $curslot + 1`
266                 if [ $newslot -gt $lastslot ]; then
267                         newslot=$firstslot
268                 fi
269                 if [ $slotparm = advance ]; then
270                         loadslot=0
271                 fi
272                 ;;
273         prev)
274                 newslot=`$EXPR $curslot - 1`
275                 if [ $newslot -lt $firstslot ]; then
276                         newslot=$lastslot
277                 fi
278                 ;;
279         *)
280                 answer=`_ '<none> %s: bad slot name "%s"' "$pname" "$slotparm"`
281                 echo `_ 'Exit ->'` $answer >> $logfile
282                 echo $answer
283                 exit 1
284                 ;;
285         esac
286         ;;
287 *)
288         usage=1
289         ;;
290 esac
291
292
293 if [ $usage -eq 1 ]; then
294         answer=`_ '<none> usage: %s {-reset | -slot [<slot-number>|current|next|prev|advance] | -info | -eject}' "$pname"`
295         echo `_ 'Exit ->'` $answer >> $logfile
296         echo $answer
297         exit 2
298 fi
299
300
301 # check for legal move
302
303 if [ $checkgravity -eq 1 ] && [ $gravity -ne 0 ] ; then
304         if [ $newslot -lt $curslot ] || [ "$slotparm" = "prev" ] ; then
305                 answer=`_ '%s %s: cannot go backwards in gravity stacker' "$newslot" "$pname"`
306                 echo `_ 'Exit ->'` $answer >> $logfile
307                 echo $answer
308                 exit 1
309         fi
310 fi
311
312 # Do the 'mt offline' style of stacker control if applicable
313 if [ $multieject -eq 1 ] && [ $loadslot -eq 1 ] && [ $newslot -ne $curslot ]
314 then
315         # XXX put changer-specific load command here, if applicable
316
317         curloaded=0             # unless something goes wrong
318         slotempty=0
319
320         while [ $curslot -ne $newslot ]; do
321             device=`awk '$1 == "slot" && $2 == '$curslot' {print $3}' $ourconf 2>/dev/null`
322             if [ "$device" = "" ]; then
323                 answer=`_ '%s %s: slot %s device not specified in %s' "$curslot" "$pname" "$curslot" "$ourconf"`
324                 echo `_ 'Exit ->'` $answer >> $logfile
325                 echo $answer
326                 exit 2
327             fi
328             echo `_ '     -> offline'` "$device"` >> $logfile
329             $MT $MTF $device offline >> $logfile 2>&1
330             if [ $? -ne 0 ]; then
331                 answer=`_ '%s %s: %s: unable to change to slot %s' "$newslot" "$pname" "$device" "$curslot"`
332                 echo `_ 'Exit ->'` $answer >> $logfile
333                 echo $answer
334                 exit 2
335             fi
336             [ $ejectdelay -gt 0 ] && sleep $ejectdelay
337             echo `_ '     -> running'` $posteject $device >> $logfile
338             $posteject $device >> $logfile 2>&1
339             status=$?
340             if [ $status -ne 0 ]; then
341                 answer=`_ '%s %s: %s %s failed: %s' "$newslot" "$pname" "$posteject" "$device" "$status"`
342                 echo `_ 'Exit ->'` $answer >> $logfile
343                 echo $answer
344                 exit 2
345             fi
346             curslot=`$EXPR $curslot + 1`
347             if [ $curslot -gt $lastslot ] ; then
348                 curslot=$firstslot
349             fi
350         done
351 fi
352
353 if [ $ejectonly -eq 1 ] \
354      || ([ $needeject -eq 1 ] \
355             && [ $loadslot -eq 1 ] \
356             && [ $curloaded -eq 1 ] \
357             && [ $newslot -ne $curslot ])
358 then
359         # XXX put changer-specific load command here, if applicable
360
361         curloaded=0             # unless something goes wrong
362         slotempty=0
363
364         # try to unload the current device
365         device=`awk '$1 == "slot" && $2 == '$curslot' {print $3}' $ourconf 2>/dev/null`
366         if [ "$device" = "" ]; then
367                 answer=`_ '%s %s: slot %s device not specified in %s' "$curslot" "$pname" "$curslot" "$ourconf"`
368                 echo `_ 'Exit ->'` $answer >> $logfile
369                 echo $answer
370                 exit 2
371         fi
372         echo `_ '     -> offline'` $device >> $logfile
373         $MT $MTF $device offline >> $logfile 2>&1
374         if [ $? -ne 0 ]; then
375                 #
376                 # XXX if the changer-specific eject command can distinguish
377                 # betweeen "slot empty" and more serious errors, return 1
378                 # for the first case, 2 for the second case.  Generically,
379                 # we just presume an error signifies an empty slot.
380                 #
381                 slotempty=1
382         else
383                 [ $ejectonly -eq 0 ] && [ $ejectdelay -gt 0 ] && sleep $ejectdelay
384                 echo `_ '     -> running '` $posteject $device >> $logfile
385                 $posteject $device >> $logfile 2>&1
386                 status=$?
387                 if [ $status -ne 0 ]; then
388                         answer=`_ '%s %s: %s %s failed: %s' "$newslot" "$pname" "$posteject" "$device" "$status"`
389                         echo `_ 'Exit ->'` $answer >> $logfile
390                         echo $answer
391                         exit 2
392                 fi
393         fi
394 fi
395
396 if [ $loadslot -eq 1 ]; then    # load the tape from the slot
397
398         # XXX put changer-specific load command here, if applicable
399
400         curloaded=1             # unless something goes wrong
401         slotempty=0
402         curslot=$newslot
403
404         # try to rewind the device
405         device=`awk '$1 == "slot" && $2 == '$curslot' {print $3}' $ourconf 2>/dev/null`
406         if [ "$device" = "" ]; then
407                 answer=`_ "%s %s: slot %s device not specified in %s' "$curslot" "$pname" "$curslot" "$ourconf"`
408                 echo `_ 'Exit ->'` $answer >> $logfile
409                 echo $answer
410                 exit 2
411         fi
412         echo `_ '     -> rewind'` $device >> $logfile
413         $MT $MTF $device rewind >> $logfile 2>&1
414         if [ $? -ne 0 ]; then
415                 #
416                 # XXX if the changer-specific load command can distinguish
417                 # betweeen "slot empty" and more serious errors, return 1
418                 # for the first case, 2 for the second case.  Generically,
419                 # we just presume an error signifies an empty slot.
420                 #
421                 slotempty=1
422                 curloaded=0
423         fi
424 fi
425
426 # update state
427
428 echo `_ '# multi-changer state cache: DO NOT EDIT!'` >  $ourstate
429 echo curslot $newslot                            >> $ourstate
430 echo curloaded $curloaded                        >> $ourstate
431
432 # return slot info
433
434 if [ $slotempty -eq 1 ]; then
435         answer=`_ '%s %s: slot is empty' "$newslot" "$pname"`
436         echo `_ 'Exit ->'` $answer >> $logfile
437         echo $answer
438         exit 1
439 fi
440
441 if [ "$command" = -slot -a "$slotparm" = advance ]; then
442         device=/dev/null
443 fi
444
445 answer="$newslot $device"
446 echo `_ 'Exit ->'` $answer >> $logfile
447 echo $answer
448 exit 0