3 # Amanda, The Advanced Maryland Automatic Network Disk Archiver
4 # Copyright (c) 1991-1999 University of Maryland at College Park
5 # Copyright (c) 2007-2012 Zmanda, Inc. All Rights Reserved.
8 # Permission to use, copy, modify, distribute, and sell this software and its
9 # documentation for any purpose is hereby granted without fee, provided that
10 # the above copyright notice appear in all copies and that both that
11 # copyright notice and this permission notice appear in supporting
12 # documentation, and that the name of U.M. not be used in advertising or
13 # publicity pertaining to distribution of the software without specific,
14 # written prior permission. U.M. makes no representations about the
15 # suitability of this software for any purpose. It is provided "as is"
16 # without express or implied warranty.
18 # U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
19 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
20 # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
22 # OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
23 # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 # Author: James da Silva, Systems Design and Analysis Group
26 # Computer Science Department
27 # University of Maryland at College Park
31 # chg-multi.sh - generic tape changer script
34 # source utility functions and values from configure
36 exec_prefix=@exec_prefix@
37 amlibexecdir=@amlibexecdir@
38 . ${amlibexecdir}/chg-lib.sh
42 if [ -d "@AMANDA_DBGDIR@" ]; then
43 logfile=@AMANDA_DBGDIR@/changer.debug
48 echo `_ "arguments ->"` "$@" >> $logfile
50 ourconf=`amgetconf changerfile`
52 if ! error=try_find_mt; then
58 # EXPR=/usr/local/bin/expr # in case you need a more powerful expr...
60 # read in some config parameters
62 if [ ! -f "$ourconf" ]; then
63 answer=`_ '<none> %s: %s does not exist' "$pname" "$ourconf"`
64 echo `_ 'Exit ->'` $answer >> $logfile
69 firstslot=`awk '$1 == "firstslot" {print $2}' $ourconf 2>/dev/null`
70 if [ -z "$firstslot" ]; then
71 answer=`_ '<none> %s: firstslot not specified in %s' "$pname" "$ourconf"`
72 echo `_ 'Exit ->'` $answer >> $logfile
77 lastslot=`awk '$1 == "lastslot" {print $2}' $ourconf 2>/dev/null`
78 if [ -z "$lastslot" ]; then
79 answer=`_ '<none> %s: lastslot not specified in %s' "$pname" "$ourconf"`
80 echo `_ 'Exit ->'` $answer >> $logfile
85 nslots=`$EXPR $lastslot - $firstslot + 1`
87 gravity=`awk '$1 == "gravity" {print $2}' $ourconf 2>/dev/null`
88 if [ -z "$gravity" ]; then
89 answer=`_ '<none> %s: gravity not specified in %s' "$pname" "$ourconf"`
90 echo `_ 'Exit ->'` $answer >> $logfile
95 needeject=`awk '$1 == "needeject" {print $2}' $ourconf 2>/dev/null`
96 if [ -z "$needeject" ]; then
97 answer=`_ '<none> %s: needeject not specified in %s' "$pname" "$ourconf"`
98 echo `_ 'Exit ->'` $answer >> $logfile
103 multieject=`awk '$1 == "multieject" {print $2}' $ourconf 2>/dev/null`
104 if [ -z "$multieject" ]; then
105 echo `_ 'Note: setting multieject to a default of zero'` >> $logfile
109 ejectdelay=`awk '$1 == "ejectdelay" {print $2}' $ourconf 2>/dev/null`
110 if [ -z "$ejectdelay" ]; then
111 echo `_ 'Note: setting ejectdelay to a default of zero'` >> $logfile
115 posteject=`awk '$1 == "posteject" {print $2}' $ourconf 2>/dev/null`
116 if [ -z "$posteject" ]; then
117 echo `_ 'Note: setting posteject to a default of "true"'` >> $logfile
121 ourstate=`awk '$1 == "statefile" {print $2}' $ourconf 2>/dev/null`
122 if [ -z "$ourstate" ]; then
123 answer=`_ '<none> %s: statefile not specified in %s' "$pname" "$ourconf"`
124 echo `_ 'Exit ->'` $answer >> $logfile
129 if [ -f "$ourstate" -a ! -r "$ourstate" ]; then
130 answer=`_ "<none> %s: Can't read the statefile %s" "$pname" "$ourstate"`
131 echo `_ 'Exit ->'` $answer >> $logfile
136 if [ -f "$ourstate" -a ! -w "$ourstate" ]; then
137 answer=`_ "<none> %s: Can't write the statefile %s" "$pname" "$ourstate"`
138 echo `_ 'Exit ->'` $answer >> $logfile
143 dirstate=`dirname $ourstate`
144 if [ ! -e "$dirstate" ]; then
145 answer=`_ "<none> %s: Directory %s doesn't exist" "$pname" "$dirstate"`
146 echo `_ 'Exit ->'` $answer >> $logfile
151 if [ ! -d "$dirstate" ]; then
152 answer=`_ '<none> %s: %s must be a directory' "$pname" "$dirstate"`
153 echo `_ 'Exit ->'` $answer >> $logfile
158 if [ ! -w "$dirstate" ]; then
159 answer=`_ "<none> %s: Can't write to %s directory" "$pname" "$dirstate"`
160 echo `_ 'Exit ->'` $answer >> $logfile
165 # needeject and multieject are incompatible
166 if [ $needeject -eq 1 ] && [ $multieject -eq 1 ] ; then
167 answer=`_ '<none> %s: needeject and multieject cannot be both enabled in %s' "$pname" "$ourconf"`
168 echo `_ 'Exit ->'` $answer >> $logfile
173 # read in state: only curslot and curloaded at the present time
175 curslot=`awk '$1 == "curslot" {print $2}' $ourstate 2>/dev/null`
176 if [ -z "$curslot" ]; then
180 curloaded=`awk '$1 == "curloaded" {print $2}' $ourstate 2>/dev/null`
181 if [ -z "$curloaded" ]; then
186 # process the command-line
188 # control vars to avoid code duplication: not all shells have functions!
196 if [ $# -ge 1 ]; then command=$1; else command="-usage"; fi
200 -info) # return basic information about changer
202 backwards=`$EXPR 1 - $gravity`
203 answer="$curslot $nslots $backwards"
204 echo `_ 'Exit ->'` $answer >> $logfile
209 -reset) # reset changer. Actually, we only reset changer state. We
210 # trust that the operator has reloaded a stack and reset the
211 # hardware. In most cases, we do not want to actually do
212 # anything: if the operator has done something with the
213 # hardware, we have no way to know what the actual current
214 # slot is. If the hardware state has not changed, and what is
215 # really wanted is to load the first slot, use "slot first"
222 # XXX put changer-specific reset here, if applicable
225 -eject) # eject tape if loaded. Note that if multieject is set, this
226 # only can make sense if the position is last and gravity 1
233 if [ $multieject -eq 1 ] && \
234 ([ $gravity -eq 0 ] || [ $curslot -ne $lastslot ]) ; then
235 # Can't do this: if we eject, the stacker is going to
236 # load the next tape, and our state will be botched
237 answer=`_ '%s %s: Cannot use -eject with multieject/nogravity/notlastslot' "$curslot" "$pname"`
238 echo `_ 'Exit ->'` $answer >> $logfile
242 if [ $curloaded -eq 0 ]; then
243 answer=`_ '%s %s: slot already empty' "$curslot" "$pname"`
244 echo `_ 'Exit ->'` $answer >> $logfile
250 -slot) # change to slot
259 if [ $newslot -gt $lastslot ] || \
260 [ $newslot -lt $firstslot ] ; then
261 answer=`_ '%s %s: no slot %s: legal range is %s ... %s' "$newslot" "$pname" "$newslot" "$firstslot" "$lastslot"`
262 echo `_ 'Exit ->'` $answer >> $logfile
277 newslot=`$EXPR $curslot + 1`
278 if [ $newslot -gt $lastslot ]; then
281 if [ $slotparm = advance ]; then
286 newslot=`$EXPR $curslot - 1`
287 if [ $newslot -lt $firstslot ]; then
292 answer=`_ '<none> %s: bad slot name "%s"' "$pname" "$slotparm"`
293 echo `_ 'Exit ->'` $answer >> $logfile
305 if [ $usage -eq 1 ]; then
306 answer=`_ '<none> usage: %s {-reset | -slot [<slot-number>|current|next|prev|advance] | -info | -eject}' "$pname"`
307 echo `_ 'Exit ->'` $answer >> $logfile
313 # check for legal move
315 if [ $checkgravity -eq 1 ] && [ $gravity -ne 0 ] ; then
316 if [ $newslot -lt $curslot ] || [ "$slotparm" = "prev" ] ; then
317 answer=`_ '%s %s: cannot go backwards in gravity stacker' "$newslot" "$pname"`
318 echo `_ 'Exit ->'` $answer >> $logfile
324 # Do the 'mt offline' style of stacker control if applicable
325 if [ $multieject -eq 1 ] && [ $loadslot -eq 1 ] && [ $newslot -ne $curslot ]
327 # XXX put changer-specific load command here, if applicable
329 curloaded=0 # unless something goes wrong
332 while [ $curslot -ne $newslot ]; do
333 device=`awk '$1 == "slot" && $2 == '$curslot' {print $3}' $ourconf 2>/dev/null`
334 if [ "$device" = "" ]; then
335 answer=`_ '%s %s: slot %s device not specified in %s' "$curslot" "$pname" "$curslot" "$ourconf"`
336 echo `_ 'Exit ->'` $answer >> $logfile
340 echo `_ ' -> offline'` "$device" >> $logfile
341 if ! try_eject_device $device; then
342 answer=`_ '%s %s: %s: unable to change to slot %s' "$newslot" "$pname" "$device" "$curslot"`
343 echo `_ 'Exit ->'` $answer >> $logfile
347 [ $ejectdelay -gt 0 ] && sleep $ejectdelay
348 echo `_ ' -> running'` $posteject $device >> $logfile
349 $posteject $device >> $logfile 2>&1
351 if [ $status -ne 0 ]; then
352 answer=`_ '%s %s: %s %s failed: %s' "$newslot" "$pname" "$posteject" "$device" "$status"`
353 echo `_ 'Exit ->'` $answer >> $logfile
357 curslot=`$EXPR $curslot + 1`
358 if [ $curslot -gt $lastslot ] ; then
364 if [ $ejectonly -eq 1 ] \
365 || ([ $needeject -eq 1 ] \
366 && [ $loadslot -eq 1 ] \
367 && [ $curloaded -eq 1 ] \
368 && [ $newslot -ne $curslot ])
370 # XXX put changer-specific load command here, if applicable
372 curloaded=0 # unless something goes wrong
375 # try to unload the current device
376 device=`awk '$1 == "slot" && $2 == '$curslot' {print $3}' $ourconf 2>/dev/null`
377 if [ "$device" = "" ]; then
378 answer=`_ '%s %s: slot %s device not specified in %s' "$curslot" "$pname" "$curslot" "$ourconf"`
379 echo `_ 'Exit ->'` $answer >> $logfile
383 echo `_ ' -> offline'` $device >> $logfile
384 try_eject_device $device
385 if [ $? -ne 0 ]; then
387 # XXX if the changer-specific eject command can distinguish
388 # betweeen "slot empty" and more serious errors, return 1
389 # for the first case, 2 for the second case. Generically,
390 # we just presume an error signifies an empty slot.
394 [ $ejectonly -eq 0 ] && [ $ejectdelay -gt 0 ] && sleep $ejectdelay
395 echo `_ ' -> running '` $posteject $device >> $logfile
396 $posteject $device >> $logfile 2>&1
398 if [ $status -ne 0 ]; then
399 answer=`_ '%s %s: %s %s failed: %s' "$newslot" "$pname" "$posteject" "$device" "$status"`
400 echo `_ 'Exit ->'` $answer >> $logfile
407 if [ $loadslot -eq 1 ]; then # load the tape from the slot
409 # XXX put changer-specific load command here, if applicable
411 curloaded=1 # unless something goes wrong
415 # try to rewind the device
416 device=`awk '$1 == "slot" && $2 == '$curslot' {print $3}' $ourconf 2>/dev/null`
417 if [ "$device" = "" ]; then
418 answer=`_ '%s %s: slot %s device not specified in %s' "$curslot" "$pname" "$curslot" "$ourconf"`
419 echo `_ 'Exit ->'` $answer >> $logfile
423 amdevcheck_status $device
424 if [ $? -ne 0 ]; then
426 # XXX if the changer-specific load command can distinguish
427 # betweeen "slot empty" and more serious errors, return 1
428 # for the first case, 2 for the second case. Generically,
429 # we just presume an error signifies an empty slot.
438 echo `_ '# multi-changer state cache: DO NOT EDIT!'` > $ourstate
439 echo curslot $newslot >> $ourstate
440 echo curloaded $curloaded >> $ourstate
444 if [ $slotempty -eq 1 ]; then
445 answer=`_ '%s %s: slot is empty: %s' "$newslot" "$pname" "$amdevcheck_message"`
446 echo `_ 'Exit ->'` $answer >> $logfile
451 if [ "$command" = -slot -a "$slotparm" = advance ]; then
455 answer="$newslot $device"
456 echo `_ 'Exit ->'` $answer >> $logfile