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 # 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
136 # read in state: only curslot and curloaded at the present time
138 curslot=`awk '$1 == "curslot" {print $2}' $ourstate 2>/dev/null`
139 if [ -z "$curslot" ]; then
143 curloaded=`awk '$1 == "curloaded" {print $2}' $ourstate 2>/dev/null`
144 if [ -z "$curloaded" ]; then
149 # process the command-line
151 # control vars to avoid code duplication: not all shells have functions!
159 if [ $# -ge 1 ]; then command=$1; else command="-usage"; fi
163 -info) # return basic information about changer
165 backwards=`$EXPR 1 - $gravity`
166 answer="$curslot $nslots $backwards"
167 echo `_ 'Exit ->'` $answer >> $logfile
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"
185 # XXX put changer-specific reset here, if applicable
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
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
205 if [ $curloaded -eq 0 ]; then
206 answer=`_ '%s %s: slot already empty' "$curslot" "$pname"`
207 echo `_ 'Exit ->'` $answer >> $logfile
213 -slot) # change to slot
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
240 newslot=`$EXPR $curslot + 1`
241 if [ $newslot -gt $lastslot ]; then
244 if [ $slotparm = advance ]; then
249 newslot=`$EXPR $curslot - 1`
250 if [ $newslot -lt $firstslot ]; then
255 answer=`_ '<none> %s: bad slot name "%s"' "$pname" "$slotparm"`
256 echo `_ 'Exit ->'` $answer >> $logfile
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
276 # check for legal move
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
287 # Do the 'mt offline' style of stacker control if applicable
288 if [ $multieject -eq 1 ] && [ $loadslot -eq 1 ] && [ $newslot -ne $curslot ]
290 # XXX put changer-specific load command here, if applicable
292 curloaded=0 # unless something goes wrong
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
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
310 [ $ejectdelay -gt 0 ] && sleep $ejectdelay
311 echo `_ ' -> running'` $posteject $device >> $logfile
312 $posteject $device >> $logfile 2>&1
314 if [ $status -ne 0 ]; then
315 answer=`_ '%s %s: %s %s failed: %s' "$newslot" "$pname" "$posteject" "$device" "$status"`
316 echo `_ 'Exit ->'` $answer >> $logfile
320 curslot=`$EXPR $curslot + 1`
321 if [ $curslot -gt $lastslot ] ; then
327 if [ $ejectonly -eq 1 ] \
328 || ([ $needeject -eq 1 ] \
329 && [ $loadslot -eq 1 ] \
330 && [ $curloaded -eq 1 ] \
331 && [ $newslot -ne $curslot ])
333 # XXX put changer-specific load command here, if applicable
335 curloaded=0 # unless something goes wrong
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
346 echo `_ ' -> offline'` $device >> $logfile
347 try_eject_device $device
348 if [ $? -ne 0 ]; then
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.
357 [ $ejectonly -eq 0 ] && [ $ejectdelay -gt 0 ] && sleep $ejectdelay
358 echo `_ ' -> running '` $posteject $device >> $logfile
359 $posteject $device >> $logfile 2>&1
361 if [ $status -ne 0 ]; then
362 answer=`_ '%s %s: %s %s failed: %s' "$newslot" "$pname" "$posteject" "$device" "$status"`
363 echo `_ 'Exit ->'` $answer >> $logfile
370 if [ $loadslot -eq 1 ]; then # load the tape from the slot
372 # XXX put changer-specific load command here, if applicable
374 curloaded=1 # unless something goes wrong
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
386 amdevcheck_status $device
387 if [ $? -ne 0 ]; then
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.
401 echo `_ '# multi-changer state cache: DO NOT EDIT!'` > $ourstate
402 echo curslot $newslot >> $ourstate
403 echo curloaded $curloaded >> $ourstate
407 if [ $slotempty -eq 1 ]; then
408 answer=`_ '%s %s: slot is empty' "$newslot" "$pname"`
409 echo `_ 'Exit ->'` $answer >> $logfile
414 if [ "$command" = -slot -a "$slotparm" = advance ]; then
418 answer="$newslot $device"
419 echo `_ 'Exit ->'` $answer >> $logfile