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