16b56f10ab00cefe7ad52f577eee589252c6ce25
[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 "\n\n==== `date` ====" >> $logfile
275 echo `_ 'Command Line ->'` $myname $@ >> $logfile
276
277
278 #
279 # is Use Cleaning activated?
280 #
281 if [ $use_cleaning -eq 1 ]; then
282    curday=`date +%j`
283    curyear=`date +%Y`
284
285    [ ! -f $cleanfile ] && echo 0 > $cleanfile
286    [ ! -f $lastfile ] && echo $curday,$curyear > $lastfile
287
288
289 #
290 # Check to see when tape drive was last cleaned
291 # output warning message if it's been too long
292 # Currently, if it's been more than 45days, then
293 # an error message is displayed everytime the
294 # script is called, until the clean parameter
295 # is run
296 #
297    cleaned=`cat $cleanfile`
298    lastcleaned=`cut -d, -f1 $lastfile`
299    yearcleaned=`cut -d, -f2 $lastfile`
300
301   if [ `expr $curday - $lastcleaned`  -lt 0 ];then
302      diffday=`expr $curday - $lastcleaned + 365`
303      diffyear=`expr $curyear - $yearcleaned - 1`
304   else
305      diffday=`expr $curday - $lastcleaned`
306      diffyear=`expr $curyear - $yearcleaned`
307   fi
308
309   if [ $diffday -gt $cleanme ] || [ $diffyear -ge 1 ];then
310      if [ $diffyear -ge 1 ];then
311           echo `_ "Warning, it's been %s year(s) & %s day(s) since you last cleaned the tape drive!" "$diffyear" "$diffday"`
312      else
313           echo `_ "Warning, it's been %s day(s) since you last cleaned the tape drive!" "$diffday"`
314      fi
315   fi
316
317 fi
318
319
320 #
321 # Read if there is a tape in the tape drive
322 # If so, determine what slot is the tape from
323 #
324 readstatus() {
325   echo `_ "querying tape drive....."` >> $logfile
326   used=`expr \`$MCUTIL -e drive | tr = \] | cut -d\] -f2\` - $slot0source`
327   echo `_ " Done"` >> $logfile
328
329   # Give changer a chance to reset itself
330   sleep 3
331 }
332
333
334 #
335 # If tape is in the drive, eject it
336 #
337 eject() {
338   echo `_ "tape drive eject was called"` >> $logfile
339
340   readstatus 
341
342   if [ $used -ge $firstslot ];then
343     $MCUTIL -m drive slot:$used
344     code=$?
345   else
346     code=1
347   fi
348
349   if [ $code -eq 0 ];then
350     answer=`_ 'Cartridge %s successfully ejected from %s' "$used" "$tape"`
351     echo `_ "Exit(%s): %s" "$code" "$answer"` >> $logfile
352     echo $current $answer       #For amtape output  
353     return $code
354   elif [ $code -eq 1 ];then
355     answer=`_ "No Cartridge in Tape Drive"`
356     echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
357     echo $current $answer       #For amtape output
358     exit $code
359   else
360     answer=`_ 'Tape abnormally failed'`
361     echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
362     echo $current $answer       #For amtape output
363     exit $code
364   fi
365 }
366
367
368 #
369 # reset tape drive to a current state.
370 # This involves ejecting the current tape (if occupied)
371 # and inserting the tape in $firstslot
372 #
373 reset() {
374   echo `_ 'tape drive reset was called'` >> $logfile
375
376   readstatus
377
378   if [ $used -ge $firstslot ];then
379      eject
380   fi
381
382   res=`$MCUTIL -m slot:$firstslot drive`
383   code=$?
384
385
386   if [ $code -eq 0 ];then
387     echo $firstslot > $currentslot
388     answer=`_ '%s - Tape drive was successfully reset' "$firstslot"`
389   elif [ $code -eq 1 ];then
390     answer=`_ '%s - Tape drive reset failed\nCommand -> %s' "$firstslot" "$res"`
391   else
392     code=2
393     answer=`_ '%s - Tape abnormally failed -> %s' "$firstslot" "$res"`
394   fi
395
396   echo `_ 'Exit(%s): slot %s' "$code" "$answer"` >> $logfile
397   echo $firstslot       #For amtape output 
398   exit $code
399 }
400
401
402
403
404 #
405 # Load a specific cartridge into the changer
406 #
407 loadslot() {
408   echo `_ "loadslot was called"` >> $logfile
409
410   readstatus
411
412   whichslot=$1
413
414   case $whichslot in
415     current)
416         if [ $current -ge $firstslot ];then
417            load=$current
418         else
419            load=$used
420         fi
421
422         movetape
423         ;;
424     next|advance)
425           [ $used -lt $firstslot ] && used=$current
426
427           load=`expr $used + 1`
428           [ $load -gt $lastslot ] && load=$firstslot
429
430           if [ $whichslot = advance ];then
431              echo $load > $currentslot
432              code=0
433              answer=`_ 'advancing to slot %s' "$load"`
434              echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
435              echo $load $code
436              exit $code
437           else
438              movetape
439           fi
440           ;;
441     prev)
442           [ $used -lt $firstslot ] && used=$current
443
444           load=`expr $used - 1`
445           [ $load -lt $firstslot ] && load=$lastslot
446           movetape
447           ;;
448     first)
449           load=$firstslot
450           movetape
451           ;;
452     last)
453           load=$lastslot
454           movetape
455           ;;
456     [$firstslot-$lastslot])
457              load=$1
458              movetape
459           ;;
460     clean)
461           if [ use_cleaning -eq 1 ];then
462              current=$cleanslot
463              eject
464              $MCUTIL slot:$cleanslot drive
465              sleep $cleansleep
466              echo "$curday,$curyear" > $lastfile
467              echo `expr $cleaned + 1` > $cleanfile
468              reset
469           else
470              code=1
471              answer=`_ "Cleaning not enabled in config"`
472              echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
473              echo $cleanslot $answer
474              exit $code
475           fi
476           ;;
477     *)
478        code=1
479        answer=`_ '"%s" invalid menu option' "$whichslot"`
480        echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
481        echo "$answer"
482        exit $code
483        ;;
484     esac
485 }
486
487
488 #
489 # sub-function that slot calls to actually eject the tape
490 # & load in the correct slot cartridge
491 #
492 movetape() {
493
494     # If the requested slot is already loaded in the tape drive
495     if [ $load -eq $used ]; then
496         code=0
497         answer=`_ 'slot %s is already loaded' "$load"`
498         echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
499         echo $load $tape        # For amtape output
500         exit $code
501     elif [ $used -ge $firstslot ];then
502         current=$load
503         eject
504     else
505         echo $load $tape        # For amtape output
506     fi
507
508     echo `_ 'Loading slot %s into Tape drive' "$load"` >> $logfile
509     $MCUTIL -m  slot:$load drive
510     code=$?
511
512     if [ $code -eq 0 ];then
513         echo $load > $currentslot
514         answer=`_ 'Cartridge %s successfully loaded in Tape drive' "$load"`
515     else
516         answer=`_ 'Cartridge %s failed to load in Tape drive' "$load"`
517     fi
518     echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
519     exit $code
520 }
521
522
523 info() {
524   echo `_ 'tape drive info was called'` >> $logfile
525
526   readstatus
527
528   if [ $used -lt 0 ];then
529     used=0
530   fi
531
532   code=0
533   answer="$used $lastslot 1"
534   echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
535   echo "$answer"
536   exit $code
537 }
538
539
540   case $1 in
541     -slot)
542            shift
543            loadslot $*
544            ;;
545     -device)
546            echo $tape
547            ;;
548     -info)
549             shift
550             info
551             ;;
552     -reset)
553             shift
554             reset
555             ;;
556     -eject)
557             shift
558             eject
559             ;;
560     --help|-help)
561             echo `_ '-slot {current|next|previous|first|last|%s-%s|clean}' "$firstslot" "$lastslot"`
562             echo `_ '   current  - show contents of current slot'`
563             echo `_ '   next     - load tape from next slot'`
564             echo `_ '   previous - load tape from previous slot'`
565             echo `_ '   first    - load tape from first slot'`
566             echo `_ '   last     - load tape from last slot'`
567             echo `_ '   %s - %s  - load tape from slot <slot #>' "$firstslot" "$lastslot"`
568             echo `_ '   clean    - Clean the drive'`
569             echo `_ '-device   : Show current tape device'`
570             echo `_ '-reset    : Reset changer to known state'`
571             echo `_ '-eject    : Eject current tape from drive'`
572             echo `_ '-info     : Output {current slot | # of slots | can changer go backwards}'`
573             echo `_ '-help     : Display this help'`
574             ;;
575     *)
576        echo `_ "<usage> %s -{slot|device|reset|eject|help}" "$myname"`
577        ;;
578  esac