683b12aba73ad2f7467309ec52759ae023eeb690
[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 USE_VERSION_SUFFIXES="@USE_VERSION_SUFFIXES@"
54 if test "$USE_VERSION_SUFFIXES" = "yes"; then
55         SUF="-@VERSION@"
56 else
57         SUF=
58 fi
59
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"
64 fi
65
66 # read in some config parameters
67
68 if [ \! -f $ourconf ]; then
69         answer=`_ '<none> %s: %s does not exist' "$pname" "$ourconf"`
70         echo `_ 'Exit ->'` $answer >> $logfile
71         echo $answer
72         exit 2
73 fi
74
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
79         echo $answer
80         exit 2
81 fi
82
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
87         echo $answer
88         exit 2
89 fi
90
91 nslots=`expr $lastslot - $firstslot + 1`
92
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
97         echo $answer
98         exit 2
99 fi
100
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
105         echo $answer
106         exit 2
107 fi
108
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
112         multieject=0
113 fi
114
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
118         ejectdelay=0
119 fi
120
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
125         echo $answer
126         exit 2
127 fi
128
129 # read in state: only curslot and curloaded at the present time
130
131 curslot=`awk '$1 == "curslot" {print $2}' $ourstate 2>/dev/null`
132 if [ "$curslot" = "" ]; then
133         curslot=$firstslot
134 fi
135
136 curloaded=`awk '$1 == "curloaded" {print $2}' $ourstate 2>/dev/null`
137 if [ "$curloaded" = "" ]; then
138         curloaded=0
139 fi
140
141
142 # process the command-line
143
144 # control vars to avoid code duplication: not all shells have functions!
145 usage=0
146 checkgravity=0
147 ejectslot=0
148 loadslot=0
149 slotempty=0
150
151 if [ $# -ge 1 ]; then command=$1; else command="-usage"; fi
152
153 case "$command" in
154
155 -info) # return basic information about changer
156
157         backwards=`expr 1 - $gravity`
158         answer="$curslot $nslots $backwards"
159         echo `_ 'Exit ->'` $answer >> $logfile
160         echo $answer
161         exit 0
162         ;;
163
164 -reset) # reset changer
165
166         checkgravity=0
167         loadslot=1
168         newslot=$firstslot
169
170         # XXX put changer-specific reset here, if applicable
171         ;;
172
173 -eject) # eject tape if loaded
174
175         checkgravity=0
176         loadslot=0
177         newslot=$curslot
178         ejectslot=1
179
180         if [ $curloaded -eq 0 ]; then
181                 answer=`_ '%s %s: slot already empty' "$curslot" "$pname"`
182                 echo `_ 'Exit ->'` $answer >> $logfile
183                 echo $answer
184                 exit 1
185         fi
186         ;;
187
188 -slot)  # change to slot
189
190         checkgravity=1
191         loadslot=1
192
193         slotparm=$2
194         case "$slotparm" in
195         [0-9]*) 
196                 newslot=$slotparm
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
201                         echo $answer
202                         exit 1
203                 fi
204                 ;;
205         current)
206                 newslot=$curslot
207                 ;;
208         first)
209                 newslot=$firstslot
210                 ;;
211         last)
212                 newslot=$lastslot
213                 ;;
214         next|advance)
215                 newslot=`expr $curslot + 1`
216                 if [ $newslot -gt $lastslot ]; then
217                         newslot=$firstslot
218                 fi
219                 if [ $slotparm = advance ]; then
220                         loadslot=0
221                 fi
222                 ;;
223         prev)
224                 newslot=`expr $curslot - 1`
225                 if [ $newslot -lt $firstslot ]; then
226                         newslot=$lastslot
227                 fi
228                 ;;
229         *)
230                 answer=`_ '<none> %s: bad slot name "%s"' "$pname" "$slotparm"`
231                 echo `_ 'Exit ->'` "$answer" >> $logfile
232                 echo $answer
233                 exit 1
234                 ;;
235         esac
236         ;;
237 *)
238         usage=1
239         ;;
240 esac
241
242
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
246         echo $answer
247         exit 2
248 fi
249
250
251 # check for legal move
252
253 if [ \( $checkgravity -eq 1 \) -a \( $gravity -ne 0 \) ]; then
254         if [ \( $newslot -lt $curslot \) -o \( "$slotparm" = "prev" \) ]
255         then
256                 answer=`_ '%s %s: cannot go backwards in gravity stacker' "$newslot" "$pname"`
257                 echo `_ 'Exit ->'` $answer >> $logfile
258                 echo $answer
259                 exit 1
260         fi
261 fi
262
263 # get tape device name
264
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
269         echo $answer
270         exit 2
271 fi
272
273 # check if load needs an eject first
274
275 if [ \( $needeject -eq 1 \) -a \( $loadslot -eq 1 \) -a \
276      \( $curloaded -eq 1 \) -a \( $newslot -ne $curslot \) ]; then
277         ejectslot=1
278 fi
279
280
281 if [ $ejectslot -eq 1 ]; then   # eject the tape from the drive
282
283         # XXX put changer-specific load command here, if applicable
284
285         curloaded=0             # unless something goes wrong
286         slotempty=0
287
288         # generically, first check that the device is there
289
290         if [ ! -c $device ]; then
291                 answer=`_ '%s %s: %s: not a device file' "$newslot" "$pname" "$device"`
292                 echo `_ 'Exit ->'` $answer >> $logfile
293                 echo $answer
294                 exit 2
295         fi
296
297         # if multiple eject is required, do it now
298         if [ $multieject -eq 1 ]; then
299                 loopslot=$curslot
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
305                                 echo $answer
306                                 exit 2
307                         fi
308                         loopslot=`/usr/bin/expr $loopslot + 1`
309                 done
310         fi
311   
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
316                 #
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.
321                 #
322                 #slotempty=1
323                 answer=`_ '<none> %s: tape unload to slot %s failed' "$pname" "$curslot"`
324                 echo `_ 'Exit ->'` $answer >> $logfile
325                 echo $answer
326                 exit 2
327         else
328                 sleep $ejectdelay
329         fi
330 fi
331
332 if [ \( $loadslot -eq 1 \) -a \( \( $curloaded -ne 1 \) -o \( \( $curloaded -eq 1 \) -a \( $newslot -ne $curslot \) \) \) ]; then       # load the tape from the slot
333
334         # XXX put changer-specific load command here, if applicable
335
336         curloaded=1             # unless something goes wrong
337         slotempty=0
338
339         # generically, first check that the device is there
340
341         if [ ! -c $device ]; then
342                 answer=`_ '%s %s: %s: not a device file' "$newslot" "$pname" "$device"`
343                 echo `_ 'Exit ->'` $answer >> $logfile
344                 echo $answer
345                 exit 2
346         fi
347
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
352                 echo $answer
353                 exit 2
354         fi
355         sleep 60
356
357         # second, try to rewind the device
358         amdevcheck_status $device
359         if [ $? -ne 0 ]; then
360                 #
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.
365                 #
366                 slotempty=1
367                 curloaded=0
368         fi
369 fi
370
371 # update state
372
373 echo "# $pname state cache: DO NOT EDIT!"       >  $ourstate
374 echo curslot $newslot                           >> $ourstate
375 echo curloaded $curloaded                       >> $ourstate
376
377 # return slot info
378
379 if [ $slotempty -eq 1 ]; then
380         answer=`_ '<none> %s: %s slot is empty: %s' "$pname" "$newslot" "$amdevcheck_message"`
381         echo `_ 'Exit ->'` $answer >> $logfile
382         echo $answer
383         exit 1
384 fi
385
386 if [ "$command" = -slot -a "$slotparm" = advance ]; then
387         device=/dev/null
388 fi
389
390 answer="$newslot $device"
391 echo `_ 'Exit ->'` $answer >> $logfile
392 echo $answer
393 exit 0