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