3 # Amanda, The Advanced Maryland Automatic Network Disk Archiver
4 # Copyright (c) 1991-1998 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-chs.sh - chs 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
51 if [ -x $sbindir/ammt$SUF ]; then
54 elif [ -x "@MT@" ]; then
58 answer="<none> $myname: mt program not found"
60 echo "Exit -> $answer" >> $logfile
64 echo MT "->" $MT $MTF >> $logfile
66 USE_VERSION_SUFFIXES="@USE_VERSION_SUFFIXES@"
67 if test "$USE_VERSION_SUFFIXES" = "yes"; then
73 ourconf=`amgetconf$SUF changerfile`
74 changerdev=`amgetconf$SUF changerdev`
75 if test -n "$changerdev" && test x"$changerdev" != x/dev/null; then
76 CHS="$CHS -f$changerdev"
79 # read in some config parameters
81 if [ \! -f $ourconf ]; then
82 answer="<none> $pname: $ourconf does not exist"
84 echo "Exit -> $answer" >> $logfile
89 firstslot=`awk '$1 == "firstslot" {print $2}' $ourconf 2>/dev/null`
90 if [ "$firstslot" = "" ]; then
91 answer="<none> $pname: firstslot not specified in $ourconf"
93 echo "Exit -> $answer" >> $logfile
98 lastslot=`awk '$1 == "lastslot" {print $2}' $ourconf 2>/dev/null`
99 if [ "$lastslot" = "" ]; then
100 answer="<none> $pname: lastslot not specified in $ourconf"
102 echo "Exit -> $answer" >> $logfile
107 nslots=`expr $lastslot - $firstslot + 1`
109 gravity=`awk '$1 == "gravity" {print $2}' $ourconf 2>/dev/null`
110 if [ "$gravity" = "" ]; then
111 answer="<none> $pname: gravity not specified in $ourconf"
113 echo "Exit -> $answer" >> $logfile
118 needeject=`awk '$1 == "needeject" {print $2}' $ourconf 2>/dev/null`
119 if [ "$needeject" = "" ]; then
120 answer="<none> $pname: needeject not specified in $ourconf"
122 echo "Exit -> $answer" >> $logfile
127 multieject=`awk '$1 == "multieject" {print $2}' $ourconf 2>/dev/null`
128 if [ "$multieject" = "" ]; then
129 echo "Note -> multieject not specified in $ourconf" >> $logfile
133 ejectdelay=`awk '$1 == "ejectdelay" {print $2}' $ourconf 2>/dev/null`
134 if [ "$ejectdelay" = "" ]; then
135 echo "Note -> ejectdelay not specified in $ourconf" >> $logfile
139 ourstate=`awk '$1 == "statefile" {print $2}' $ourconf 2>/dev/null`
140 if [ "$ourstate" = "" ]; then
141 answer="<none> $pname: statefile not specified in $ourconf"
143 echo "Exit -> $answer" >> $logfile
148 # read in state: only curslot and curloaded at the present time
150 curslot=`awk '$1 == "curslot" {print $2}' $ourstate 2>/dev/null`
151 if [ "$curslot" = "" ]; then
155 curloaded=`awk '$1 == "curloaded" {print $2}' $ourstate 2>/dev/null`
156 if [ "$curloaded" = "" ]; then
161 # process the command-line
163 # control vars to avoid code duplication: not all shells have functions!
170 if [ $# -ge 1 ]; then command=$1; else command="-usage"; fi
174 -info) # return basic information about changer
176 backwards=`expr 1 - $gravity`
177 answer="$curslot $nslots $backwards"
179 echo "Exit -> $answer" >> $logfile
184 -reset) # reset changer
190 # XXX put changer-specific reset here, if applicable
193 -eject) # eject tape if loaded
200 if [ $curloaded -eq 0 ]; then
201 answer="$curslot $pname: slot already empty"
203 echo "Exit -> $answer" >> $logfile
209 -slot) # change to slot
218 if [ \( $newslot -gt $lastslot \) -o \
219 \( $newslot -lt $firstslot \) ]; then
220 answer="$newslot $pname: no slot $newslot: legal range is $firstslot ... $lastslot"
222 echo "Exit -> $answer" >> $logfile
237 newslot=`expr $curslot + 1`
238 if [ $newslot -gt $lastslot ]; then
241 if [ $slotparm = advance ]; then
246 newslot=`expr $curslot - 1`
247 if [ $newslot -lt $firstslot ]; then
252 answer="<none> $pname: bad slot name \"$slotparm\""
254 echo "Exit -> $answer" >> $logfile
266 if [ $usage -eq 1 ]; then
267 answer="<none> usage: $pname {-reset | -slot [<slot-number>|current|next|prev|advance]}"
269 echo "Exit -> $answer" >> $logfile
275 # check for legal move
277 if [ \( $checkgravity -eq 1 \) -a \( $gravity -ne 0 \) ]; then
278 if [ \( $newslot -lt $curslot \) -o \( "$slotparm" = "prev" \) ]
280 answer="$newslot $pname: cannot go backwards in gravity stacker"
282 echo "Exit -> $answer" >> $logfile
288 # get tape device name
290 device=`awk '$1 == "slot" && $2 == '$newslot' {print $3}' $ourconf 2>/dev/null`
291 if [ "$device" = "" ]; then
292 answer="$newslot $pname: slot $newslot device not specified in $ourconf"
294 echo "Exit -> $answer" >> $logfile
299 # check if load needs an eject first
301 if [ \( $needeject -eq 1 \) -a \( $loadslot -eq 1 \) -a \
302 \( $curloaded -eq 1 \) -a \( $newslot -ne $curslot \) ]; then
307 if [ $ejectslot -eq 1 ]; then # eject the tape from the drive
309 # XXX put changer-specific load command here, if applicable
311 curloaded=0 # unless something goes wrong
314 # generically, first check that the device is there
316 if [ ! -c $device ]; then
317 answer="$newslot $pname: $device: not a device file"
319 echo "Exit -> $answer" >> $logfile
324 # if multiple eject is required, do it now
325 if [ $multieject -eq 1 ]; then
327 while [ $loopslot -lt $newslot ]; do
328 $MT $MTF $device offline >/dev/null 2>&1
329 if [ $? -ne 0 ]; then
330 answer="$newslot $pname: $device: unable to change slot $loopslot"
332 echo "Exit -> $answer" >> $logfile
336 loopslot=`/usr/bin/expr $loopslot + 1`
340 # second, try to unload the device
341 $MT $MTF $device offline >/dev/null 2>&1
342 $CHS deselect -d1 -s$curslot >/dev/null 2>&1
343 if [ $? -ne 0 ]; then
345 # XXX if the changer-specific eject command can distinguish
346 # betweeen "slot empty" and more serious errors, return 1
347 # for the first case, 2 for the second case. Generically,
348 # we just presume an error signifies an empty slot.
351 answer="<none> $pname: tape unload to slot $curslot failed"
353 echo "Exit -> $answer" >> $logfile
361 if [ \( $loadslot -eq 1 \) -a \( \( $curloaded -ne 1 \) -o \( \( $curloaded -eq 1 \) -a \( $newslot -ne $curslot \) \) \) ]; then # load the tape from the slot
363 # XXX put changer-specific load command here, if applicable
365 curloaded=1 # unless something goes wrong
368 # generically, first check that the device is there
370 if [ ! -c $device ]; then
371 answer="$newslot $pname: $device: not a device file"
373 echo "Exit -> $answer" >> $logfile
378 $CHS select -s$newslot -d1 >/dev/null 2>&1
379 if [ $? -ne 0 ]; then
380 answer="<none> $pname: tape load from slot $newslot failed"
382 echo "Exit -> $answer" >> $logfile
388 # second, try to rewind the device
389 $MT $MTF $device rewind >/dev/null 2>&1
390 if [ $? -ne 0 ]; then
392 # XXX if the changer-specific load command can distinguish
393 # betweeen "slot empty" and more serious errors, return 1
394 # for the first case, 2 for the second case. Generically,
395 # we just presume an error signifies an empty slot.
404 echo "# $pname state cache: DO NOT EDIT!" > $ourstate
405 echo curslot $newslot >> $ourstate
406 echo curloaded $curloaded >> $ourstate
410 if [ $slotempty -eq 1 ]; then
411 answer="<nonw> $pname: $newslot slot is empty"
413 echo "Exit -> $answer" >> $logfile
418 if [ "$command" = -slot -a "$slotparm" = advance ]; then
422 answer="$newslot $device"
424 echo "Exit -> $answer" >> $logfile