resolve potential bashisms
[debian/amanda] / changer-src / chg-mcutil.sh
1 #!@SHELL@ 
2 #
3 # Author: Robert Dege
4 #
5 #
6 # version 1.2
7 # -----------
8 # fixed last_cleaned file so that if it doesn't exist, it gets created with current date, not '0,0'
9 # fixed a bug that was reporting the wrong slot # to amcheck
10 #
11 # version 1.1
12 # -----------
13 # amverify was failing when using -slot current.  Fixed exit $code from 1 -> 0.
14 # removed useless $current variables from movetape() function.
15 #
16 #
17 #
18 # Exit Status:
19 # 0 Alles Ok
20 # 1 Illegal Request
21 # 2 Fatal Error
22 #
23
24
25 #
26 # Set Path so that it includes Amanda binaries, and access to tapechanger & drive programs
27 #
28 prefix="@prefix@"
29 exec_prefix="@exec_prefix@"
30 sbindir="@sbindir@"
31 amlibexecdir="@amlibexecdir@"
32 . "${amlibexecdir}/amanda-sh-lib.sh"
33
34 # add sbin and ucb dirs
35 PATH="$PATH:/usr/sbin:/sbin:/usr/ucb"
36 export PATH
37
38
39 #
40 # Define Suffix for amanda binaries
41 #
42 USE_VERSION_SUFFIXES="@USE_VERSION_SUFFIXES@"
43 if test "$USE_VERSION_SUFFIXES" = "yes"; then
44         SUF="-@VERSION@"
45 else
46         SUF=
47 fi
48
49 #
50 # Load configuration data from the config file
51 #
52
53 ourconf=`amgetconf$SUF changerfile`
54 myname=$0
55
56
57 if [ ! -f "$ourconf" ]; then
58         code=2
59         echo `_ 'Command Line ->'` $myname $@
60         echo `_ 'Exit(%s): %s not found as listed in amanda.conf' "$code" "$ourconf"` 1>&2
61         exit $code
62 fi
63
64
65 # grab mcutil info
66 tmpval1=`grep ^mcutil $ourconf | awk -F\  '{print $2}'`
67 tmpval2=`grep ^mcutil $ourconf | awk -F= '{print $2}'`
68 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
69         code=2
70         echo `_ 'Command Line ->'` $myname $@
71         echo `_ 'Exit(%s): mcutil not specified in %s' "$code" "$ourconf"` 1>&2
72         exit $code
73 elif [ -z "$tmpval1" ]; then
74         MCUTIL=$tmpval2
75 else
76         MCUTIL=$tmpval1
77 fi
78
79
80 # grab tape info
81 tmpval1=`grep ^tape $ourconf | awk -F\  '{print $2}'`
82 tmpval2=`grep ^tape $ourconf | awk -F= '{print $2}'`
83 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
84         code=2
85         echo `_ 'Command Line ->'` $myname $@
86         echo `_ 'Exit(%s): tape not specified in %s' "$code" "$ourconf"` 1>&2
87         exit $code
88 elif [ -z "$tmpval1" ]; then
89         tape=$tmpval2
90 else
91         tape=$tmpval1
92 fi
93
94
95 # grab firstslot info
96 tmpval1=`grep ^firstslot $ourconf | awk -F\  '{print $2}'`
97 tmpval2=`grep ^firstslot $ourconf | awk -F= '{print $2}'`
98 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
99         code=2
100         echo `_ 'Command Line ->'` $myname $@
101         echo `_ 'Exit(%s): firstslot not specified in %s' "$code" "$ourconf"` 1>&2
102         exit $code
103 elif [ -z "$tmpval1" ]; then
104         firstslot=$tmpval2
105 else
106         firstslot=$tmpval1
107 fi
108
109
110 # grab lastslot info
111 tmpval1=`grep ^lastslot $ourconf | awk -F\  '{print $2}'`
112 tmpval2=`grep ^lastslot $ourconf | awk -F= '{print $2}'`
113 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
114         code=2
115         echo `_ 'Command Line ->'` $myname $@
116         echo `_ 'Exit(%s): lastslot not specified in %s' "$code" "$ourconf"` 1>&2
117 elif [ -z "$tmpval1" ]; then
118         lastslot=$tmpval2
119 else
120         lastslot=$tmpval1
121 fi
122
123
124 # grab use_cleaning info
125 tmpval1=`grep ^use_cleaning $ourconf | awk -F\  '{print $2}'`
126 tmpval2=`grep ^use_cleaning $ourconf | awk -F= '{print $2}'`
127 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
128         code=2
129         echo `_ 'Command Line ->'` $myname $@
130         echo `_ 'Exit(%s): use_cleaning not specified in %s' "$code" "$ourconf"` 1>&2
131         exit $code
132 elif [ -z "$tmpval1" ]; then
133         use_cleaning=$tmpval2
134 else
135         use_cleaning=$tmpval1
136 fi
137
138
139 # grab cleanslot info
140 tmpval1=`grep ^cleanslot $ourconf | awk -F\  '{print $2}'`
141 tmpval2=`grep ^cleanslot $ourconf | awk -F= '{print $2}'`
142 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
143         code=2
144         echo `_ 'Command Line ->'` $myname $@
145         echo `_ 'Exit(%s): cleanslot not specified in %s' "$code" "$ourconf"` 1>&2
146         exit $code
147 elif [ -z "$tmpval1" ]; then
148         cleanslot=$tmpval2
149 else
150         cleanslot=$tmpval1
151 fi
152
153
154 # grab cleansleep info
155 tmpval1=`grep ^cleansleep $ourconf | awk -F\  '{print $2}'`
156 tmpval2=`grep ^cleansleep $ourconf | awk -F= '{print $2}'`
157 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
158         code=2
159         echo `_ 'Command Line ->'` $myname $@
160         echo `_ 'Exit(%s): cleansleep not specified in %s' "$code" "$ourconf"` 1>&2
161         exit $code
162 elif [ -z "$tmpval1" ]; then
163         cleansleep=$tmpval2
164 else
165         cleansleep=$tmpval1
166 fi
167
168
169 # grab cleanme info
170 tmpval1=`grep ^cleanme $ourconf | awk -F\  '{print $2}'`
171 tmpval2=`grep ^cleanme $ourconf | awk -F= '{print $2}'`
172 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
173         code=2
174         echo `_ 'Command Line ->'` $myname $@
175         echo `_ 'Exit(%s): cleanme not specified in %s' "$code" "$ourconf"` 1>&2
176         exit $code
177 elif [ -z "$tmpval1" ]; then
178         cleanme=$tmpval2
179 else
180         cleanme=$tmpval1
181 fi
182
183
184 # grab cleanfile info
185 tmpval1=`grep ^cleanfile $ourconf | awk -F\  '{print $2}'`
186 tmpval2=`grep ^cleanfile $ourconf | awk -F= '{print $2}'`
187 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
188         code=2
189         echo `_ 'Command Line ->'` $myname $@
190         echo `_ 'Exit(%s): cleanfile not specified in %s' "$code" "$ourconf"` 1>&2
191         exit $code
192 elif [ -z "$tmpval1" ]; then
193         cleanfile=$tmpval2
194 else
195         cleanfile=$tmpval1
196 fi
197
198
199 # grab lastfile info
200 tmpval1=`grep ^lastfile $ourconf | awk -F\  '{print $2}'`
201 tmpval2=`grep ^lastfile $ourconf | awk -F= '{print $2}'`
202 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
203         code=2
204         echo `_ 'Command Line ->'` $myname $@
205         echo `_ 'Exit(%s): lastfile not specified in %s' "$code" "$ourconf"` 1>&2
206         exit $code
207 elif [ -z "$tmpval1" ]; then
208         lastfile=$tmpval2
209 else
210         lastfile=$tmpval1
211 fi
212
213
214 # grab currentslot info
215 tmpval1=`grep ^currentslot $ourconf | awk -F\  '{print $2}'`
216 tmpval2=`grep ^currentslot $ourconf | awk -F= '{print $2}'`
217 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
218         code=2
219         echo `_ 'Command Line ->'` $myname $@
220         echo `_ 'Exit(%s): currentslot not specified in %s' "$code" "$ourconf"` 1>&2
221         exit $code
222 elif [ -z "$tmpval1" ]; then
223         currentslot=$tmpval2
224 else
225         currentslot=$tmpval1
226 fi
227
228
229 # grab logfile info
230 tmpval1=`grep ^logfile $ourconf | awk -F\  '{print $2}'`
231 tmpval2=`grep ^logfile $ourconf | awk -F= '{print $2}'`
232 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
233         code=2
234         echo `_ 'Command Line ->'` $myname $@
235         echo `_ 'Exit(%s): logfile not specified in %s' "$code" "$ourconf"` 1>&2
236         exit $code
237 elif [ -z "$tmpval1" ]; then
238         logfile=$tmpval2
239 else
240         logfile=$tmpval1
241 fi
242
243 [ ! -w $logfile ] && logfile=/dev/null
244
245
246 # grab slot0source info
247 tmpval1=`grep ^slot0source $ourconf | awk -F\  '{print $2}'`
248 tmpval2=`grep ^slot0source $ourconf | awk -F= '{print $2}'`
249 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
250         code=2
251         echo `_ 'Command Line ->'` $myname $@
252         echo `_ 'Exit(%s): slot0source not specified in %s' "$code" "$ourconf"` 1>&2
253         exit $code
254 elif [ -z "$tmpval1" ]; then
255         slot0source=$tmpval2
256 else
257         slot0source=$tmpval1
258 fi
259
260
261
262 #
263 # Verify currentslot contains a value
264 #
265 if [ ! -f $currentslot ] || [ `cat $currentslot` -lt $firstslot ];then
266    readstatus
267    echo $used > $currentslot
268 fi
269
270 current=`cat $currentslot`
271
272
273 # Start logging to $logfile
274 echo >> $logfile
275 echo >> $logfile
276 echo "==== `date` ====" >> $logfile
277 echo `_ 'Command Line ->'` $myname $@ >> $logfile
278
279
280 #
281 # is Use Cleaning activated?
282 #
283 if [ $use_cleaning -eq 1 ]; then
284    curday=`date +%j`
285    curyear=`date +%Y`
286
287    [ ! -f $cleanfile ] && echo 0 > $cleanfile
288    [ ! -f $lastfile ] && echo $curday,$curyear > $lastfile
289
290
291 #
292 # Check to see when tape drive was last cleaned
293 # output warning message if it's been too long
294 # Currently, if it's been more than 45days, then
295 # an error message is displayed everytime the
296 # script is called, until the clean parameter
297 # is run
298 #
299    cleaned=`cat $cleanfile`
300    lastcleaned=`cut -d, -f1 $lastfile`
301    yearcleaned=`cut -d, -f2 $lastfile`
302
303   if [ `expr $curday - $lastcleaned`  -lt 0 ];then
304      diffday=`expr $curday - $lastcleaned + 365`
305      diffyear=`expr $curyear - $yearcleaned - 1`
306   else
307      diffday=`expr $curday - $lastcleaned`
308      diffyear=`expr $curyear - $yearcleaned`
309   fi
310
311   if [ $diffday -gt $cleanme ] || [ $diffyear -ge 1 ];then
312      if [ $diffyear -ge 1 ];then
313           echo `_ "Warning, it's been %s year(s) & %s day(s) since you last cleaned the tape drive!" "$diffyear" "$diffday"`
314      else
315           echo `_ "Warning, it's been %s day(s) since you last cleaned the tape drive!" "$diffday"`
316      fi
317   fi
318
319 fi
320
321
322 #
323 # Read if there is a tape in the tape drive
324 # If so, determine what slot is the tape from
325 #
326 readstatus() {
327   echo `_ "querying tape drive....."` >> $logfile
328   used=`expr \`$MCUTIL -e drive | tr = \] | cut -d\] -f2\` - $slot0source`
329   echo `_ " Done"` >> $logfile
330
331   # Give changer a chance to reset itself
332   sleep 3
333 }
334
335
336 #
337 # If tape is in the drive, eject it
338 #
339 eject() {
340   echo `_ "tape drive eject was called"` >> $logfile
341
342   readstatus 
343
344   if [ $used -ge $firstslot ];then
345     $MCUTIL -m drive slot:$used
346     code=$?
347   else
348     code=1
349   fi
350
351   if [ $code -eq 0 ];then
352     answer=`_ 'Cartridge %s successfully ejected from %s' "$used" "$tape"`
353     echo `_ "Exit(%s): %s" "$code" "$answer"` >> $logfile
354     echo $current $answer       #For amtape output  
355     return $code
356   elif [ $code -eq 1 ];then
357     answer=`_ "No Cartridge in Tape Drive"`
358     echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
359     echo $current $answer       #For amtape output
360     exit $code
361   else
362     answer=`_ 'Tape abnormally failed'`
363     echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
364     echo $current $answer       #For amtape output
365     exit $code
366   fi
367 }
368
369
370 #
371 # reset tape drive to a current state.
372 # This involves ejecting the current tape (if occupied)
373 # and inserting the tape in $firstslot
374 #
375 reset() {
376   echo `_ 'tape drive reset was called'` >> $logfile
377
378   readstatus
379
380   if [ $used -ge $firstslot ];then
381      eject
382   fi
383
384   res=`$MCUTIL -m slot:$firstslot drive`
385   code=$?
386
387
388   if [ $code -eq 0 ];then
389     echo $firstslot > $currentslot
390     answer=`_ '%s - Tape drive was successfully reset' "$firstslot"`
391   elif [ $code -eq 1 ];then
392     answer=`_ '%s - Tape drive reset failed\nCommand -> %s' "$firstslot" "$res"`
393   else
394     code=2
395     answer=`_ '%s - Tape abnormally failed -> %s' "$firstslot" "$res"`
396   fi
397
398   echo `_ 'Exit(%s): slot %s' "$code" "$answer"` >> $logfile
399   echo $firstslot       #For amtape output 
400   exit $code
401 }
402
403
404
405
406 #
407 # Load a specific cartridge into the changer
408 #
409 loadslot() {
410   echo `_ "loadslot was called"` >> $logfile
411
412   readstatus
413
414   whichslot=$1
415
416   case $whichslot in
417     current)
418         if [ $current -ge $firstslot ];then
419            load=$current
420         else
421            load=$used
422         fi
423
424         movetape
425         ;;
426     next|advance)
427           [ $used -lt $firstslot ] && used=$current
428
429           load=`expr $used + 1`
430           [ $load -gt $lastslot ] && load=$firstslot
431
432           if [ $whichslot = advance ];then
433              echo $load > $currentslot
434              code=0
435              answer=`_ 'advancing to slot %s' "$load"`
436              echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
437              echo $load $code
438              exit $code
439           else
440              movetape
441           fi
442           ;;
443     prev)
444           [ $used -lt $firstslot ] && used=$current
445
446           load=`expr $used - 1`
447           [ $load -lt $firstslot ] && load=$lastslot
448           movetape
449           ;;
450     first)
451           load=$firstslot
452           movetape
453           ;;
454     last)
455           load=$lastslot
456           movetape
457           ;;
458     [$firstslot-$lastslot])
459              load=$1
460              movetape
461           ;;
462     clean)
463           if [ use_cleaning -eq 1 ];then
464              current=$cleanslot
465              eject
466              $MCUTIL slot:$cleanslot drive
467              sleep $cleansleep
468              echo "$curday,$curyear" > $lastfile
469              echo `expr $cleaned + 1` > $cleanfile
470              reset
471           else
472              code=1
473              answer=`_ "Cleaning not enabled in config"`
474              echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
475              echo $cleanslot $answer
476              exit $code
477           fi
478           ;;
479     *)
480        code=1
481        answer=`_ '"%s" invalid menu option' "$whichslot"`
482        echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
483        echo "$answer"
484        exit $code
485        ;;
486     esac
487 }
488
489
490 #
491 # sub-function that slot calls to actually eject the tape
492 # & load in the correct slot cartridge
493 #
494 movetape() {
495
496     # If the requested slot is already loaded in the tape drive
497     if [ $load -eq $used ]; then
498         code=0
499         answer=`_ 'slot %s is already loaded' "$load"`
500         echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
501         echo $load $tape        # For amtape output
502         exit $code
503     elif [ $used -ge $firstslot ];then
504         current=$load
505         eject
506     else
507         echo $load $tape        # For amtape output
508     fi
509
510     echo `_ 'Loading slot %s into Tape drive' "$load"` >> $logfile
511     $MCUTIL -m  slot:$load drive
512     code=$?
513
514     if [ $code -eq 0 ];then
515         echo $load > $currentslot
516         answer=`_ 'Cartridge %s successfully loaded in Tape drive' "$load"`
517     else
518         answer=`_ 'Cartridge %s failed to load in Tape drive' "$load"`
519     fi
520     echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
521     exit $code
522 }
523
524
525 info() {
526   echo `_ 'tape drive info was called'` >> $logfile
527
528   readstatus
529
530   if [ $used -lt 0 ];then
531     used=0
532   fi
533
534   code=0
535   answer="$used $lastslot 1"
536   echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
537   echo "$answer"
538   exit $code
539 }
540
541
542   case $1 in
543     -slot)
544            shift
545            loadslot $*
546            ;;
547     -device)
548            echo $tape
549            ;;
550     -info)
551             shift
552             info
553             ;;
554     -reset)
555             shift
556             reset
557             ;;
558     -eject)
559             shift
560             eject
561             ;;
562     --help|-help)
563             echo `_ '-slot {current|next|previous|first|last|%s-%s|clean}' "$firstslot" "$lastslot"`
564             echo `_ '   current  - show contents of current slot'`
565             echo `_ '   next     - load tape from next slot'`
566             echo `_ '   previous - load tape from previous slot'`
567             echo `_ '   first    - load tape from first slot'`
568             echo `_ '   last     - load tape from last slot'`
569             echo `_ '   %s - %s  - load tape from slot <slot #>' "$firstslot" "$lastslot"`
570             echo `_ '   clean    - Clean the drive'`
571             echo `_ '-device   : Show current tape device'`
572             echo `_ '-reset    : Reset changer to known state'`
573             echo `_ '-eject    : Eject current tape from drive'`
574             echo `_ '-info     : Output {current slot | # of slots | can changer go backwards}'`
575             echo `_ '-help     : Display this help'`
576             ;;
577     *)
578        echo `_ "<usage> %s -{slot|device|reset|eject|help}" "$myname"`
579        ;;
580  esac