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 ourconf=`amgetconf changerfile`
54 changerdev=`amgetconf changerdev`
55 if test -n "$changerdev" && test x"$changerdev" != x/dev/null; then
56 CHS="$CHS -f$changerdev"
59 # read in some config parameters
61 if [ \! -f $ourconf ]; then
62 answer=`_ '<none> %s: %s does not exist' "$pname" "$ourconf"`
63 echo `_ 'Exit ->'` $answer >> $logfile
68 firstslot=`awk '$1 == "firstslot" {print $2}' $ourconf 2>/dev/null`
69 if [ "$firstslot" = "" ]; then
70 answer=`_ '<none> %s: firstslot not specified in %s' "$pname" "$ourconf"`
71 echo `_ 'Exit ->'` $answer >> $logfile
76 lastslot=`awk '$1 == "lastslot" {print $2}' $ourconf 2>/dev/null`
77 if [ "$lastslot" = "" ]; then
78 answer=`_ '<none> %s: lastslot not specified in %s' "$pname" "$ourconf"`
79 echo `_ 'Exit ->'` $answer >> $logfile
84 nslots=`expr $lastslot - $firstslot + 1`
86 gravity=`awk '$1 == "gravity" {print $2}' $ourconf 2>/dev/null`
87 if [ "$gravity" = "" ]; then
88 answer=`_ '<none> %s: gravity not specified in %s' "$pname" "$ourconf"`
89 echo `_ 'Exit ->'` $answer >> $logfile
94 needeject=`awk '$1 == "needeject" {print $2}' $ourconf 2>/dev/null`
95 if [ "$needeject" = "" ]; then
96 answer=`_ '<none> %s: needeject not specified in %s' "$pname" "$ourconf"`
97 echo `_ 'Exit ->'` $answer >> $logfile
102 multieject=`awk '$1 == "multieject" {print $2}' $ourconf 2>/dev/null`
103 if [ "$multieject" = "" ]; then
104 echo `_ 'Note -> multieject not specified in %s' "$ourconf"` >> $logfile
108 ejectdelay=`awk '$1 == "ejectdelay" {print $2}' $ourconf 2>/dev/null`
109 if [ "$ejectdelay" = "" ]; then
110 echo `_ 'Note -> ejectdelay not specified in %s' "$ourconf"` >> $logfile
114 ourstate=`awk '$1 == "statefile" {print $2}' $ourconf 2>/dev/null`
115 if [ "$ourstate" = "" ]; then
116 answer=`_ '<none> %s: statefile not specified in %s' "$pname" "$ourconf"`
117 echo `_ 'Exit ->'` $answer >> $logfile
122 # read in state: only curslot and curloaded at the present time
124 curslot=`awk '$1 == "curslot" {print $2}' $ourstate 2>/dev/null`
125 if [ "$curslot" = "" ]; then
129 curloaded=`awk '$1 == "curloaded" {print $2}' $ourstate 2>/dev/null`
130 if [ "$curloaded" = "" ]; then
135 # process the command-line
137 # control vars to avoid code duplication: not all shells have functions!
144 if [ $# -ge 1 ]; then command=$1; else command="-usage"; fi
148 -info) # return basic information about changer
150 backwards=`expr 1 - $gravity`
151 answer="$curslot $nslots $backwards"
152 echo `_ 'Exit ->'` $answer >> $logfile
157 -reset) # reset changer
163 # XXX put changer-specific reset here, if applicable
166 -eject) # eject tape if loaded
173 if [ $curloaded -eq 0 ]; then
174 answer=`_ '%s %s: slot already empty' "$curslot" "$pname"`
175 echo `_ 'Exit ->'` $answer >> $logfile
181 -slot) # change to slot
190 if [ \( $newslot -gt $lastslot \) -o \
191 \( $newslot -lt $firstslot \) ]; then
192 answer =`_ '%s %s: no slot %s: legal range is %s ... %s' "$newslot" "$pname" "$newslot" "$firstslot" "$lastslot"`
193 echo `_ 'Exit ->'` $answer >> $logfile
208 newslot=`expr $curslot + 1`
209 if [ $newslot -gt $lastslot ]; then
212 if [ $slotparm = advance ]; then
217 newslot=`expr $curslot - 1`
218 if [ $newslot -lt $firstslot ]; then
223 answer=`_ '<none> %s: bad slot name "%s"' "$pname" "$slotparm"`
224 echo `_ 'Exit ->'` "$answer" >> $logfile
236 if [ $usage -eq 1 ]; then
237 answer=`_ '<none> usage: %s {-reset | -slot [<slot-number>|current|next|prev|advance]}' "$pname"`
238 echo `_ 'Exit ->'` $answer >> $logfile
244 # check for legal move
246 if [ \( $checkgravity -eq 1 \) -a \( $gravity -ne 0 \) ]; then
247 if [ \( $newslot -lt $curslot \) -o \( "$slotparm" = "prev" \) ]
249 answer=`_ '%s %s: cannot go backwards in gravity stacker' "$newslot" "$pname"`
250 echo `_ 'Exit ->'` $answer >> $logfile
256 # get tape device name
258 device=`awk '$1 == "slot" && $2 == '$newslot' {print $3}' $ourconf 2>/dev/null`
259 if [ "$device" = "" ]; then
260 answer=`_ '%s %s: slot %s device not specified in %s' "$newslot" "$pname" "$newslot" "$ourconf"`
261 echo `_ 'Exit ->'` $answer >> $logfile
266 # check if load needs an eject first
268 if [ \( $needeject -eq 1 \) -a \( $loadslot -eq 1 \) -a \
269 \( $curloaded -eq 1 \) -a \( $newslot -ne $curslot \) ]; then
274 if [ $ejectslot -eq 1 ]; then # eject the tape from the drive
276 # XXX put changer-specific load command here, if applicable
278 curloaded=0 # unless something goes wrong
281 # generically, first check that the device is there
283 if [ ! -c $device ]; then
284 answer=`_ '%s %s: %s: not a device file' "$newslot" "$pname" "$device"`
285 echo `_ 'Exit ->'` $answer >> $logfile
290 # if multiple eject is required, do it now
291 if [ $multieject -eq 1 ]; then
293 while [ $loopslot -lt $newslot ]; do
294 try_eject_device $device
295 if [ $? -ne 0 ]; then
296 answer=`_ '%s %s: %s: unable to change slot %s' "$newslot" "$pname" "$device" "$loopslot"`
297 echo `_ 'Exit ->'` $answer >> $logfile
301 loopslot=`/usr/bin/expr $loopslot + 1`
305 # second, try to unload the device
306 try_eject_device $device
307 $CHS deselect -d1 -s$curslot >/dev/null 2>&1
308 if [ $? -ne 0 ]; then
310 # XXX if the changer-specific eject command can distinguish
311 # betweeen "slot empty" and more serious errors, return 1
312 # for the first case, 2 for the second case. Generically,
313 # we just presume an error signifies an empty slot.
316 answer=`_ '<none> %s: tape unload to slot %s failed' "$pname" "$curslot"`
317 echo `_ 'Exit ->'` $answer >> $logfile
325 if [ \( $loadslot -eq 1 \) -a \( \( $curloaded -ne 1 \) -o \( \( $curloaded -eq 1 \) -a \( $newslot -ne $curslot \) \) \) ]; then # load the tape from the slot
327 # XXX put changer-specific load command here, if applicable
329 curloaded=1 # unless something goes wrong
332 # generically, first check that the device is there
334 if [ ! -c $device ]; then
335 answer=`_ '%s %s: %s: not a device file' "$newslot" "$pname" "$device"`
336 echo `_ 'Exit ->'` $answer >> $logfile
341 $CHS select -s$newslot -d1 >/dev/null 2>&1
342 if [ $? -ne 0 ]; then
343 answer=`_ '<none> %s: tape load from slot %s failed' "$pname" "$newslot"`
344 echo `_ 'Exit ->'` $answer >> $logfile
350 # second, try to rewind the device
351 amdevcheck_status $device
352 if [ $? -ne 0 ]; then
354 # XXX if the changer-specific load command can distinguish
355 # betweeen "slot empty" and more serious errors, return 1
356 # for the first case, 2 for the second case. Generically,
357 # we just presume an error signifies an empty slot.
366 echo "# $pname state cache: DO NOT EDIT!" > $ourstate
367 echo curslot $newslot >> $ourstate
368 echo curloaded $curloaded >> $ourstate
372 if [ $slotempty -eq 1 ]; then
373 answer=`_ '<none> %s: %s slot is empty: %s' "$pname" "$newslot" "$amdevcheck_message"`
374 echo `_ 'Exit ->'` $answer >> $logfile
379 if [ "$command" = -slot -a "$slotparm" = advance ]; then
383 answer="$newslot $device"
384 echo `_ 'Exit ->'` $answer >> $logfile