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
34 exec_prefix=@exec_prefix@
36 libexecdir=@libexecdir@
40 PATH=$sbindir:$libexecdir:/usr/bin:/bin:/usr/sbin:/sbin:/usr/ucb
43 if [ -d "@AMANDA_DBGDIR@" ]; then
44 logfile=@AMANDA_DBGDIR@/changer.debug
49 # support simultaneous development with and without gettext
56 echo `_ "arguments ->"` "$@" >> $logfile
58 USE_VERSION_SUFFIXES="@USE_VERSION_SUFFIXES@"
59 if test "$USE_VERSION_SUFFIXES" = "yes"; then
65 ourconf=`amgetconf$SUF changerfile`
67 if [ -x $sbindir/ammt$SUF ]; then
70 elif [ -x "@MT@" ]; then
74 answer=`_ '<none> %s: mt program not found' "$myname"`
75 echo `_ 'Exit ->'` $answer >> $logfile
79 echo "MT ->" $MT $MTF >> $logfile
82 # EXPR=/usr/local/bin/expr # in case you need a more powerful expr...
84 # read in some config parameters
86 if [ ! -f "$ourconf" ]; then
87 answer=`_ '<none> %s: %s does not exist' "$pname" "$ourconf"`
88 echo `_ 'Exit ->'` $answer >> $logfile
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
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
109 nslots=`$EXPR $lastslot - $firstslot + 1`
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
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
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
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
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
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
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
161 # read in state: only curslot and curloaded at the present time
163 curslot=`awk '$1 == "curslot" {print $2}' $ourstate 2>/dev/null`
164 if [ -z "$curslot" ]; then
168 curloaded=`awk '$1 == "curloaded" {print $2}' $ourstate 2>/dev/null`
169 if [ -z "$curloaded" ]; then
174 # process the command-line
176 # control vars to avoid code duplication: not all shells have functions!
184 if [ $# -ge 1 ]; then command=$1; else command="-usage"; fi
188 -info) # return basic information about changer
190 backwards=`$EXPR 1 - $gravity`
191 answer="$curslot $nslots $backwards"
192 echo `_ 'Exit ->'` $answer >> $logfile
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"
210 # XXX put changer-specific reset here, if applicable
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
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
230 if [ $curloaded -eq 0 ]; then
231 answer=`_ '%s %s: slot already empty' "$curslot" "$pname"`
232 echo `_ 'Exit ->'` $answer >> $logfile
238 -slot) # change to slot
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
265 newslot=`$EXPR $curslot + 1`
266 if [ $newslot -gt $lastslot ]; then
269 if [ $slotparm = advance ]; then
274 newslot=`$EXPR $curslot - 1`
275 if [ $newslot -lt $firstslot ]; then
280 answer=`_ '<none> %s: bad slot name "%s"' "$pname" "$slotparm"`
281 echo `_ 'Exit ->'` $answer >> $logfile
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
301 # check for legal move
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
312 # Do the 'mt offline' style of stacker control if applicable
313 if [ $multieject -eq 1 ] && [ $loadslot -eq 1 ] && [ $newslot -ne $curslot ]
315 # XXX put changer-specific load command here, if applicable
317 curloaded=0 # unless something goes wrong
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
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
336 [ $ejectdelay -gt 0 ] && sleep $ejectdelay
337 echo `_ ' -> running'` $posteject $device >> $logfile
338 $posteject $device >> $logfile 2>&1
340 if [ $status -ne 0 ]; then
341 answer=`_ '%s %s: %s %s failed: %s' "$newslot" "$pname" "$posteject" "$device" "$status"`
342 echo `_ 'Exit ->'` $answer >> $logfile
346 curslot=`$EXPR $curslot + 1`
347 if [ $curslot -gt $lastslot ] ; then
353 if [ $ejectonly -eq 1 ] \
354 || ([ $needeject -eq 1 ] \
355 && [ $loadslot -eq 1 ] \
356 && [ $curloaded -eq 1 ] \
357 && [ $newslot -ne $curslot ])
359 # XXX put changer-specific load command here, if applicable
361 curloaded=0 # unless something goes wrong
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
372 echo `_ ' -> offline'` $device >> $logfile
373 $MT $MTF $device offline >> $logfile 2>&1
374 if [ $? -ne 0 ]; then
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.
383 [ $ejectonly -eq 0 ] && [ $ejectdelay -gt 0 ] && sleep $ejectdelay
384 echo `_ ' -> running '` $posteject $device >> $logfile
385 $posteject $device >> $logfile 2>&1
387 if [ $status -ne 0 ]; then
388 answer=`_ '%s %s: %s %s failed: %s' "$newslot" "$pname" "$posteject" "$device" "$status"`
389 echo `_ 'Exit ->'` $answer >> $logfile
396 if [ $loadslot -eq 1 ]; then # load the tape from the slot
398 # XXX put changer-specific load command here, if applicable
400 curloaded=1 # unless something goes wrong
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
412 echo `_ ' -> rewind'` $device >> $logfile
413 $MT $MTF $device rewind >> $logfile 2>&1
414 if [ $? -ne 0 ]; then
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.
428 echo `_ '# multi-changer state cache: DO NOT EDIT!'` > $ourstate
429 echo curslot $newslot >> $ourstate
430 echo curloaded $curloaded >> $ourstate
434 if [ $slotempty -eq 1 ]; then
435 answer=`_ '%s %s: slot is empty' "$newslot" "$pname"`
436 echo `_ 'Exit ->'` $answer >> $logfile
441 if [ "$command" = -slot -a "$slotparm" = advance ]; then
445 answer="$newslot $device"
446 echo `_ 'Exit ->'` $answer >> $logfile