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 amlibexecdir="@amlibexecdir@"
37 . "${amlibexecdir}/amanda-sh-lib.sh"
41 # add sbin and ucb dirs
42 PATH="$PATH:/usr/sbin:/sbin:/usr/ucb"
45 if [ -d "@AMANDA_DBGDIR@" ]; then
46 logfile=@AMANDA_DBGDIR@/changer.debug
53 USE_VERSION_SUFFIXES="@USE_VERSION_SUFFIXES@"
54 if test "$USE_VERSION_SUFFIXES" = "yes"; then
60 ourconf=`amgetconf$SUF changerfile`
61 changerdev=`amgetconf$SUF changerdev`
62 if test -n "$changerdev" && test x"$changerdev" != x/dev/null; then
63 CHS="$CHS -f$changerdev"
66 # read in some config parameters
68 if [ \! -f $ourconf ]; then
69 answer=`_ '<none> %s: %s does not exist' "$pname" "$ourconf"`
70 echo `_ 'Exit ->'` $answer >> $logfile
75 firstslot=`awk '$1 == "firstslot" {print $2}' $ourconf 2>/dev/null`
76 if [ "$firstslot" = "" ]; then
77 answer=`_ '<none> %s: firstslot not specified in %s' "$pname" "$ourconf"`
78 echo `_ 'Exit ->'` $answer >> $logfile
83 lastslot=`awk '$1 == "lastslot" {print $2}' $ourconf 2>/dev/null`
84 if [ "$lastslot" = "" ]; then
85 answer=`_ '<none> %s: lastslot not specified in %s' "$pname" "$ourconf"`
86 echo `_ 'Exit ->'` $answer >> $logfile
91 nslots=`expr $lastslot - $firstslot + 1`
93 gravity=`awk '$1 == "gravity" {print $2}' $ourconf 2>/dev/null`
94 if [ "$gravity" = "" ]; then
95 answer=`_ '<none> %s: gravity not specified in %s' "$pname" "$ourconf"`
96 echo `_ 'Exit ->'` $answer >> $logfile
101 needeject=`awk '$1 == "needeject" {print $2}' $ourconf 2>/dev/null`
102 if [ "$needeject" = "" ]; then
103 answer=`_ '<none> %s: needeject not specified in %s' "$pname" "$ourconf"`
104 echo `_ 'Exit ->'` $answer >> $logfile
109 multieject=`awk '$1 == "multieject" {print $2}' $ourconf 2>/dev/null`
110 if [ "$multieject" = "" ]; then
111 echo `_ 'Note -> multieject not specified in %s' "$ourconf"` >> $logfile
115 ejectdelay=`awk '$1 == "ejectdelay" {print $2}' $ourconf 2>/dev/null`
116 if [ "$ejectdelay" = "" ]; then
117 echo `_ 'Note -> ejectdelay not specified in %s' "$ourconf"` >> $logfile
121 ourstate=`awk '$1 == "statefile" {print $2}' $ourconf 2>/dev/null`
122 if [ "$ourstate" = "" ]; then
123 answer=`_ '<none> %s: statefile not specified in %s' "$pname" "$ourconf"`
124 echo `_ 'Exit ->'` $answer >> $logfile
129 # read in state: only curslot and curloaded at the present time
131 curslot=`awk '$1 == "curslot" {print $2}' $ourstate 2>/dev/null`
132 if [ "$curslot" = "" ]; then
136 curloaded=`awk '$1 == "curloaded" {print $2}' $ourstate 2>/dev/null`
137 if [ "$curloaded" = "" ]; then
142 # process the command-line
144 # control vars to avoid code duplication: not all shells have functions!
151 if [ $# -ge 1 ]; then command=$1; else command="-usage"; fi
155 -info) # return basic information about changer
157 backwards=`expr 1 - $gravity`
158 answer="$curslot $nslots $backwards"
159 echo `_ 'Exit ->'` $answer >> $logfile
164 -reset) # reset changer
170 # XXX put changer-specific reset here, if applicable
173 -eject) # eject tape if loaded
180 if [ $curloaded -eq 0 ]; then
181 answer=`_ '%s %s: slot already empty' "$curslot" "$pname"`
182 echo `_ 'Exit ->'` $answer >> $logfile
188 -slot) # change to slot
197 if [ \( $newslot -gt $lastslot \) -o \
198 \( $newslot -lt $firstslot \) ]; then
199 answer =`_ '%s %s: no slot %s: legal range is %s ... %s' "$newslot" "$pname" "$newslot" "$firstslot" "$lastslot"`
200 echo `_ 'Exit ->'` $answer >> $logfile
215 newslot=`expr $curslot + 1`
216 if [ $newslot -gt $lastslot ]; then
219 if [ $slotparm = advance ]; then
224 newslot=`expr $curslot - 1`
225 if [ $newslot -lt $firstslot ]; then
230 answer=`_ '<none> %s: bad slot name "%s"' "$pname" "$slotparm"`
231 echo `_ 'Exit ->'` "$answer" >> $logfile
243 if [ $usage -eq 1 ]; then
244 answer=`_ '<none> usage: %s {-reset | -slot [<slot-number>|current|next|prev|advance]}' "$pname"`
245 echo `_ 'Exit ->'` $answer >> $logfile
251 # check for legal move
253 if [ \( $checkgravity -eq 1 \) -a \( $gravity -ne 0 \) ]; then
254 if [ \( $newslot -lt $curslot \) -o \( "$slotparm" = "prev" \) ]
256 answer=`_ '%s %s: cannot go backwards in gravity stacker' "$newslot" "$pname"`
257 echo `_ 'Exit ->'` $answer >> $logfile
263 # get tape device name
265 device=`awk '$1 == "slot" && $2 == '$newslot' {print $3}' $ourconf 2>/dev/null`
266 if [ "$device" = "" ]; then
267 answer=`_ '%s %s: slot %s device not specified in %s' "$newslot" "$pname" "$newslot" "$ourconf"`
268 echo `_ 'Exit ->'` $answer >> $logfile
273 # check if load needs an eject first
275 if [ \( $needeject -eq 1 \) -a \( $loadslot -eq 1 \) -a \
276 \( $curloaded -eq 1 \) -a \( $newslot -ne $curslot \) ]; then
281 if [ $ejectslot -eq 1 ]; then # eject the tape from the drive
283 # XXX put changer-specific load command here, if applicable
285 curloaded=0 # unless something goes wrong
288 # generically, first check that the device is there
290 if [ ! -c $device ]; then
291 answer=`_ '%s %s: %s: not a device file' "$newslot" "$pname" "$device"`
292 echo `_ 'Exit ->'` $answer >> $logfile
297 # if multiple eject is required, do it now
298 if [ $multieject -eq 1 ]; then
300 while [ $loopslot -lt $newslot ]; do
301 try_eject_device $device
302 if [ $? -ne 0 ]; then
303 answer=`_ '%s %s: %s: unable to change slot %s' "$newslot" "$pname" "$device" "$loopslot"`
304 echo `_ 'Exit ->'` $answer >> $logfile
308 loopslot=`/usr/bin/expr $loopslot + 1`
312 # second, try to unload the device
313 try_eject_device $device
314 $CHS deselect -d1 -s$curslot >/dev/null 2>&1
315 if [ $? -ne 0 ]; then
317 # XXX if the changer-specific eject command can distinguish
318 # betweeen "slot empty" and more serious errors, return 1
319 # for the first case, 2 for the second case. Generically,
320 # we just presume an error signifies an empty slot.
323 answer=`_ '<none> %s: tape unload to slot %s failed' "$pname" "$curslot"`
324 echo `_ 'Exit ->'` $answer >> $logfile
332 if [ \( $loadslot -eq 1 \) -a \( \( $curloaded -ne 1 \) -o \( \( $curloaded -eq 1 \) -a \( $newslot -ne $curslot \) \) \) ]; then # load the tape from the slot
334 # XXX put changer-specific load command here, if applicable
336 curloaded=1 # unless something goes wrong
339 # generically, first check that the device is there
341 if [ ! -c $device ]; then
342 answer=`_ '%s %s: %s: not a device file' "$newslot" "$pname" "$device"`
343 echo `_ 'Exit ->'` $answer >> $logfile
348 $CHS select -s$newslot -d1 >/dev/null 2>&1
349 if [ $? -ne 0 ]; then
350 answer=`_ '<none> %s: tape load from slot %s failed' "$pname" "$newslot"`
351 echo `_ 'Exit ->'` $answer >> $logfile
357 # second, try to rewind the device
358 amdevcheck_status $device
359 if [ $? -ne 0 ]; then
361 # XXX if the changer-specific load command can distinguish
362 # betweeen "slot empty" and more serious errors, return 1
363 # for the first case, 2 for the second case. Generically,
364 # we just presume an error signifies an empty slot.
373 echo "# $pname state cache: DO NOT EDIT!" > $ourstate
374 echo curslot $newslot >> $ourstate
375 echo curloaded $curloaded >> $ourstate
379 if [ $slotempty -eq 1 ]; then
380 answer=`_ '<none> %s: %s slot is empty: %s' "$pname" "$newslot" "$amdevcheck_message"`
381 echo `_ 'Exit ->'` $answer >> $logfile
386 if [ "$command" = -slot -a "$slotparm" = advance ]; then
390 answer="$newslot $device"
391 echo `_ 'Exit ->'` $answer >> $logfile