3 # Amanda, The Advanced Maryland Automatic Network Disk Archiver
4 # Copyright (c) 1991-1999 University of Maryland at College Park
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.
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.
24 # Author: James da Silva, Systems Design and Analysis Group
25 # Computer Science Department
26 # University of Maryland at College Park
30 # chg-multi.sh - generic tape changer script
33 # source utility functions and values from configure
35 exec_prefix=@exec_prefix@
36 amlibexecdir=@amlibexecdir@
37 . ${amlibexecdir}/chg-lib.sh
41 if [ -d "@AMANDA_DBGDIR@" ]; then
42 logfile=@AMANDA_DBGDIR@/changer.debug
47 echo `_ "arguments ->"` "$@" >> $logfile
49 ourconf=`amgetconf changerfile`
51 if ! error=try_find_mt; then
57 # EXPR=/usr/local/bin/expr # in case you need a more powerful expr...
59 # read in some config parameters
61 if [ ! -f "$ourconf" ]; then
62 answer=`_ '<none> %s: %s does not exist' "$pname" "$ourconf"`
63 echo `_ 'Exit ->'` $answer >> $logfile
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
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
84 nslots=`$EXPR $lastslot - $firstslot + 1`
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
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
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
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
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
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
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
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
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
150 if [ ! -d "$dirstate" ]; then
151 answer=`_ '<none> %s: %s must be a directory' "$pname" "$dirstate"`
152 echo `_ 'Exit ->'` $answer >> $logfile
157 if [ ! -w "$dirstate" ]; then
158 answer=`_ "<none> %s: Can't write to %s directory" "$pname" "$dirstate"`
159 echo `_ 'Exit ->'` $answer >> $logfile
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
172 # read in state: only curslot and curloaded at the present time
174 curslot=`awk '$1 == "curslot" {print $2}' $ourstate 2>/dev/null`
175 if [ -z "$curslot" ]; then
179 curloaded=`awk '$1 == "curloaded" {print $2}' $ourstate 2>/dev/null`
180 if [ -z "$curloaded" ]; then
185 # process the command-line
187 # control vars to avoid code duplication: not all shells have functions!
195 if [ $# -ge 1 ]; then command=$1; else command="-usage"; fi
199 -info) # return basic information about changer
201 backwards=`$EXPR 1 - $gravity`
202 answer="$curslot $nslots $backwards"
203 echo `_ 'Exit ->'` $answer >> $logfile
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"
221 # XXX put changer-specific reset here, if applicable
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
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
241 if [ $curloaded -eq 0 ]; then
242 answer=`_ '%s %s: slot already empty' "$curslot" "$pname"`
243 echo `_ 'Exit ->'` $answer >> $logfile
249 -slot) # change to slot
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
276 newslot=`$EXPR $curslot + 1`
277 if [ $newslot -gt $lastslot ]; then
280 if [ $slotparm = advance ]; then
285 newslot=`$EXPR $curslot - 1`
286 if [ $newslot -lt $firstslot ]; then
291 answer=`_ '<none> %s: bad slot name "%s"' "$pname" "$slotparm"`
292 echo `_ 'Exit ->'` $answer >> $logfile
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
312 # check for legal move
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
323 # Do the 'mt offline' style of stacker control if applicable
324 if [ $multieject -eq 1 ] && [ $loadslot -eq 1 ] && [ $newslot -ne $curslot ]
326 # XXX put changer-specific load command here, if applicable
328 curloaded=0 # unless something goes wrong
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
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
346 [ $ejectdelay -gt 0 ] && sleep $ejectdelay
347 echo `_ ' -> running'` $posteject $device >> $logfile
348 $posteject $device >> $logfile 2>&1
350 if [ $status -ne 0 ]; then
351 answer=`_ '%s %s: %s %s failed: %s' "$newslot" "$pname" "$posteject" "$device" "$status"`
352 echo `_ 'Exit ->'` $answer >> $logfile
356 curslot=`$EXPR $curslot + 1`
357 if [ $curslot -gt $lastslot ] ; then
363 if [ $ejectonly -eq 1 ] \
364 || ([ $needeject -eq 1 ] \
365 && [ $loadslot -eq 1 ] \
366 && [ $curloaded -eq 1 ] \
367 && [ $newslot -ne $curslot ])
369 # XXX put changer-specific load command here, if applicable
371 curloaded=0 # unless something goes wrong
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
382 echo `_ ' -> offline'` $device >> $logfile
383 try_eject_device $device
384 if [ $? -ne 0 ]; then
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.
393 [ $ejectonly -eq 0 ] && [ $ejectdelay -gt 0 ] && sleep $ejectdelay
394 echo `_ ' -> running '` $posteject $device >> $logfile
395 $posteject $device >> $logfile 2>&1
397 if [ $status -ne 0 ]; then
398 answer=`_ '%s %s: %s %s failed: %s' "$newslot" "$pname" "$posteject" "$device" "$status"`
399 echo `_ 'Exit ->'` $answer >> $logfile
406 if [ $loadslot -eq 1 ]; then # load the tape from the slot
408 # XXX put changer-specific load command here, if applicable
410 curloaded=1 # unless something goes wrong
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
422 amdevcheck_status $device
423 if [ $? -ne 0 ]; then
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.
437 echo `_ '# multi-changer state cache: DO NOT EDIT!'` > $ourstate
438 echo curslot $newslot >> $ourstate
439 echo curloaded $curloaded >> $ourstate
443 if [ $slotempty -eq 1 ]; then
444 answer=`_ '%s %s: slot is empty: %s' "$newslot" "$pname" "$amdevcheck_message"`
445 echo `_ 'Exit ->'` $answer >> $logfile
450 if [ "$command" = -slot -a "$slotparm" = advance ]; then
454 answer="$newslot $device"
455 echo `_ 'Exit ->'` $answer >> $logfile