Imported Upstream version 2.5.1p3
[debian/amanda] / changer-src / chg-zd-mtx.sh.in
1 #!@SHELL@ 
2 #
3 # Exit Status:
4 # 0 Alles Ok
5 # 1 Illegal Request
6 # 2 Fatal Error
7 #
8 # Contributed by Eric DOUTRELEAU <Eric.Doutreleau@int-evry.fr>
9 # This is supposed to work with Zubkoff/Dandelion version of mtx
10 #
11 # Modified by Joe Rhett <jrhett@isite.net>
12 # to work with MTX 1.2.9 by Eric Lee Green http://mtx.sourceforge.net
13 #
14 # Modified by Jason Hollinden <jhollind@sammg.com> on 13-Feb-2001
15 # to work with MTX 1.2.10, >9 slots, has barcode support, and works with
16 # multiple configs at once.
17 # NOTE:  Only tested the 2 additions with an ADIC Scalar 100.
18
19 ################################################################################
20 # Here are the things you need to do and know to configure this script:
21 #
22 #   * Figure out what the robot device name is and what the tape drive
23 #     device name is.  They will be different!
24 #
25 #     You cannot send robot commands to a tape drive and vice versa.
26 #     Both should respond to "mtx -f /dev/... inquiry".  Hopefully,
27 #     that output will make it obvious which is which.
28 #
29 #     For instance, here is what mtx has to say about my current robot:
30 #
31 #       Product Type: Medium Changer
32 #       Vendor ID: 'ATL     '
33 #       Product ID: 'ACL2640 206     '
34 #       Revision: '2A5A'
35 #       Attached Changer: No
36 #
37 #     and here is what it says about a tape drive:
38 #
39 #       Product Type: Tape Drive
40 #       Vendor ID: 'Quantum '
41 #       Product ID: 'DLT4000         '
42 #       Revision: 'CD50'
43 #       Attached Changer: No
44 #
45 #     Note the "Product Type" value makes it clear which is which.
46 #
47 #     If it is not obvious, "mf -f /dev/... rewind" should be happy when
48 #     talking to a (loaded) tape drive but the changer should give some
49 #     kind of error.  Similarly, "mtx -f /dev/... status" should show good
50 #     results with the changer but fail with a tape drive device name.
51 #
52 #     Once you have this figured out, set "changerdev" in amanda.conf
53 #     to the changer device and "tapedev" to the tape device.
54 #
55 #   * Find out what the first and last storage slots are.  Running
56 #     "mtx -f /dev/... status" should give you something like this
57 #     (although the output will vary widely based on the version of mtx
58 #     and the specifics of your robot):
59 #
60 #         Storage Changer /dev/changer:1 Drives, 9 Slots ( 0 Import/Export )
61 #       Data Transfer Element 0:Empty
62 #             Storage Element 1:Full :VolumeTag=SR0001
63 #             Storage Element 2:Full :VolumeTag=SR0002
64 #             Storage Element 3:Full :VolumeTag=SR0003
65 #             Storage Element 4:Full :VolumeTag=SR0004
66 #             Storage Element 5:Full :VolumeTag=SR0005
67 #             Storage Element 6:Full :VolumeTag=SR0006
68 #             Storage Element 7:Full :VolumeTag=SR0007
69 #             Storage Element 8:Full :VolumeTag=SR0008
70 #             Storage Element 9:Full :VolumeTag=SR0009
71 #             Storage Element 10 IMPORT/EXPORT:Full :VolumeTag=SR0009
72 #
73 #     This says the first storage slot (element) is "1" and the last
74 #     is "9".  If you allocate the entire robot to Amanda, you do not need
75 #     to set the "firstslot" or "lastslot" configuration file variables --
76 #     the script will compute these values for you.
77 #
78 #     You do not have to allocate all of the slots for Amanda use,
79 #     but whatever slots you use must be contiguous (i.e. 4 through 9
80 #     in the above would be OK but 1, 2, 5, 6, 9 would not).  The one
81 #     exception to this is that if one of the slots contains a cleaning
82 #     cartridge, it may be in any slot (Amanda will just skip over it if
83 #     it is between firstslot and lastslot).
84 #
85 #   * Speaking of cleaning cartridges, if you have a storage slot dedicated
86 #     to one, figure out what slot it is in.  That slot number will go in
87 #     the "cleanslot" variable.
88 #
89 #     Also, decide if you want the changer script to automatically run
90 #     the cleaning tape through the drive after every so many mounts,
91 #     and how many mounts you want to do between cleanings.  If you
92 #     want the script to do this, set the "autoclean" variable to 1 and
93 #     the "autocleancount" to the number of mounts between cleanings.
94 #     If you do not want to do automatic cleanings (including not having
95 #     a cleaning cartridge in the robot), set "autoclean" to 0.
96 #
97 #     Note that only a count of mounts is used to determine when it is
98 #     time to clean.  The script does not try to detect if the drive is
99 #     requesting cleaning, or how much the drive was used on a given
100 #     mount.
101 #
102 #   * If you tell Amanda about a cleaning cartridge, whether for automatic
103 #     operation or manual (amtape <config> clean), you must also tell
104 #     the script how long it takes to run the cleaning cycle.  It is
105 #     impossible for the script to determine when the cleaning operation
106 #     is done, so the "cleancycle" variable is the number of seconds
107 #     the longest cleaning operation takes (you'll just have to figure
108 #     this out by watching it a few times, or maybe finding it in a tape
109 #     drive hardware manual).  The script will sleep for this length of
110 #     time whenever the cleaning tape is referenced.  The default is 120
111 #     seconds (two minutes).
112 #
113 #   * Figure out the drive slot number.  By default, it is set to 0.
114 #     In the example above, the tape drive ("Data Transfer Element")
115 #     is in slot 0. If your drive slot is not 0, you
116 #     need to set the drive slot number with the "driveslot" variable.
117 #
118 #   * Figure out whether your robot has a barcode reader and whether
119 #     your version of mtx supports it.  If you see "VolumeTag" entries
120 #     in the "mtx -f /dev/xxx status" output you did above, you have
121 #     a reader and mtx can work with it, so you may set the "havereader"
122 #     variable to 1.  The default is 0 (do not use a reader).
123 #
124 #   * Pick any tape to load and then determine if the robot can put it
125 #     away directly or whether an "offline" must be done first.
126 #
127 #     With the tape still mounted and ready, try to put the tape away
128 #     with "mtx".  If you get some kind of error, which is the most
129 #     common response, try "mt -f /dev/... offline", wait for the drive
130 #     to unload and make sure the robot takes no action on its own to
131 #     store the tape.  Assuming it does not, try the "mtx" command again
132 #     to store the tape.
133 #
134 #     If you had to issue the "mt -f /dev/... offline" before you could
135 #     use "mtx" to store the tape, set the "offline_before_unload"
136 #     variable to 1.  If "mtx" unloaded the drive and put the tape away
137 #     all by itself, set it to 0.
138 #
139 #   * Some drives and robots require a small delay between unloading the
140 #     tape and instructing the robot to move it back to storage.
141 #     For instance, if you try to grab the tape too soon on an ATL robot
142 #     with DLT tape drives, it will rip the leader out of the drive and
143 #     require sincerely painful hardware maintenance.
144 #
145 #     If you need a little delay, set the "unloadpause" variable to
146 #     the number of seconds to wait before trying to take a tape from
147 #     a drive back to storage.  The default is 0.
148 #
149 #   * Some drives also require a short pause after loading, or the drive
150 #     will return an I/O error during a test to see if it's online (which
151 #     this script uses "mt rewind" to test).  My drives don't recover from
152 #     this, and must be reloaded before they will come online after failing
153 #     such a test.  For this reason there is an "initial_poll_delay"
154 #     variable which will pause for a certain number of seconds before
155 #     looping through the online test for the first time.  The default is 0.
156 ####
157
158 ####
159 # Now you are ready to set up the variables in the changer configuration
160 # file.
161 #
162 # All variables are in "changerfile".conf where "changerfile" is set
163 # in amanda.conf.  For example, if amanda.conf has:
164 #
165 #       changerfile="/etc/amanda/Dailyset1/CHANGER"
166 #    or changerfile="/etc/amanda/Dailyset1/CHANGER.conf"
167 #
168 # the variables must be in "/etc/amanda/Dailyset1/CHANGER.conf".
169 # The ".conf" is appended only if it's not there".
170 #
171 # If "changerfile" is a relative path, it is relative to the directory
172 # that contains amanda.conf.  That also happens to be the directory Amanda
173 # makes current before running this script.
174 #
175 # Here is a commented out example file with all the variables and showing
176 # their default value (if any):
177 ####
178 # firstslot=?               #### First storage slot (element) -- required
179 # lastslot=?                #### Last storage slot (element) -- required
180 # cleanslot=-1              #### Slot with cleaner tape -- default is "-1"
181 #                           #### Set negative to indicate no cleaner available
182 # driveslot=0               #### Drive slot number.  Defaults to 0
183 #                           #### Use the 'Data Transfer Element' you want
184 #
185 #   # Do you want to clean the drive after a certain number of accesses?
186 #   # NOTE - This is unreliable, since 'accesses' aren't 'uses', and we
187 #   #        have no reliable way to count this.  A single amcheck could
188 #   #        generate as many accesses as slots you have, plus 1.
189 #   # ALSO NOTE - many modern tape loaders handle this automatically.
190 #
191 # autoclean=0               #### Set to '1' or greater to enable
192 #
193 # autocleancount=99         #### Number of access before a clean.
194 #
195 # havereader=0              #### If you have a barcode reader, set to 1.
196 #
197 # offline_before_unload=0   #### Does your robot require an
198 #                           #### 'mt offline' before mtx unload?
199 #
200 # poll_drive_ready=NN       #### Time (seconds) between tests to see if
201 #                           #### the tape drive has gone ready (default: 3).
202 #
203 # max_drive_wait=NN         #### Maximum time (seconds) to wait for the
204 #                           #### tape drive to become ready (default: 120).
205 #
206 # initial_poll_delay=NN     #### initial delay after load before polling for
207 #                           #### readiness
208 ####
209
210 ####
211 # Now it is time to test the setup.  Do all of the following in the
212 # directory that contains the amanda.conf file, and do all of it as
213 # the Amanda user.
214 #
215 #   * Run this:
216 #
217 #       .../chg-zd-mtx -info
218 #       echo $?             #### (or "echo $status" if you use csh/tcsh)
219 #
220 #     You should get a single line from the script like this (the actual
221 #     numbers will vary):
222 #
223 #       5 9 1 1
224 #
225 #     The first number (5) is the "current" slot.  This may or may not be
226 #     the slot actually loaded at the moment (if any).  It is the slot
227 #     Amanda will try to use next.
228 #
229 #     The second number (9) is the number of slots.
230 #
231 #     The third number will always be "1" and indicates the changer is
232 #     capable of going backward.
233 #
234 #     The fourth number is optional.  If you set $havereader to 1, it
235 #     will be "1", otherwise it will not be present.
236 #
237 #     The exit code ($? or $status) should be zero.
238 #
239 #   * Run this:
240 #
241 #       .../chg-zd-mtx -reset
242 #       echo $?
243 #
244 #     The script should output a line like this:
245 #
246 #       1 /dev/rmt/0mn
247 #
248 #     The number at the first should match $firstslot.  The device name
249 #     after that should be your tape device.
250 #
251 #     The exit code ($? or $status) should be zero.
252 #
253 #   * Run this:
254 #
255 #       .../chg-zd-mtx -slot next
256 #       echo $?
257 #
258 #     The script should output a line like this:
259 #
260 #       2 /dev/rmt/0mn
261 #
262 #     The number at the first should be one higher than $firstslot.
263 #     The device name after that should be your tape device.
264 #
265 #     The exit code ($? or $status) should be zero.
266 #
267 #   * Run this:
268 #
269 #       .../chg-zd-mtx -slot current
270 #       echo $?
271 #
272 #     Assuming the tape is still loaded from the previous test, the
273 #     robot should not move and the script should report the same thing
274 #     the previous command did.
275 #
276 #   * If you continue to run "-slot next" commands, the robot should load
277 #     each tape in turn then wrap back around to the first when it
278 #     reaches $lasttape.  If $cleanslot is within the $firstslot to
279 #     $lastslot range, the script will skip over that entry.
280 #
281 #   * Finally, try some of the amtape commands and make sure they work:
282 #
283 #       amtape <config> reset
284 #       amtape <config> slot next
285 #       amtape <config> slot current
286 #
287 #   * If you set $havereader non-zero, now would be a good time to create
288 #     the initial barcode database:
289 #
290 #       amtape <config> update
291 ####
292
293 ################################################################################
294 # To debug this script, first look in @AMANDA_DBGDIR@.  The script
295 # uses one of two log files there, depending on what version of Amanda
296 # is calling it.  It may be chg-zd-mtx.YYYYMMDD*.debug, or it may be
297 # changer.debug.driveN where 'N' is the drive number.
298 #
299 # If the log file does not help, try running the script, **as the Amanda
300 # user**, in the amanda.conf directory with whatever set of args the log
301 # said were used when you had a problem.  If nothing else useful shows up
302 # in the output, try running the script with the DEBUG environment variable
303 # set non-null, e.g.:
304 #
305 #       env DEBUG=yes .../chg-zd-mtx ...
306 ################################################################################
307
308 ################################################################################
309 # You may need to customize these things
310 ################################################################################
311
312 MT=@MT@
313 MTF=@MT_FILE_FLAG@
314 MTX=@MTX@
315
316 ################################################################################
317 # No user-level customization should be required beyond this point.
318 ################################################################################
319
320 test -n "$DEBUG" && set -x
321 TMPDIR="@AMANDA_TMPDIR@"
322 DBGDIR="@AMANDA_DBGDIR@"
323
324 argv0=$0
325 myname=`expr "$argv0" : '.*/\(.*\)'`
326
327 config=`pwd 2>/dev/null`
328 config=`expr "$config" : '.*/\(.*\)'`
329
330 ###
331 # Functions to write a new log file entry and append more log information.
332 ###
333
334 ds=`date '+%H:%M:%S' 2>/dev/null`
335 if [ $? -eq 0  -a  -n "$ds" ]; then
336         logprefix=`echo "$ds" | sed 's/./ /g'`
337 else
338         logprefix=""
339 fi
340
341 LogAppend() {
342         if [ -z "$logprefix" ]; then
343                 echo "$@" >> $DBGFILE
344         else
345                 echo "$logprefix" "$@" >> $DBGFILE
346         fi
347 }
348
349 Log() {
350         if [ -z "$logprefix" ]; then
351                 echo "===" "`date`" "===" >> $DBGFILE
352                 echo "$@" >> $DBGFILE
353         else
354                 ds=`date '+%H:%M:%S' 2>/dev/null`
355                 echo "$ds" "$@" >> $DBGFILE
356         fi
357 }
358
359 ###
360 # Common exit function.
361 #
362 #   $1 = exit code
363 #   $2 = slot result
364 #   $3 = additional information (error message, tape devive, etc)
365 ###
366
367 internal_call=0
368 Exit() {
369         if [ $internal_call -gt 0 ]; then
370                 call_type=Return
371         else
372                 call_type=Exit
373         fi
374         code=$1
375         shift
376         exit_slot=$1
377         shift
378         exit_answer="$@"
379         Log $call_type "($code)" "->" "$exit_slot" "$@"
380         echo "$exit_slot" "$@"
381         if [ $call_type = Return ]; then
382                 return $code
383         fi
384         amgetconf$SUF dbclose.$argv0:$DBGFILE > /dev/null 2>&1
385         exit $code
386 }
387
388 ###
389 # Function to run another command and log it.
390 ###
391
392 Run() {
393         Log Running: "$@"
394         rm -f $stdout $stderr
395         "$@" > $stdout 2> $stderr
396         exitcode=$?
397         Log Exit code: $exitcode
398         if [ -s $stdout ]
399         then
400                 LogAppend Stdout:
401                 cat $stdout >> $DBGFILE
402         fi
403         if [ -s $stderr ]
404         then
405                 LogAppend Stderr:
406                 cat $stderr >> $DBGFILE
407         fi
408         cat $stdout
409         cat $stderr 1>&2
410         return $exitcode
411 }
412
413 ###
414 # Return success if the arg is numeric.
415 ###
416
417 IsNumeric() {
418         test -z "$1" && return 1
419         x="`expr "$1" : '\([-0-9][0-9]*\)' 2>/dev/null`"
420         return `expr X"$1" != X"$x"`
421 }
422
423 ###
424 # Run $MTX status unless the previous output is still valid.
425 ###
426
427 mtx_status_valid=0
428 get_mtx_status() {
429         test -n "$DEBUG" && set -x
430         if [ $mtx_status_valid -ne 0 ]; then
431                 return 0
432         fi
433         rm -f $mtx_status
434         Run $MTX status > $mtx_status 2>&1
435         status=$?
436         if [ $status -eq 0 ]; then
437                 mtx_status_valid=1
438         fi
439         return $status
440 }
441
442 ###
443 # Determine the slot currently loaded.  Set $loadedslot to the slot
444 # currently loaded, or "-1", and $loadedbarcode to the corresponding
445 # barcode (or nothing).
446 ###
447
448 get_loaded_info() {
449         test -n "$DEBUG" && set -x
450         get_mtx_status
451
452         set x `sed -n '
453 /^Data Transfer Element:Empty/                          {
454     s/.*/-1/p
455     q
456 }
457 /^Data Transfer Element '$driveslot':Empty/             {
458     s/.*/-1/p
459     q
460 }
461 /^Data Transfer Element:Full (Storage Element \([0-9][0-9]*\) Loaded):VolumeTag *= *\([^     ]*\)/               {
462     s/.*(Storage Element \([0-9][0-9]*\) Loaded):VolumeTag *= *\([^     ]*\)/\1 \2/p
463     q
464 }
465 /^Data Transfer Element '$driveslot':Full (Storage Element \([0-9][0-9]*\) Loaded):VolumeTag *= *\([^     ]*\)/  {
466     s/.*(Storage Element \([0-9][0-9]*\) Loaded):VolumeTag *= *\([^     ]*\)/\1 \2/p
467     q
468 }
469 /^Data Transfer Element '$driveslot':Full (Unknown Storage Element Loaded):VolumeTag *= *\([^     ]*\)/ {
470     s/.*:VolumeTag *= *\([^     ]*\)/-2 \1/p
471     q
472 }
473 /^Data Transfer Element:Full (Storage Element \([0-9][0-9]*\) Loaded)/                           {
474     s/.*(Storage Element \([0-9][0-9]*\) Loaded).*/\1/p
475     q
476 }
477 /^Data Transfer Element '$driveslot':Full (Storage Element \([0-9][0-9]*\) Loaded)/              {
478     s/.*Storage Element \([0-9][0-9]*\) Loaded.*/\1/p
479     q
480 }
481 /^Data Transfer Element '$driveslot':Full (Unknown Storage Element Loaded)/     {
482     s/.*/-2/p
483     q
484 }
485 ' < $mtx_status 2>&1`
486         shift                                   # get rid of the "x"
487         loadedslot=$1
488         loadedbarcode=$2
489         if [ -z "$loadedslot" ]; then
490                 Exit 2 "<none>" "could not determine current slot, are you sure your drive slot is $driveslot"
491                 return $?                       # in case we are internal
492         fi
493
494         #Use the current slot if it's empty and we don't know which slot is loaded'
495         if [ $loadedslot -eq -2 ]; then
496                 set x `sed -n '
497 {
498     /^.*Storage Element '$currentslot':Empty/ {
499         s/.*Storage Element \([0-9][0-9]*\):Empty/\1/p
500         q
501     }
502     /^.*Storage Element '$currentslot':Full/ {
503         s/.*Storage Element \([0-9][0-9]*\):Full/-2/p
504         q
505     }
506     /^.*Storage Element '$currentslot' IMPORT\/EXPORT:Empty/ {
507         s/.*Storage Element \([0-9][0-9]*\) IMPORT\/EXPORT:Empty/\1/p
508         q
509     }
510     /^.*Storage Element '$currentslot' IMPORT\/EXPORT:Full/ {
511         s/.*Storage Element \([0-9][0-9]*\) IMPORT\/EXPORT:Full/-2/p
512         q
513     }
514 }
515 ' < $mtx_status 2>& 1`
516                 shift                           # get rid of the "x"
517                 loadedslotx=$1
518                 if [ ! -z $loadedslotx ]; then
519                         loadedslot=$loadedslotx
520                 fi
521         fi
522
523         #Use the first empty slot if we don't know which slot is loaded'
524         if [ $loadedslot -eq -2 ]; then
525                 set x `sed -n '
526 {
527     /^.*Storage Element \([0-9][0-9]*\):Empty/ {
528         s/.*Storage Element \([0-9][0-9]*\):Empty/\1/p
529         q
530     }
531     /^.*Storage Element \([0-9][0-9]*\) IMPORT\/EXPORT:Empty/ {
532         s/.*Storage Element \([0-9][0-9]*\) IMPORT\/EXPORT:Empty/\1/p
533         q
534     }
535 }
536 ' < $mtx_status 2>& 1`
537                 shift                           # get rid of the "x"
538                 loadedslot=$1
539         fi
540
541         if IsNumeric "$loadedslot" ; then
542                 :
543         else
544                 Exit 2 \
545                      "<none>" \
546                      "currently loaded slot ($loadedslot) not numeric"
547                 return $?                       # in case we are internal
548         fi
549         Log       "STATUS   -> currently loaded slot = $loadedslot"
550         LogAppend "         -> currently loaded barcode = \"$loadedbarcode\""
551 }
552
553 ###
554 # Get a list of slots between $firstslot and $lastslot, if they are set.
555 # If they are not set, set them to the first and last slot seen on the
556 # assumption the entire robot is to be used (???).
557 ###
558
559 slot_list=
560 get_slot_list() {
561         test -n "$DEBUG" && set -x
562         if [ -n "$slot_list" ]; then
563                 return
564         fi
565         get_mtx_status
566         slot_list=`sed -n '
567 /^Data Transfer Element:Full (Storage Element \([0-9][0-9]*\) Loaded)/ {
568     s/.*(Storage Element \([0-9][0-9]*\) Loaded).*/\1/p
569 }
570 /^Data Transfer Element '$driveslot':Full (Storage Element \([0-9][0-9]*\) Loaded)/ {
571     s/.*Storage Element \([0-9][0-9]*\) Loaded.*/\1/p
572 }
573 /^Data Transfer Element '$driveslot':Full (Unknown Storage Element Loaded)/ {
574     : loop
575     n
576     /^.*Storage Element \([0-9][0-9]*\):Full/ {
577         s/.*Storage Element \([0-9][0-9]*\):Full.*/\1/p
578         b loop
579     }
580     /^.*Storage Element \([0-9][0-9]*\):Empty/ {
581         s/.*Storage Element \([0-9][0-9]*\):Empty/\1/p
582     }
583 }
584 /^.*Storage Element \([0-9][0-9]*\):Full/ {
585     s/.*Storage Element \([0-9][0-9]*\):Full.*/\1/p
586 }
587 /^.*Storage Element \([0-9][0-9]*\) IMPORT\/EXPORT:Full/ {
588     s/.*Storage Element \([0-9][0-9]*\) IMPORT\/EXPORT:Full.*/\1/p
589 }
590 ' < $mtx_status 2>&1 | grep -v "^${cleanslot}\$" | sort -n`
591         slot_list=`echo $slot_list`             # remove the newlines
592         if [ $firstslot -lt 0 -o $lastslot -lt 0 ]; then
593                 last=$lastslot
594                 for slot in $slot_list; do
595                         if [ $firstslot -lt 0 ]; then
596                                 Log "SLOTLIST -> firstslot set to $slot"
597                                 firstslot=$slot
598                         fi
599                         if [ $lastslot -lt 0 ]; then
600                                 last=$slot
601                         fi
602                 done
603                 if [ $lastslot -lt 0 -a $last -ge 0 ]; then
604                         Log "SLOTLIST -> lastslot set to $last"
605                         lastslot=$last
606                 fi
607                 if [ $firstslot -lt 0 ]; then
608                         Exit 2 \
609                              "<none>" \
610                              "cannot determine first slot"
611                         return $?               # in case we are internal
612                 elif [ $lastslot -lt 0 ]; then
613                         Exit 2 \
614                              "<none>" \
615                              "cannot determine last slot"
616                         return $?               # in case we are internal
617                 fi
618         fi
619         amanda_slot_list=
620         for slot in $slot_list; do
621                 if [ $slot -ge $firstslot -a $slot -le $lastslot ]; then
622                         amanda_slot_list="$amanda_slot_list $slot"
623                 fi
624         done
625         if [ -z "$amanda_slot_list" ]; then
626                 Exit 2 \
627                      "<none>" \
628                      "no slots available"
629                 return $?                       # in case we are internal
630         fi
631         slot_list="$amanda_slot_list"
632 }
633
634 # Paths
635 prefix=@prefix@
636 exec_prefix=@exec_prefix@
637 sbindir=@sbindir@
638 libexecdir=@libexecdir@
639
640 # try to hit all the possibilities here
641 PATH=$sbindir:$libexecdir:/usr/bin:/bin:/usr/sbin:/sbin:/usr/ucb:/usr/local/bin
642 export PATH
643
644 USE_VERSION_SUFFIXES="@USE_VERSION_SUFFIXES@"
645 if test "$USE_VERSION_SUFFIXES" = "yes"; then
646         SUF="-@VERSION@"
647 else
648         SUF=
649 fi
650
651 DBGFILE=`amgetconf$SUF dbopen.$argv0 2>/dev/null`
652 if [ -z "$DBGFILE" ]
653 then
654         DBGFILE=/dev/null                       # will try this again below
655 fi
656
657 changerfile=`amgetconf$SUF changerfile 2>/dev/null`
658 if [ -z "$changerfile" ]; then
659         Exit 2 \
660              "<none>" \
661              "changerfile must be specified in amanda.conf"
662 fi
663
664 tape=`amgetconf$SUF tapedev 2>/dev/null`
665 if [ -z "$tape" ]; then
666         Exit 2 \
667              "<none>" \
668              "tapedev may not be empty"
669 elif [ $tape = "/dev/null" -o `expr "$tape" : 'null:'` -eq 5 ]; then
670         Exit 2 \
671              "<none>" \
672              "tapedev ($tape) may not be the null device"
673 fi
674 TAPE=`amgetconf$SUF changerdev 2>/dev/null`
675 if [ -z "$TAPE" ]; then
676         Exit 2 \
677              "<none>" \
678              "changerdev may not be empty"
679 elif [ $TAPE = "/dev/null" ]; then
680         Exit 2 \
681              "<none>" \
682              "changerdev ($TAPE) may not be the null device"
683 fi
684 export TAPE                                     # for mtx command
685
686 CHANGER=$TAPE
687 export CHANGER                                  # for mtx command
688
689 #### Set up the various config files.
690
691 conf_match=`expr "$changerfile" : .\*\.conf\$`
692 if [ $conf_match -ge 6 ]; then
693         configfile=$changerfile
694         changerfile=`echo $changerfile | sed 's/.conf$//g'`
695 else
696         configfile=$changerfile.conf
697 fi
698
699 cleanfile=$changerfile-clean
700 accessfile=$changerfile-access
701 slotfile=$changerfile-slot
702 labelfile=$changerfile-barcodes
703 [ ! -s $cleanfile ] && echo 0 > $cleanfile
704 [ ! -s $accessfile ] && echo 0 > $accessfile
705 [ ! -s $slotfile ] && echo -1 > $slotfile
706 [ ! -f $labelfile ] && > $labelfile
707 cleancount=`cat $cleanfile`
708 accesscount=`cat $accessfile`
709
710 test -z "$MT" && Exit 2 "<none>" "No mt command defined"
711 test ! -f "$MT" && Exit 2 "<none>" "mt command ($MT) doesn't exist"
712 test -z "$MTX" && Exit 2 "<none>" "No mtx command defined"
713 test ! -f "$MTX" && Exit 2 "<none>" "mtx command ($MTX) doesn't exist"
714
715 #### Dig out of the config file what is needed
716
717 varlist=
718 varlist="$varlist firstslot"
719 varlist="$varlist lastslot"
720 varlist="$varlist cleanslot"
721 varlist="$varlist cleancycle"
722 varlist="$varlist OFFLINE_BEFORE_UNLOAD"        # old name
723 varlist="$varlist offline_before_unload"
724 varlist="$varlist unloadpause"
725 varlist="$varlist AUTOCLEAN"                    # old name
726 varlist="$varlist autoclean"
727 varlist="$varlist autocleancount"
728 varlist="$varlist havereader"
729 varlist="$varlist driveslot"
730 varlist="$varlist poll_drive_ready"
731 varlist="$varlist initial_poll_delay"
732 varlist="$varlist max_drive_wait"
733
734 for var in $varlist
735 do
736         val="`cat $configfile 2>/dev/null | sed -n '
737 # Ignore comment lines (anything starting with a #).
738 /^[     ]*#/d
739 # Find the first var=val line in the file, print the value and quit.
740 /^[     ]*'$var'[       ]*=[    ]*\([^  ][^     ]*\).*/ {
741         s/^[    ]*'$var'[       ]*=[    ]*\([^  ][^     ]*\).*/\1/p
742         q
743 }
744 '`"
745         eval $var=\"$val\"
746 done
747
748 # Deal with driveslot first so we can get DBGFILE set if we are still
749 # using the old amgetconf.
750
751 if [ -z "$driveslot" ]; then
752         driveslot=0;
753 fi
754
755 # Get DBGFILE set if it is not already.
756
757 if [ $DBGFILE = /dev/null ]; then
758         if [ -d "$DBGDIR" ]; then
759                 DBGFILE=$DBGDIR/changer.debug.drive$driveslot
760         else
761                 DBGFILE=/dev/null
762         fi
763         Log === Start "`date`" ===
764 fi
765 if [ -z "$driveslot" ]; then
766         Exit 2 \
767              "<none>" \
768              "cannot determine drive slot from $tape"
769 fi
770
771 stdout=$TMPDIR/$myname.1.$$
772 stderr=$TMPDIR/$myname.2.$$
773 mtx_status=$TMPDIR/$myname.status.$$
774 trap "rm -f $stdout $stderr $mtx_status" 0      # exit cleanup
775
776 Log "Using config file $configfile"
777
778 # Log the argument list.
779
780 Log "Arg info:"
781 LogAppend "\$# = $#"
782 i=0
783 LogAppend "\$$i = \"$argv0\""
784 for arg in "$@"; do
785         i=`expr $i + 1`
786         LogAppend "\$$i = \"$arg\""
787 done
788
789 # Set the default config values for those not in the file.  Log the
790 # results and make sure each is valid (numeric).
791
792 firstslot=${firstslot:-'-1'}                            # default: mtx status
793 lastslot=${lastslot:-'-1'}                              # default: mtx status
794 cleanslot=${cleanslot:-'-1'}                            # default: -1
795 cleancycle=${cleancycle:-'120'}                         # default: two minutes
796 if [ -z "$offline_before_unload" -a -n "$OFFLINE_BEFORE_UNLOAD" ]; then
797         offline_before_unload=$OFFLINE_BEFORE_UNLOAD    # (old name)
798 fi
799 offline_before_unload=${offline_before_unload:-'0'}     # default: 0
800 unloadpause=${unloadpause:-'0'}                         # default: 0
801 if [ -z "$autoclean" -a -n "$AUTOCLEAN" ]; then
802         autoclean=$AUTOCLEAN                            # (old name)
803 fi
804 autoclean=${autoclean:-'0'}                             # default: 0
805 autocleancount=${autocleancount:-'99'}                  # default: 99
806 havereader=${havereader:-'0'}                           # default: 0
807 poll_drive_ready=${poll_drive_ready:-'3'}               # default: three seconds
808 initial_poll_delay=${initial_poll_delay:-'0'}           # default: zero zeconds
809 max_drive_wait=${max_drive_wait:-'120'}                 # default: two minutes
810
811 get_slot_list
812
813 Log "Config info:"
814 for var in $varlist; do
815         if [ $var = "OFFLINE_BEFORE_UNLOAD" ]; then
816                 continue                        # old name
817         elif [ $var = "AUTOCLEAN" ]; then
818                 continue                        # old name
819         fi
820         eval val=\"'$'$var\"
821         if [ -z "$val" ]; then
822                 Exit 2 \
823                      "<none>" \
824                      "$var missing in $configfile"
825         fi
826         if IsNumeric "$val" ; then
827                 :
828         else
829                 Exit 2 \
830                      "<none>" \
831                      "$var ($val) not numeric in $configfile"
832         fi
833         LogAppend $var = \"$val\"
834 done
835
836 # Run the rest of the config file sanity checks.
837
838 if [ $firstslot -gt $lastslot ]; then
839         Exit 2 \
840              "<none>" \
841              "firstslot ($firstslot) greater than" \
842              "lastslot ($lastslot) in $configfile"
843 fi
844 if [ $autoclean -ne 0 -a $cleanslot -lt 0 ]; then
845         Exit 2 \
846              "<none>" \
847              "autoclean set but cleanslot not valid ($cleanslot)"
848 fi
849
850 # Set up the current slot
851
852 currentslot=`cat $slotfile`
853 if IsNumeric "$currentslot" ; then
854         if [ $currentslot -lt $firstslot ]; then
855                 Log "SETUP    -> current slot $currentslot" \
856                                  "less than $firstslot ..." \
857                                  "resetting to $firstslot"
858                 currentslot=$firstslot
859         elif [ $currentslot -gt $lastslot ]; then
860                 Log "SETUP    -> current slot $currentslot" \
861                                  "greater than $lastslot ..." \
862                                  "resetting to $lastslot"
863                 currentslot=$lastslot
864         fi
865 else
866         Log "SETUP    -> contents of $slotfile ($currentslot) invalid," \
867                          "setting current slot to first slot ($firstslot)"
868         currentslot=$firstslot
869 fi
870
871 found_current=0
872 first_slot_in_list=-1
873 next_slot_after_current=-1
874 for slot in $slot_list; do
875         if [ $first_slot_in_list -lt 0 ]; then
876                 first_slot_in_list=$slot        # in case $firstslot is missing
877         fi
878         if [ $slot -eq $currentslot ]; then
879                 found_current=1
880                 break
881         elif [ $slot -gt $currentslot ]; then
882                 next_slot_after_current=$slot   # $currentslot is missing
883                 break
884         fi
885 done
886 if [ $found_current -eq 0 ]; then
887         if [ $next_slot_after_current -lt 0 ]; then
888                 new_currentslot=$first_slot_in_list
889         else
890                 new_currentslot=$next_slot_after_current
891         fi
892         Log "WARNING  -> current slot $currentslot not available," \
893                          "setting current slot to next slot ($new_currentslot)"
894         currentslot=$new_currentslot
895 fi
896
897 # More routines.
898
899 ###
900 # Eject the current tape and put it away.
901 ###
902
903 eject() {
904         test -n "$DEBUG" && set -x
905         Log "EJECT    -> ejecting tape from $tape"
906         get_loaded_info 
907         if [ $loadedslot -gt 0 ]; then
908                 Log "EJECT    -> moving tape from drive $driveslot" \
909                                  "to storage slot $loadedslot"
910                 if [ $offline_before_unload -ne 0 ]; then
911                         Run $MT $MTF $tape offline > /dev/null 2>&1
912                 fi
913                 sleep $unloadpause
914                 result=`Run $MTX unload $loadedslot $driveslot 2>&1`
915                 status=$?
916                 Log "         -> status $status, result \"$result\""
917                 mtx_status_valid=0
918                 if [ $status -ne 0 ]; then
919                         answer="$result"
920                         code=2
921                 else
922                         answer="$tape"
923                         code=0
924                 fi
925         else
926                 answer="Drive was not loaded"
927                 code=1
928         fi
929         Exit $code "$loadedslot" "$answer"
930         return $?                               # in case we are internal
931 }
932
933 ###
934 # Reset the robot back to the first slot.
935 ###
936
937 reset() {
938         test -n "$DEBUG" && set -x
939         Log "RESET    -> loading tape from slot $firstslot" \
940                          "to drive $driveslot ($tape)"
941         # Call loadslot without doing it as an internal and let it finish
942         # things up.
943         loadslot $firstslot
944         # NOTREACHED
945         Exit 2 "<none>" "reset: should not get here"
946         return $?                               # in case we are internal
947 }
948
949 ###
950 # Unload the current tape (if necessary) and load a new one (unless
951 # "advance").  If no tape is loaded, get the value of "current" from
952 # $slotfile.
953 ###
954
955 loadslot() {
956         test -n "$DEBUG" && set -x
957         if [ $# -lt 1 ]; then
958                 Exit 2 "<none>" "Missing -slot argument"
959                 return $?                       # in case we are internal
960         fi
961         whichslot=$1
962         Log "LOADSLOT -> load drive $driveslot ($tape) from slot $whichslot"
963
964         numeric=`echo $whichslot | sed 's/[^0-9]//g'`
965         case $whichslot in
966         current|prev|next|advance)
967                 find_slot=$currentslot
968                 ;;
969         first)
970                 find_slot=$firstslot
971                 ;;
972         last)
973                 find_slot=$lastslot
974                 ;;
975         $numeric)
976                 find_slot=$numeric
977                 ;;
978         clean)
979                 find_slot=$cleanslot
980                 ;;
981         *)
982                 Exit 2 "<none>" "Illegal slot: \"$whichslot\""
983                 return $?                       # in case we are internal
984                 ;;
985         esac
986
987         # Find the requested slot in the slot list.  By loading the "set"
988         # command with multiple copies, we guarantee that if the slot is
989         # found, we can look both forward and backward without running
990         # off the end.  Putting $cleanslot at the end allows us to find
991         # that slot since it is not in $slot_list.
992         get_slot_list
993         set x $slot_list $slot_list $slot_list $cleanslot
994         shift                                   # get rid of the "x"
995         prev_slot=$1
996         shift
997         while [ $# -gt 0 ]; do
998                 if [ $1 -eq $find_slot ]; then
999                         break
1000                 fi
1001                 prev_slot=$1
1002                 shift
1003         done
1004         if [ $# -le 0 ]; then
1005                 Exit 2 \
1006                      "<none>" \
1007                      "Cannot find slot $find_slot" \
1008                      "in slot list ($slot_list)"
1009                 return $?                       # in case we are internal
1010         fi
1011
1012         # Determine the slot to load.
1013         case $whichslot in
1014         next|advance)
1015                 shift
1016                 loadslot=$1
1017                 ;;
1018         prev)
1019                 loadslot=$prev_slot
1020                 ;;
1021         *)
1022                 loadslot=$find_slot
1023         esac
1024
1025         # If the desired slot is already loaded, we are done.  Only update
1026         # current slot if this is not the cleaning slot.
1027         get_loaded_info
1028         if [ $loadslot = $loadedslot ]; then
1029                 if [ $loadslot -ne $cleanslot ]; then
1030                         rm -f $slotfile
1031                         echo $loadslot > $slotfile
1032                 fi
1033                 Exit 0 "$loadedslot" "$tape"
1034                 return $?                       # in case we are internal
1035         fi
1036         if [ $loadedslot -eq -2 ]; then
1037                 Exit 0 "$loadedslot" "$tape"
1038                 return $?                       # in case we are internal
1039         fi
1040
1041         # If we are loading the cleaning tape, bump the cleaning count
1042         # and reset the access count.  Otherwise, bump the access count
1043         # and see if it is time to do a cleaning.
1044         if [ $loadslot = $cleanslot ]; then
1045                 rm -f $cleanfile $accessfile
1046                 expr $cleancount + 1 > $cleanfile
1047                 echo 0 > $accessfile
1048         else
1049                 rm -f $accessfile
1050                 expr $accesscount + 1 > $accessfile
1051                 if [ $autoclean -ne 0 -a $accesscount -gt $autocleancount ]
1052                 then
1053                         internal_call=`expr $internal_call + 1`
1054                         loadslot clean > /dev/null 2>&1
1055                         status=$?
1056                         internal_call=`expr $internal_call - 1`
1057                         if [ $status -ne 0 ]; then
1058                                 Exit $status "$loadslot" "$exit_answer"
1059                                 return $?       # in case we are internal
1060                         fi
1061
1062                         # Slot $cleanslot might contain an ordinary tape
1063                         # rather than a cleaning tape.  A cleaning tape
1064                         # *MIGHT* auto-eject; an ordinary tape does not.
1065                         # We therefore have to read the status again to
1066                         # check what actually happened.
1067                         mtx_status_valid=0
1068                         get_loaded_info
1069                 fi
1070         fi
1071
1072         # Unload whatever tape is in the drive.
1073         internal_call=`expr $internal_call + 1`
1074         eject > /dev/null 2>&1
1075         status=$?
1076         internal_call=`expr $internal_call - 1`
1077         if [ $status -gt 1 ]; then
1078                 Exit $status "$exit_slot" "$exit_answer"
1079                 return $?                       # in case we are internal
1080         fi
1081
1082         # If we were doing an "advance", we are done.
1083         if [ $whichslot = advance ]; then
1084                 if [ $loadslot -ne $cleanslot ]; then
1085                         rm -f $slotfile
1086                         echo $loadslot > $slotfile
1087                 fi
1088                 Exit 0 "$loadslot" "/dev/null"
1089                 return $?                       # in case we are internal
1090         fi
1091
1092         # Load the tape, finally!
1093         Log "LOADSLOT -> loading tape from slot $loadslot" \
1094                          "to drive $driveslot ($tape)"
1095         result=`Run $MTX load $loadslot $driveslot 2>&1`
1096         status=$?
1097         Log "         -> status $status, result \"$result\""
1098         mtx_status_valid=0
1099         if [ $status -ne 0 ]; then
1100                 Exit 2 "$loadslot" "$result"
1101                 return $?                       # in case we are internal
1102         fi
1103
1104         ###
1105         # Cleaning tapes never go "ready", so instead we just sit here
1106         # for "long enough" (as determined empirically by the user),
1107         # then return success.
1108         ###
1109         if [ $loadslot -eq $cleanslot ]; then
1110                 Run sleep $cleancycle
1111                 Exit 0 "$loadslot" "$tape"
1112                 return $?                       # in case we are internal
1113         fi
1114
1115         ###
1116         # Wait for the drive to go online.
1117         ###
1118         waittime=0
1119         ready=0
1120         sleep $initial_poll_delay
1121         while [ $waittime -lt $max_drive_wait ]; do
1122                 result=`Run $MT $MTF $tape rewind 2>&1`
1123                 if [ $? -eq 0 ]; then
1124                         ready=1
1125                         break
1126                 fi
1127                 sleep $poll_drive_ready
1128                 waittime=`expr $waittime + $poll_drive_ready`
1129         done
1130         if [ $ready -eq 0 ]; then
1131                 Exit 2 "$loadslot" "Drive not ready after" \
1132                                    "$max_drive_wait seconds," \
1133                                    "rewind said \"$result\""
1134                 return $?                       # in case we are internal
1135         fi
1136
1137         if [ $loadslot -ne $cleanslot ]; then
1138                 rm -f $slotfile
1139                 echo $loadslot > $slotfile
1140         fi
1141         Exit 0 "$loadslot" "$tape"
1142         return $?                               # in case we are internal
1143 }
1144
1145 ###
1146 # Return information about how the changer is configured and the current
1147 # state of the robot.
1148 ###
1149
1150 info() {
1151         test -n "$DEBUG" && set -x
1152         get_loaded_info
1153         get_slot_list
1154         Log       "INFO     -> first slot: $firstslot"
1155         LogAppend "         -> current slot: $currentslot"
1156         LogAppend "         -> loaded slot: $loadedslot"
1157         LogAppend "         -> last slot: $lastslot"
1158         LogAppend "         -> slot list: $slot_list"
1159         LogAppend "         -> can go backwards: 1"
1160         LogAppend "         -> havereader: $havereader"
1161
1162         ###
1163         # Check if a barcode reader is configured or not.  If so, it
1164         # passes the 4th item in the echo back to amtape signifying it
1165         # can search based on barcodes.
1166         ###
1167         reader=
1168         if [ $havereader -eq 1 ]; then
1169                 reader=1
1170         fi
1171
1172         if [ $currentslot -lt $firstslot -o $currentslot -gt $lastslot ]; then
1173                 currentslot=$firstslot          # what "current" will get
1174         fi
1175         numslots=`expr $lastslot - $firstslot + 1`
1176         Exit 0 "$currentslot" "$numslots 1 $reader"
1177         return $?                               # in case we are internal
1178 }
1179
1180 ###
1181 # Read the labelfile and scan for a particular entry.
1182 ###
1183
1184 read_labelfile() {
1185         labelfile_entry_found=0
1186         labelfile_label=
1187         labelfile_barcode=
1188
1189         lbl_search=$1
1190         bc_search=$2
1191
1192         line=0
1193         while read lbl bc junk; do
1194                 line=`expr $line + 1`
1195                 if [ -z "$lbl" -o -z "$bc" -o -n "$junk" ]; then
1196                         Log       "ERROR    -> Line $line malformed: $lbl $bc $junk"
1197                         LogAppend "         -> Remove $labelfile" \
1198                                                "and run" \
1199                                                "\"$sbindir/amtape $config update\""
1200                         Exit 2 \
1201                              "<none>" \
1202                              "Line $line malformed in $labelfile: $lbl $bc $junk"
1203                         return $?               # in case we are internal
1204                 fi
1205                 if [ $lbl = "$lbl_search" -o $bc = "$bc_search" ]; then
1206                         if [ $labelfile_entry_found -ne 0 ]; then
1207                                 Log       "ERROR    -> Duplicate entries: $labelfile line $line"
1208                                 LogAppend "         -> Remove $labelfile" \
1209                                                        "and run" \
1210                                                        "\"$sbindir/amtape $config update\""
1211                                 Exit 2 \
1212                                      "<none>" \
1213                                      "Duplicate entries: $labelfile line $line"
1214                                 return $?       # in case we are internal
1215                         fi
1216                         labelfile_entry_found=1
1217                         labelfile_label=$lbl
1218                         labelfile_barcode=$bc
1219                 fi
1220         done
1221 }
1222
1223 ###
1224 # Adds the label and barcode for the currently loaded tape to the
1225 # barcode file.  Return an error if the database is messed up.
1226 ###
1227
1228 addlabel() {
1229         test -n "$DEBUG" && set -x
1230         if [ $# -lt 1 ]; then
1231                 Exit 2 "<none>" "Missing -label argument"
1232                 return $?                       # in case we are internal
1233         fi
1234         tapelabel=$1
1235         if [ $havereader -eq 0 ]; then
1236                 Exit 2 "<none>" "Not configured with barcode reader"
1237                 return $?                       # in case we are internal
1238         fi
1239         get_loaded_info
1240         if [ $loadedslot -lt 0 ]; then
1241                 Exit 1 "<none>" "No tape currently loaded"
1242                 return $?                       # in case we are internal
1243         fi
1244         Log       "LABEL    -> Adding label \"$tapelabel\"" \
1245                                "with barcode \"$loadedbarcode\"" \
1246                                "for slot $loadedslot" \
1247                                "into $labelfile"
1248         read_labelfile "$tapelabel" "$loadedbarcode" < $labelfile
1249         if [ $labelfile_entry_found -ne 0 ]; then
1250                 lf_val=
1251                 if [ "$labelfile_barcode" != "$loadedbarcode" ]; then
1252                         lf_type=label
1253                         lf_val=$tapelabel
1254                         val_type=barcode
1255                         old_val=$labelfile_barcode
1256                         new_val=$loadedbarcode
1257                 elif [ "$labelfile_label" != "$tapelabel" ]; then
1258                         lf_type=barcode
1259                         lf_val=$loadedbarcode
1260                         val_type=label
1261                         old_val=$labelfile_label
1262                         new_val=$tapelabel
1263                 fi
1264                 if [ -n "$lf_val" ]; then
1265                         LogAppend "ERROR    -> !!! Label database corrupted !!!"
1266                         LogAppend "         -> \"$old_val\" conflicts with" \
1267                                                "new $val_type \"$new_val\"" \
1268                                                "for $lf_type \"$lf_val\""
1269                         LogAppend "         -> Remove $labelfile" \
1270                                                "and run" \
1271                                                "\"$sbindir/amtape $config update\""
1272                         Exit 2 \
1273                              "<none>" \
1274                              "$tapelabel: \"$old_val\" conflicts with" \
1275                              "new $val_type \"$new_val\"" \
1276                              "for $lf_type \"$lf_val\""
1277                         return $?               # in case we are internal
1278                 fi
1279                 LogAppend "         -> already synced"
1280         else
1281                 echo "$tapelabel $loadedbarcode" >> $labelfile
1282                 LogAppend "         -> appended $labelfile entry:" \
1283                                        "$tapelabel $loadedbarcode"
1284         fi
1285         Exit 0 "$loadedslot" "$tape"
1286         return $?                               # in case we are internal
1287 }
1288
1289 ###
1290 # Look for a label in the barcode file.  If found, locate the slot it's
1291 # in by looking for the barcode in the mtx output, then load that tape.
1292 ###
1293
1294 searchtape() {
1295         test -n "$DEBUG" && set -x
1296         if [ $# -lt 1 ]; then
1297                 Exit 2 "<none>" "Missing -search argument"
1298                 return $?                       # in case we are internal
1299         fi
1300         tapelabel=$1
1301         if [ $havereader -eq 0 ]; then
1302                 Exit 2 "<none>" "Not configured with barcode reader"
1303                 return $?                       # in case we are internal
1304         fi
1305         Log "SEARCH   -> Hunting for label \"$tapelabel\""
1306         read_labelfile "$tapelabel" "" < $labelfile
1307         if [ $labelfile_entry_found -eq 0 ]; then
1308                 LogAppend "         -> !!! label \"$tapelabel\" not found" \
1309                                        "in $labelfile !!!"
1310                 LogAppend "         -> Remove $labelfile" \
1311                                        "and run" \
1312                                        "\"$sbindir/amtape $config update\""
1313                 Exit 2 \
1314                      "<none>" \
1315                      "$tapelabel: label \"$tapelabel\" not found in $labelfile"
1316                 return $?                       # in case we are internal
1317         fi
1318         LogAppend "         -> barcode is \"$labelfile_barcode\""
1319         get_mtx_status
1320         foundslot=`sed -n '
1321 /VolumeTag *= *'$labelfile_barcode' *$/                 {
1322         s/.*Storage Element \([0-9][0-9]*\).*/\1/p
1323         q
1324 }
1325 ' < $mtx_status`
1326         LogAppend "         -> foundslot is $foundslot"
1327         if [ -z "$foundslot" ]; then
1328                 LogAppend "ERROR    -> !!! Could not find slot" \
1329                                        "for barcode \"$labelfile_barcode\"!!!"
1330                 LogAppend "         -> Remove $labelfile" \
1331                                        "and run" \
1332                                        "\"$sbindir/amtape $config update\""
1333                 Exit 2 \
1334                      "<none>" \
1335                      "barcode \"$labelfile_barcode\"" \
1336                      "not found in mtx status output"
1337                 return $?                       # in case we are internal
1338         fi
1339         # Call loadslot without doing it as an internal and let it finish
1340         # things up.
1341         loadslot $foundslot
1342         # NOTREACHED
1343         Exit 2 "<none>" "searchtape: should not get here"
1344         return $?                               # in case we are internal
1345 }
1346
1347 ###
1348 # Program invocation begins here
1349 ###
1350
1351 if [ $# -lt 1 ]; then
1352         Exit 2 "<none>" "Usage: $myname -command args"
1353 fi
1354 cmd=$1
1355 shift
1356 case "$cmd" in
1357 -slot)
1358         loadslot "$@"
1359         ;;
1360 -info)
1361         info "$@"
1362         ;;
1363 -reset)
1364         reset "$@"
1365         ;;
1366 -eject)
1367         eject "$@"
1368         ;;
1369 -label) 
1370         addlabel "$@"
1371         ;;
1372 -search)
1373         searchtape "$@"
1374         ;;
1375 -clean)
1376         loadslot clean
1377         ;;
1378 *)
1379         Exit 2 "<none>" "unknown option: $cmd"
1380         ;;
1381 esac
1382
1383 Exit 2 "<none>" "$myname: should not get here"