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
32 # chg-multi is 'not' intended for use with tape changers or
33 # libraries. It is primarily used to make several, individual
34 # tape drives appear to amanda as a single changer or library.
35 # The "multi" in the script name refers to multiple drives.
39 exec_prefix=@exec_prefix@
41 libexecdir=@libexecdir@
45 PATH=$sbindir:$libexecdir:/usr/bin:/bin:/usr/sbin:/sbin:/usr/ucb
48 if [ -d "@AMANDA_DBGDIR@" ]; then
49 logfile=@AMANDA_DBGDIR@/changer.debug
54 echo Args "->" "$@" >> $logfile
56 USE_VERSION_SUFFIXES="@USE_VERSION_SUFFIXES@"
57 if test "$USE_VERSION_SUFFIXES" = "yes"; then
63 ourconf=`amgetconf$SUF changerfile`
65 if [ -x $sbindir/ammt$SUF ]; then
68 elif [ -x "@MT@" ]; then
72 answer="<none> $myname: mt program not found"
74 echo "Exit -> $answer" >> $logfile
78 echo MT "->" $MT $MTF >> $logfile
81 # EXPR=/usr/local/bin/expr # in case you need a more powerful expr...
83 # read in some config parameters
85 if [ ! -f "$ourconf" ]; then
86 answer="<none> $pname: $ourconf does not exist"
88 echo "Exit -> $answer" >> $logfile
93 firstslot=`awk '$1 == "firstslot" {print $2}' $ourconf 2>/dev/null`
94 if [ -z "$firstslot" ]; then
95 answer="<none> $pname: firstslot not specified in $ourconf"
97 echo "Exit -> $answer" >> $logfile
102 lastslot=`awk '$1 == "lastslot" {print $2}' $ourconf 2>/dev/null`
103 if [ -z "$lastslot" ]; then
104 answer="<none> $pname: lastslot not specified in $ourconf"
106 echo "Exit -> $answer" >> $logfile
111 nslots=`$EXPR $lastslot - $firstslot + 1`
113 gravity=`awk '$1 == "gravity" {print $2}' $ourconf 2>/dev/null`
114 if [ -z "$gravity" ]; then
115 answer="<none> $pname: gravity not specified in $ourconf"
117 echo "Exit -> $answer" >> $logfile
122 needeject=`awk '$1 == "needeject" {print $2}' $ourconf 2>/dev/null`
123 if [ -z "$needeject" ]; then
124 answer="<none> $pname: needeject not specified in $ourconf"
126 echo "Exit -> $answer" >> $logfile
131 multieject=`awk '$1 == "multieject" {print $2}' $ourconf 2>/dev/null`
132 if [ -z "$multieject" ]; then
133 echo "Note: setting multieject to a default of zero" >> $logfile
137 ejectdelay=`awk '$1 == "ejectdelay" {print $2}' $ourconf 2>/dev/null`
138 if [ -z "$ejectdelay" ]; then
139 echo "Note: setting ejectdelay to a default of zero" >> $logfile
143 posteject=`awk '$1 == "posteject" {print $2}' $ourconf 2>/dev/null`
144 if [ -z "$posteject" ]; then
145 echo "Note: setting posteject to a default of \"true\"" >> $logfile
149 ourstate=`awk '$1 == "statefile" {print $2}' $ourconf 2>/dev/null`
150 if [ -z "$ourstate" ]; then
151 answer="<none> $pname: statefile not specified in $ourconf"
153 echo "Exit -> $answer" >> $logfile
158 # needeject and multieject are incompatible
159 if [ $needeject -eq 1 ] && [ $multieject -eq 1 ] ; then
160 answer="<none> $pname: needeject and multieject cannot be both enabled in $ourconf"
162 echo "Exit -> $answer" >> $logfile
167 # read in state: only curslot and curloaded at the present time
169 curslot=`awk '$1 == "curslot" {print $2}' $ourstate 2>/dev/null`
170 if [ -z "$curslot" ]; then
174 curloaded=`awk '$1 == "curloaded" {print $2}' $ourstate 2>/dev/null`
175 if [ -z "$curloaded" ]; then
180 # process the command-line
182 # control vars to avoid code duplication: not all shells have functions!
190 if [ $# -ge 1 ]; then command=$1; else command="-usage"; fi
194 -info) # return basic information about changer
196 backwards=`$EXPR 1 - $gravity`
197 answer="$curslot $nslots $backwards"
199 echo "Exit -> $answer" >> $logfile
204 -reset) # reset changer. Actually, we only reset changer state. We
205 # trust that the operator has reloaded a stack and reset the
206 # hardware. In most cases, we do not want to actually do
207 # anything: if the operator has done something with the
208 # hardware, we have no way to know what the actual current
209 # slot is. If the hardware state has not changed, and what is
210 # really wanted is to load the first slot, use "slot first"
217 # XXX put changer-specific reset here, if applicable
220 -eject) # eject tape if loaded. Note that if multieject is set, this
221 # only can make sense if the position is last and gravity 1
228 if [ $multieject -eq 1 ] && \
229 ([ $gravity -eq 0 ] || [ $curslot -ne $lastslot ]) ; then
230 # Can't do this: if we eject, the stacker is going to
231 # load the next tape, and our state will be botched
232 answer="$curslot $pname: Cannot use -eject with multieject/nogravity/notlastslot"
234 echo "Exit -> $answer" >> $logfile
238 if [ $curloaded -eq 0 ]; then
239 answer="$curslot $pname: slot already empty"
241 echo "Exit -> $answer" >> $logfile
247 -slot) # change to slot
256 if [ $newslot -gt $lastslot ] || \
257 [ $newslot -lt $firstslot ] ; then
258 answer="$newslot $pname: no slot $newslot: legal range is $firstslot ... $lastslot"
260 echo "Exit -> $answer" >> $logfile
275 newslot=`$EXPR $curslot + 1`
276 if [ $newslot -gt $lastslot ]; then
279 if [ $slotparm = advance ]; then
284 newslot=`$EXPR $curslot - 1`
285 if [ $newslot -lt $firstslot ]; then
290 answer="<none> $pname: bad slot name \"$slotparm\""
292 echo "Exit -> $answer" >> $logfile
304 if [ $usage -eq 1 ]; then
305 answer="<none> usage: $pname {-reset | -slot [<slot-number>|current|next|prev|advance] | -info | -eject}"
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="$newslot $pname: cannot go backwards in gravity stacker"
319 echo "Exit -> $answer" >> $logfile
325 # Do the 'mt offline' style of stacker control if applicable
326 if [ $multieject -eq 1 ] && [ $loadslot -eq 1 ] && [ $newslot -ne $curslot ]
328 # XXX put changer-specific load command here, if applicable
330 curloaded=0 # unless something goes wrong
333 while [ $curslot -ne $newslot ]; do
334 device=`awk '$1 == "slot" && $2 == '$curslot' {print $3}' $ourconf 2>/dev/null`
335 if [ "$device" = "" ]; then
336 answer="$curslot $pname: slot $curslot device not specified in $ourconf"
338 echo "Exit -> $answer" >> $logfile
342 echo " -> offline $device" >> $logfile
343 $MT $MTF $device offline >> $logfile 2>&1
344 if [ $? -ne 0 ]; then
345 answer="$newslot $pname: $device: unable to change to slot $curslot"
347 echo "Exit -> $answer" >> $logfile
351 [ $ejectdelay -gt 0 ] && sleep $ejectdelay
352 echo " -> running $posteject $device" >> $logfile
353 $posteject $device >> $logfile 2>&1
355 if [ $status -ne 0 ]; then
356 answer="$newslot $pname: $posteject $device failed: $status"
358 echo "Exit -> $answer" >> $logfile
362 curslot=`$EXPR $curslot + 1`
363 if [ $curslot -gt $lastslot ] ; then
369 if [ $ejectonly -eq 1 ] \
370 || ([ $needeject -eq 1 ] \
371 && [ $loadslot -eq 1 ] \
372 && [ $curloaded -eq 1 ] \
373 && [ $newslot -ne $curslot ])
375 # XXX put changer-specific load command here, if applicable
377 curloaded=0 # unless something goes wrong
380 # try to unload the current device
381 device=`awk '$1 == "slot" && $2 == '$curslot' {print $3}' $ourconf 2>/dev/null`
382 if [ "$device" = "" ]; then
383 answer="$curslot $pname: slot $curslot device not specified in $ourconf"
385 echo "Exit -> $answer" >> $logfile
389 echo " -> offline $device" >> $logfile
390 $MT $MTF $device offline >> $logfile 2>&1
391 if [ $? -ne 0 ]; then
393 # XXX if the changer-specific eject command can distinguish
394 # betweeen "slot empty" and more serious errors, return 1
395 # for the first case, 2 for the second case. Generically,
396 # we just presume an error signifies an empty slot.
400 [ $ejectonly -eq 0 ] && [ $ejectdelay -gt 0 ] && sleep $ejectdelay
401 echo " -> running $posteject $device" >> $logfile
402 $posteject $device >> $logfile 2>&1
404 if [ $status -ne 0 ]; then
405 answer="$newslot $pname: $posteject $device failed: $status"
407 echo "Exit -> $answer" >> $logfile
414 if [ $loadslot -eq 1 ]; then # load the tape from the slot
416 # XXX put changer-specific load command here, if applicable
418 curloaded=1 # unless something goes wrong
422 # try to rewind the device
423 device=`awk '$1 == "slot" && $2 == '$curslot' {print $3}' $ourconf 2>/dev/null`
424 if [ "$device" = "" ]; then
425 answer="$curslot $pname: slot $curslot device not specified in $ourconf"
427 echo "Exit -> $answer" >> $logfile
431 echo " -> rewind $device" >> $logfile
432 $MT $MTF $device rewind >> $logfile 2>&1
433 if [ $? -ne 0 ]; then
435 # XXX if the changer-specific load command can distinguish
436 # betweeen "slot empty" and more serious errors, return 1
437 # for the first case, 2 for the second case. Generically,
438 # we just presume an error signifies an empty slot.
447 echo "# multi-changer state cache: DO NOT EDIT!" > $ourstate
448 echo curslot $newslot >> $ourstate
449 echo curloaded $curloaded >> $ourstate
453 if [ $slotempty -eq 1 ]; then
454 answer="$newslot $pname: slot is empty"
456 echo "Exit -> $answer" >> $logfile
461 if [ "$command" = -slot -a "$slotparm" = advance ]; then
465 answer="$newslot $device"
467 echo "Exit -> $answer" >> $logfile