Imported Upstream version 3.1.0
[debian/amanda] / changer-src / chg-chs.sh
1 #!@SHELL@
2 #
3 # Amanda, The Advanced Maryland Automatic Network Disk Archiver
4 # Copyright (c) 1991-1998 University of Maryland at College Park
5 # All Rights Reserved.
6 #
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.
16 #
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.
23 #
24 # Author: James da Silva, Systems Design and Analysis Group
25 #                          Computer Science Department
26 #                          University of Maryland at College Park
27 #
28
29 #
30 # chg-chs.sh - chs tape changer script
31 #
32
33 prefix="@prefix@"
34 exec_prefix="@exec_prefix@"
35 sbindir="@sbindir@"
36 amlibexecdir="@amlibexecdir@"
37 . "${amlibexecdir}/amanda-sh-lib.sh"
38
39 pname="chg-chs"
40
41 # add sbin and ucb dirs
42 PATH="$PATH:/usr/sbin:/sbin:/usr/ucb"
43 export PATH
44
45 if [ -d "@AMANDA_DBGDIR@" ]; then
46         logfile=@AMANDA_DBGDIR@/changer.debug
47 else
48         logfile=/dev/null
49 fi
50
51 CHS=@CHS@
52
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"
57 fi
58
59 # read in some config parameters
60
61 if [ \! -f $ourconf ]; then
62         answer=`_ '<none> %s: %s does not exist' "$pname" "$ourconf"`
63         echo `_ 'Exit ->'` $answer >> $logfile
64         echo $answer
65         exit 2
66 fi
67
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
72         echo $answer
73         exit 2
74 fi
75
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
80         echo $answer
81         exit 2
82 fi
83
84 nslots=`expr $lastslot - $firstslot + 1`
85
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
90         echo $answer
91         exit 2
92 fi
93
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
98         echo $answer
99         exit 2
100 fi
101
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
105         multieject=0
106 fi
107
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
111         ejectdelay=0
112 fi
113
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
118         echo $answer
119         exit 2
120 fi
121
122 # read in state: only curslot and curloaded at the present time
123
124 curslot=`awk '$1 == "curslot" {print $2}' $ourstate 2>/dev/null`
125 if [ "$curslot" = "" ]; then
126         curslot=$firstslot
127 fi
128
129 curloaded=`awk '$1 == "curloaded" {print $2}' $ourstate 2>/dev/null`
130 if [ "$curloaded" = "" ]; then
131         curloaded=0
132 fi
133
134
135 # process the command-line
136
137 # control vars to avoid code duplication: not all shells have functions!
138 usage=0
139 checkgravity=0
140 ejectslot=0
141 loadslot=0
142 slotempty=0
143
144 if [ $# -ge 1 ]; then command=$1; else command="-usage"; fi
145
146 case "$command" in
147
148 -info) # return basic information about changer
149
150         backwards=`expr 1 - $gravity`
151         answer="$curslot $nslots $backwards"
152         echo `_ 'Exit ->'` $answer >> $logfile
153         echo $answer
154         exit 0
155         ;;
156
157 -reset) # reset changer
158
159         checkgravity=0
160         loadslot=1
161         newslot=$firstslot
162
163         # XXX put changer-specific reset here, if applicable
164         ;;
165
166 -eject) # eject tape if loaded
167
168         checkgravity=0
169         loadslot=0
170         newslot=$curslot
171         ejectslot=1
172
173         if [ $curloaded -eq 0 ]; then
174                 answer=`_ '%s %s: slot already empty' "$curslot" "$pname"`
175                 echo `_ 'Exit ->'` $answer >> $logfile
176                 echo $answer
177                 exit 1
178         fi
179         ;;
180
181 -slot)  # change to slot
182
183         checkgravity=1
184         loadslot=1
185
186         slotparm=$2
187         case "$slotparm" in
188         [0-9]*) 
189                 newslot=$slotparm
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
194                         echo $answer
195                         exit 1
196                 fi
197                 ;;
198         current)
199                 newslot=$curslot
200                 ;;
201         first)
202                 newslot=$firstslot
203                 ;;
204         last)
205                 newslot=$lastslot
206                 ;;
207         next|advance)
208                 newslot=`expr $curslot + 1`
209                 if [ $newslot -gt $lastslot ]; then
210                         newslot=$firstslot
211                 fi
212                 if [ $slotparm = advance ]; then
213                         loadslot=0
214                 fi
215                 ;;
216         prev)
217                 newslot=`expr $curslot - 1`
218                 if [ $newslot -lt $firstslot ]; then
219                         newslot=$lastslot
220                 fi
221                 ;;
222         *)
223                 answer=`_ '<none> %s: bad slot name "%s"' "$pname" "$slotparm"`
224                 echo `_ 'Exit ->'` "$answer" >> $logfile
225                 echo $answer
226                 exit 1
227                 ;;
228         esac
229         ;;
230 *)
231         usage=1
232         ;;
233 esac
234
235
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
239         echo $answer
240         exit 2
241 fi
242
243
244 # check for legal move
245
246 if [ \( $checkgravity -eq 1 \) -a \( $gravity -ne 0 \) ]; then
247         if [ \( $newslot -lt $curslot \) -o \( "$slotparm" = "prev" \) ]
248         then
249                 answer=`_ '%s %s: cannot go backwards in gravity stacker' "$newslot" "$pname"`
250                 echo `_ 'Exit ->'` $answer >> $logfile
251                 echo $answer
252                 exit 1
253         fi
254 fi
255
256 # get tape device name
257
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
262         echo $answer
263         exit 2
264 fi
265
266 # check if load needs an eject first
267
268 if [ \( $needeject -eq 1 \) -a \( $loadslot -eq 1 \) -a \
269      \( $curloaded -eq 1 \) -a \( $newslot -ne $curslot \) ]; then
270         ejectslot=1
271 fi
272
273
274 if [ $ejectslot -eq 1 ]; then   # eject the tape from the drive
275
276         # XXX put changer-specific load command here, if applicable
277
278         curloaded=0             # unless something goes wrong
279         slotempty=0
280
281         # generically, first check that the device is there
282
283         if [ ! -c $device ]; then
284                 answer=`_ '%s %s: %s: not a device file' "$newslot" "$pname" "$device"`
285                 echo `_ 'Exit ->'` $answer >> $logfile
286                 echo $answer
287                 exit 2
288         fi
289
290         # if multiple eject is required, do it now
291         if [ $multieject -eq 1 ]; then
292                 loopslot=$curslot
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
298                                 echo $answer
299                                 exit 2
300                         fi
301                         loopslot=`/usr/bin/expr $loopslot + 1`
302                 done
303         fi
304   
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
309                 #
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.
314                 #
315                 #slotempty=1
316                 answer=`_ '<none> %s: tape unload to slot %s failed' "$pname" "$curslot"`
317                 echo `_ 'Exit ->'` $answer >> $logfile
318                 echo $answer
319                 exit 2
320         else
321                 sleep $ejectdelay
322         fi
323 fi
324
325 if [ \( $loadslot -eq 1 \) -a \( \( $curloaded -ne 1 \) -o \( \( $curloaded -eq 1 \) -a \( $newslot -ne $curslot \) \) \) ]; then       # load the tape from the slot
326
327         # XXX put changer-specific load command here, if applicable
328
329         curloaded=1             # unless something goes wrong
330         slotempty=0
331
332         # generically, first check that the device is there
333
334         if [ ! -c $device ]; then
335                 answer=`_ '%s %s: %s: not a device file' "$newslot" "$pname" "$device"`
336                 echo `_ 'Exit ->'` $answer >> $logfile
337                 echo $answer
338                 exit 2
339         fi
340
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
345                 echo $answer
346                 exit 2
347         fi
348         sleep 60
349
350         # second, try to rewind the device
351         amdevcheck_status $device
352         if [ $? -ne 0 ]; then
353                 #
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.
358                 #
359                 slotempty=1
360                 curloaded=0
361         fi
362 fi
363
364 # update state
365
366 echo "# $pname state cache: DO NOT EDIT!"       >  $ourstate
367 echo curslot $newslot                           >> $ourstate
368 echo curloaded $curloaded                       >> $ourstate
369
370 # return slot info
371
372 if [ $slotempty -eq 1 ]; then
373         answer=`_ '<none> %s: %s slot is empty: %s' "$pname" "$newslot" "$amdevcheck_message"`
374         echo `_ 'Exit ->'` $answer >> $logfile
375         echo $answer
376         exit 1
377 fi
378
379 if [ "$command" = -slot -a "$slotparm" = advance ]; then
380         device=/dev/null
381 fi
382
383 answer="$newslot $device"
384 echo `_ 'Exit ->'` $answer >> $logfile
385 echo $answer
386 exit 0