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 echo Args "->" "$@" >> $logfile
51 USE_VERSION_SUFFIXES="@USE_VERSION_SUFFIXES@"
52 if test "$USE_VERSION_SUFFIXES" = "yes"; then
58 ourconf=`amgetconf$SUF changerfile`
60 if [ -x $sbindir/ammt$SUF ]; then
63 elif [ -x "@MT@" ]; then
67 answer="<none> $myname: mt program not found"
69 echo "Exit -> $answer" >> $logfile
73 echo MT "->" $MT $MTF >> $logfile
76 # EXPR=/usr/local/bin/expr # in case you need a more powerful expr...
78 # read in some config parameters
80 if [ ! -f "$ourconf" ]; then
81 answer="<none> $pname: $ourconf does not exist"
83 echo "Exit -> $answer" >> $logfile
88 firstslot=`awk '$1 == "firstslot" {print $2}' $ourconf 2>/dev/null`
89 if [ -z "$firstslot" ]; then
90 answer="<none> $pname: firstslot not specified in $ourconf"
92 echo "Exit -> $answer" >> $logfile
97 lastslot=`awk '$1 == "lastslot" {print $2}' $ourconf 2>/dev/null`
98 if [ -z "$lastslot" ]; then
99 answer="<none> $pname: lastslot not specified in $ourconf"
101 echo "Exit -> $answer" >> $logfile
106 nslots=`$EXPR $lastslot - $firstslot + 1`
108 gravity=`awk '$1 == "gravity" {print $2}' $ourconf 2>/dev/null`
109 if [ -z "$gravity" ]; then
110 answer="<none> $pname: gravity not specified in $ourconf"
112 echo "Exit -> $answer" >> $logfile
117 needeject=`awk '$1 == "needeject" {print $2}' $ourconf 2>/dev/null`
118 if [ -z "$needeject" ]; then
119 answer="<none> $pname: needeject not specified in $ourconf"
121 echo "Exit -> $answer" >> $logfile
126 multieject=`awk '$1 == "multieject" {print $2}' $ourconf 2>/dev/null`
127 if [ -z "$multieject" ]; then
128 echo "Note: setting multieject to a default of zero" >> $logfile
132 ejectdelay=`awk '$1 == "ejectdelay" {print $2}' $ourconf 2>/dev/null`
133 if [ -z "$ejectdelay" ]; then
134 echo "Note: setting ejectdelay to a default of zero" >> $logfile
138 posteject=`awk '$1 == "posteject" {print $2}' $ourconf 2>/dev/null`
139 if [ -z "$posteject" ]; then
140 echo "Note: setting posteject to a default of \"true\"" >> $logfile
144 ourstate=`awk '$1 == "statefile" {print $2}' $ourconf 2>/dev/null`
145 if [ -z "$ourstate" ]; then
146 answer="<none> $pname: statefile not specified in $ourconf"
148 echo "Exit -> $answer" >> $logfile
153 # needeject and multieject are incompatible
154 if [ $needeject -eq 1 ] && [ $multieject -eq 1 ] ; then
155 answer="<none> $pname: needeject and multieject cannot be both enabled in $ourconf"
157 echo "Exit -> $answer" >> $logfile
162 # read in state: only curslot and curloaded at the present time
164 curslot=`awk '$1 == "curslot" {print $2}' $ourstate 2>/dev/null`
165 if [ -z "$curslot" ]; then
169 curloaded=`awk '$1 == "curloaded" {print $2}' $ourstate 2>/dev/null`
170 if [ -z "$curloaded" ]; then
175 # process the command-line
177 # control vars to avoid code duplication: not all shells have functions!
185 if [ $# -ge 1 ]; then command=$1; else command="-usage"; fi
189 -info) # return basic information about changer
191 backwards=`$EXPR 1 - $gravity`
192 answer="$curslot $nslots $backwards"
194 echo "Exit -> $answer" >> $logfile
199 -reset) # reset changer. Actually, we only reset changer state. We
200 # trust that the operator has reloaded a stack and reset the
201 # hardware. In most cases, we do not want to actually do
202 # anything: if the operator has done something with the
203 # hardware, we have no way to know what the actual current
204 # slot is. If the hardware state has not changed, and what is
205 # really wanted is to load the first slot, use "slot first"
212 # XXX put changer-specific reset here, if applicable
215 -eject) # eject tape if loaded. Note that if multieject is set, this
216 # only can make sense if the position is last and gravity 1
223 if [ $multieject -eq 1 ] && \
224 ([ $gravity -eq 0 ] || [ $curslot -ne $lastslot ]) ; then
225 # Can't do this: if we eject, the stacker is going to
226 # load the next tape, and our state will be botched
227 answer="$curslot $pname: Cannot use -eject with multieject/nogravity/notlastslot"
229 echo "Exit -> $answer" >> $logfile
233 if [ $curloaded -eq 0 ]; then
234 answer="$curslot $pname: slot already empty"
236 echo "Exit -> $answer" >> $logfile
242 -slot) # change to slot
251 if [ $newslot -gt $lastslot ] || \
252 [ $newslot -lt $firstslot ] ; then
253 answer="$newslot $pname: no slot $newslot: legal range is $firstslot ... $lastslot"
255 echo "Exit -> $answer" >> $logfile
270 newslot=`$EXPR $curslot + 1`
271 if [ $newslot -gt $lastslot ]; then
274 if [ $slotparm = advance ]; then
279 newslot=`$EXPR $curslot - 1`
280 if [ $newslot -lt $firstslot ]; then
285 answer="<none> $pname: bad slot name \"$slotparm\""
287 echo "Exit -> $answer" >> $logfile
299 if [ $usage -eq 1 ]; then
300 answer="<none> usage: $pname {-reset | -slot [<slot-number>|current|next|prev|advance] | -info | -eject}"
302 echo "Exit -> $answer" >> $logfile
308 # check for legal move
310 if [ $checkgravity -eq 1 ] && [ $gravity -ne 0 ] ; then
311 if [ $newslot -lt $curslot ] || [ "$slotparm" = "prev" ] ; then
312 answer="$newslot $pname: cannot go backwards in gravity stacker"
314 echo "Exit -> $answer" >> $logfile
320 # Do the 'mt offline' style of stacker control if applicable
321 if [ $multieject -eq 1 ] && [ $loadslot -eq 1 ] && [ $newslot -ne $curslot ]
323 # XXX put changer-specific load command here, if applicable
325 curloaded=0 # unless something goes wrong
328 while [ $curslot -ne $newslot ]; do
329 device=`awk '$1 == "slot" && $2 == '$curslot' {print $3}' $ourconf 2>/dev/null`
330 if [ "$device" = "" ]; then
331 answer="$curslot $pname: slot $curslot device not specified in $ourconf"
333 echo "Exit -> $answer" >> $logfile
337 echo " -> offline $device" >> $logfile
338 $MT $MTF $device offline >> $logfile 2>&1
339 if [ $? -ne 0 ]; then
340 answer="$newslot $pname: $device: unable to change to slot $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="$newslot $pname: $posteject $device failed: $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="$curslot $pname: slot $curslot device not specified in $ourconf"
380 echo "Exit -> $answer" >> $logfile
384 echo " -> offline $device" >> $logfile
385 $MT $MTF $device offline >> $logfile 2>&1
386 if [ $? -ne 0 ]; then
388 # XXX if the changer-specific eject command can distinguish
389 # betweeen "slot empty" and more serious errors, return 1
390 # for the first case, 2 for the second case. Generically,
391 # we just presume an error signifies an empty slot.
395 [ $ejectonly -eq 0 ] && [ $ejectdelay -gt 0 ] && sleep $ejectdelay
396 echo " -> running $posteject $device" >> $logfile
397 $posteject $device >> $logfile 2>&1
399 if [ $status -ne 0 ]; then
400 answer="$newslot $pname: $posteject $device failed: $status"
402 echo "Exit -> $answer" >> $logfile
409 if [ $loadslot -eq 1 ]; then # load the tape from the slot
411 # XXX put changer-specific load command here, if applicable
413 curloaded=1 # unless something goes wrong
417 # try to rewind the device
418 device=`awk '$1 == "slot" && $2 == '$curslot' {print $3}' $ourconf 2>/dev/null`
419 if [ "$device" = "" ]; then
420 answer="$curslot $pname: slot $curslot device not specified in $ourconf"
422 echo "Exit -> $answer" >> $logfile
426 echo " -> rewind $device" >> $logfile
427 $MT $MTF $device rewind >> $logfile 2>&1
428 if [ $? -ne 0 ]; then
430 # XXX if the changer-specific load command can distinguish
431 # betweeen "slot empty" and more serious errors, return 1
432 # for the first case, 2 for the second case. Generically,
433 # we just presume an error signifies an empty slot.
442 echo "# multi-changer state cache: DO NOT EDIT!" > $ourstate
443 echo curslot $newslot >> $ourstate
444 echo curloaded $curloaded >> $ourstate
448 if [ $slotempty -eq 1 ]; then
449 answer="$newslot $pname: slot is empty"
451 echo "Exit -> $answer" >> $logfile
456 if [ "$command" = -slot -a "$slotparm" = advance ]; then
460 answer="$newslot $device"
462 echo "Exit -> $answer" >> $logfile