464c07519537bdc4053e16dde5b0b92f11e124f9
[debian/amanda] / changer-src / chg-zd-mtx.sh
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 ####
212 # Now it is time to test the setup.  Do all of the following in the
213 # directory that contains the amanda.conf file, and do all of it as
214 # the Amanda user.
215 #
216 #   * Run this:
217 #
218 #       .../chg-zd-mtx -info
219 #       echo $?             #### (or "echo $status" if you use csh/tcsh)
220 #
221 #     You should get a single line from the script like this (the actual
222 #     numbers will vary):
223 #
224 #       5 9 1 1
225 #
226 #     The first number (5) is the "current" slot.  This may or may not be
227 #     the slot actually loaded at the moment (if any).  It is the slot
228 #     Amanda will try to use next.
229 #
230 #     The second number (9) is the number of slots.
231 #
232 #     The third number will always be "1" and indicates the changer is
233 #     capable of going backward.
234 #
235 #     The fourth number is optional.  If you set $havereader to 1, it
236 #     will be "1", otherwise it will not be present.
237 #
238 #     The exit code ($? or $status) should be zero.
239 #
240 #   * Run this:
241 #
242 #       .../chg-zd-mtx -reset
243 #       echo $?
244 #
245 #     The script should output a line like this:
246 #
247 #       1 /dev/rmt/0mn
248 #
249 #     The number at the first should match $firstslot.  The device name
250 #     after that should be your tape device.
251 #
252 #     The exit code ($? or $status) should be zero.
253 #
254 #   * Run this:
255 #
256 #       .../chg-zd-mtx -slot next
257 #       echo $?
258 #
259 #     The script should output a line like this:
260 #
261 #       2 /dev/rmt/0mn
262 #
263 #     The number at the first should be one higher than $firstslot.
264 #     The device name after that should be your tape device.
265 #
266 #     The exit code ($? or $status) should be zero.
267 #
268 #   * Run this:
269 #
270 #       .../chg-zd-mtx -slot current
271 #       echo $?
272 #
273 #     Assuming the tape is still loaded from the previous test, the
274 #     robot should not move and the script should report the same thing
275 #     the previous command did.
276 #
277 #   * If you continue to run "-slot next" commands, the robot should load
278 #     each tape in turn then wrap back around to the first when it
279 #     reaches $lasttape.  If $cleanslot is within the $firstslot to
280 #     $lastslot range, the script will skip over that entry.
281 #
282 #   * Finally, try some of the amtape commands and make sure they work:
283 #
284 #       amtape <config> reset
285 #       amtape <config> slot next
286 #       amtape <config> slot current
287 #
288 #   * If you set $havereader non-zero, now would be a good time to create
289 #     the initial barcode database:
290 #
291 #       amtape <config> update
292 ####
293
294 ################################################################################
295 # To debug this script, first look in @AMANDA_DBGDIR@.  The script
296 # uses one of two log files there, depending on what version of Amanda
297 # is calling it.  It may be chg-zd-mtx.YYYYMMDD*.debug, or it may be
298 # changer.debug.driveN where 'N' is the drive number.
299 #
300 # If the log file does not help, try running the script, **as the Amanda
301 # user**, in the amanda.conf directory with whatever set of args the log
302 # said were used when you had a problem.  If nothing else useful shows up
303 # in the output, try running the script with the DEBUG environment variable
304 # set non-null, e.g.:
305 #
306 #       env DEBUG=yes .../chg-zd-mtx ...
307 ################################################################################
308
309 # source utility functions and values from configure
310 prefix=@prefix@
311 exec_prefix=@exec_prefix@
312 amlibexecdir=@amlibexecdir@
313 . ${amlibexecdir}/chg-lib.sh
314
315 test -n "$DEBUG" && set -x
316 TMPDIR="@AMANDA_TMPDIR@"
317 DBGDIR="@AMANDA_DBGDIR@"
318
319 argv0=$0
320 myname=`expr "$argv0" : '.*/\(.*\)'`
321
322 config=`pwd 2>/dev/null`
323 config=`expr "$config" : '.*/\(.*\)'`
324
325 ###
326 # Functions to write a new log file entry and append more log information.
327 ###
328
329 ds=`date '+%H:%M:%S' 2>/dev/null`
330 if [ $? -eq 0  -a  -n "$ds" ]; then
331         logprefix=`echo "$ds" | sed 's/./ /g'`
332 else
333         logprefix=""
334 fi
335
336 LogAppend() {
337         if [ -z "$logprefix" ]; then
338                 echo "$@" >> $DBGFILE
339         else
340                 echo "$logprefix" "$@" >> $DBGFILE
341         fi
342 }
343
344 Log() {
345         if [ -z "$logprefix" ]; then
346                 echo "===" "`date`" "===" >> $DBGFILE
347                 echo "$@" >> $DBGFILE
348         else
349                 ds=`date '+%H:%M:%S' 2>/dev/null`
350                 echo "$ds" "$@" >> $DBGFILE
351         fi
352 }
353
354 ###
355 # Common exit function.
356 #
357 #   $1 = exit code
358 #   $2 = slot result
359 #   $3 = additional information (error message, tape devive, etc)
360 ###
361
362 internal_call=0
363 Exit() {
364         if [ $internal_call -gt 0 ]; then
365                 call_type=Return
366         else
367                 call_type=Exit
368         fi
369         code=$1
370         shift
371         exit_slot=$1
372         shift
373         exit_answer="$@"
374         Log $call_type "($code) -> $exit_slot $@"
375         echo "$exit_slot" "$@"
376         if [ $call_type = Return ]; then
377                 return $code
378         fi
379         amgetconf dbclose.$argv0:$DBGFILE > /dev/null 2>&1
380         exit $code
381 }
382
383 ###
384 # Function to run another command and log it.
385 ###
386
387 Run() {
388         Log `_ 'Running: %s' "$*"`
389         rm -f $stdout $stderr
390         "$@" > $stdout 2> $stderr
391         exitcode=$?
392         Log `_ 'Exit code: %s' "$exitcode"`
393         if [ -s $stdout ]
394         then
395                 LogAppend Stdout:
396                 cat $stdout >> $DBGFILE
397         fi
398         if [ -s $stderr ]
399         then
400                 LogAppend Stderr:
401                 cat $stderr >> $DBGFILE
402         fi
403         cat $stdout
404         cat $stderr 1>&2
405         return $exitcode
406 }
407
408 ###
409 # Return success if the arg is numeric.
410 ###
411
412 IsNumeric() {
413         test -z "$1" && return 1
414         x="`expr "$1" : '\([-0-9][0-9]*\)' 2>/dev/null`"
415         return `expr X"$1" != X"$x"`
416 }
417
418 ###
419 # Run $MTX status unless the previous output is still valid.
420 ###
421
422 mtx_status_valid=0
423 get_mtx_status() {
424         test -n "$DEBUG" && set -x
425         if [ $mtx_status_valid -ne 0 ]; then
426                 return 0
427         fi
428         rm -f $mtx_status
429         Run $MTX status > $mtx_status 2>&1
430         status=$?
431         if [ $status -eq 0 ]; then
432                 mtx_status_valid=1
433         fi
434         return $status
435 }
436
437 ###
438 # Determine the slot currently loaded.  Set $loadedslot to the slot
439 # currently loaded, or "-1", and $loadedbarcode to the corresponding
440 # barcode (or nothing).
441 ###
442
443 get_loaded_info() {
444         test -n "$DEBUG" && set -x
445         get_mtx_status
446
447         set x `sed -n '
448 /^Data Transfer Element:Empty/                          {
449     s/.*/-1/p
450     q
451 }
452 /^Data Transfer Element '$driveslot':Empty/             {
453     s/.*/-1/p
454     q
455 }
456 /^Data Transfer Element:Full (Storage Element \([0-9][0-9]*\) Loaded):VolumeTag *= *\([^     ]*\)/               {
457     s/.*(Storage Element \([0-9][0-9]*\) Loaded):VolumeTag *= *\([^     ]*\)/\1 \2/p
458     q
459 }
460 /^Data Transfer Element '$driveslot':Full (Storage Element \([0-9][0-9]*\) Loaded):VolumeTag *= *\([^     ]*\)/  {
461     s/.*(Storage Element \([0-9][0-9]*\) Loaded):VolumeTag *= *\([^     ]*\)/\1 \2/p
462     q
463 }
464 /^Data Transfer Element '$driveslot':Full (Unknown Storage Element Loaded):VolumeTag *= *\([^     ]*\)/ {
465     s/.*:VolumeTag *= *\([^     ]*\)/-2 \1/p
466     q
467 }
468 /^Data Transfer Element:Full (Storage Element \([0-9][0-9]*\) Loaded)/                           {
469     s/.*(Storage Element \([0-9][0-9]*\) Loaded).*/\1/p
470     q
471 }
472 /^Data Transfer Element '$driveslot':Full (Storage Element \([0-9][0-9]*\) Loaded)/              {
473     s/.*Storage Element \([0-9][0-9]*\) Loaded.*/\1/p
474     q
475 }
476 /^Data Transfer Element '$driveslot':Full (Unknown Storage Element Loaded)/     {
477     s/.*/-2/p
478     q
479 }
480 ' < $mtx_status 2>&1`
481         shift                                   # get rid of the "x"
482         loadedslot=$1
483         loadedbarcode=$2
484         if [ -z "$loadedslot" ]; then
485                 Exit 2 \
486                     `_ '<none>'` \
487                     "could not determine current slot, are you sure your drive slot is $driveslot"
488                 return $?                       # in case we are internal
489         fi
490
491         #Use the current slot if it's empty and we don't know which slot is loaded'
492         if [ $loadedslot -eq -2 ]; then
493                 set x `sed -n '
494 {
495     /^.*Storage Element '$currentslot':Empty/ {
496         s/.*Storage Element \([0-9][0-9]*\):Empty/\1/p
497         q
498     }
499     /^.*Storage Element '$currentslot':Full/ {
500         s/.*Storage Element \([0-9][0-9]*\):Full/-2/p
501         q
502     }
503     /^.*Storage Element '$currentslot' IMPORT\/EXPORT:Empty/ {
504         s/.*Storage Element \([0-9][0-9]*\) IMPORT\/EXPORT:Empty/\1/p
505         q
506     }
507     /^.*Storage Element '$currentslot' IMPORT\/EXPORT:Full/ {
508         s/.*Storage Element \([0-9][0-9]*\) IMPORT\/EXPORT:Full/-2/p
509         q
510     }
511 }
512 ' < $mtx_status 2>& 1`
513                 shift                           # get rid of the "x"
514                 loadedslotx=$1
515                 if [ ! -z $loadedslotx ]; then
516                         loadedslot=$loadedslotx
517                 fi
518         fi
519
520         #Use the first empty slot if we don't know which slot is loaded'
521         if [ $loadedslot -eq -2 ]; then
522                 set x `sed -n '
523 {
524     /^.*Storage Element \([0-9][0-9]*\):Empty/ {
525         s/.*Storage Element \([0-9][0-9]*\):Empty/\1/p
526         q
527     }
528     /^.*Storage Element \([0-9][0-9]*\) IMPORT\/EXPORT:Empty/ {
529         s/.*Storage Element \([0-9][0-9]*\) IMPORT\/EXPORT:Empty/\1/p
530         q
531     }
532 }
533 ' < $mtx_status 2>& 1`
534                 shift                           # get rid of the "x"
535                 loadedslot=$1
536         fi
537
538         if IsNumeric "$loadedslot" ; then
539                 :
540         else
541                 Exit 2 \
542                      `_ '<none>'` \
543                      "currently loaded slot ($loadedslot) not numeric"
544                 return $?                       # in case we are internal
545         fi
546         Log       `_ 'STATUS   -> currently loaded slot = %s' "$loadedslot"`
547         LogAppend `_ '         -> currently loaded barcode = "%s"' "$loadedbarcode"`
548 }
549
550 ###
551 # Get a list of slots between $firstslot and $lastslot, if they are set.
552 # If they are not set, set them to the first and last slot seen on the
553 # assumption the entire robot is to be used (???).
554 ###
555
556 slot_list=
557 get_slot_list() {
558         test -n "$DEBUG" && set -x
559         if [ -n "$slot_list" ]; then
560                 return
561         fi
562         get_mtx_status
563         slot_list=`sed -n '
564 /^Data Transfer Element:Full (Storage Element \([0-9][0-9]*\) Loaded)/ {
565     s/.*(Storage Element \([0-9][0-9]*\) Loaded).*/\1/p
566 }
567 /^Data Transfer Element '$driveslot':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 (Unknown Storage Element Loaded)/ {
571     : loop
572     n
573     /^.*Storage Element \([0-9][0-9]*\):Full/ {
574         s/.*Storage Element \([0-9][0-9]*\):Full.*/\1/p
575         b loop
576     }
577     /^.*Storage Element \([0-9][0-9]*\):Empty/ {
578         s/.*Storage Element \([0-9][0-9]*\):Empty/\1/p
579     }
580 }
581 /^.*Storage Element \([0-9][0-9]*\):Full/ {
582     s/.*Storage Element \([0-9][0-9]*\):Full.*/\1/p
583 }
584 /^.*Storage Element \([0-9][0-9]*\) IMPORT\/EXPORT:Full/ {
585     s/.*Storage Element \([0-9][0-9]*\) IMPORT\/EXPORT:Full.*/\1/p
586 }
587 ' < $mtx_status 2>&1 | grep -v "^${cleanslot}\$" | sort -n`
588         slot_list=`echo $slot_list`             # remove the newlines
589         if [ $firstslot -lt 0 -o $lastslot -lt 0 ]; then
590                 last=$lastslot
591                 for slot in $slot_list; do
592                         if [ $firstslot -lt 0 ]; then
593                                 Log `_ 'SLOTLIST -> firstslot set to %s' "$slot"`
594                                 firstslot=$slot
595                         fi
596                         if [ $lastslot -lt 0 ]; then
597                                 last=$slot
598                         fi
599                 done
600                 if [ $lastslot -lt 0 -a $last -ge 0 ]; then
601                         Log `_ 'SLOTLIST -> lastslot set to %s' "$last"`
602                         lastslot=$last
603                 fi
604                 if [ $firstslot -lt 0 ]; then
605                         Exit 2 \
606                              `_ '<none>'` \
607                              `_ 'cannot determine first slot'`
608                         return $?               # in case we are internal
609                 elif [ $lastslot -lt 0 ]; then
610                         Exit 2 \
611                              `_ '<none>'` \
612                              `_ 'cannot determine last slot'`
613                         return $?               # in case we are internal
614                 fi
615         fi
616         amanda_slot_list=
617         for slot in $slot_list; do
618                 if [ $slot -ge $firstslot -a $slot -le $lastslot ]; then
619                         amanda_slot_list="$amanda_slot_list $slot"
620                 fi
621         done
622         if [ -z "$amanda_slot_list" ]; then
623                 Exit 2 \
624                      `_ '<none>'` \
625                      "no slots available"
626                 return $?                       # in case we are internal
627         fi
628         slot_list="$amanda_slot_list"
629 }
630
631 DBGFILE=`amgetconf dbopen.$argv0 2>/dev/null`
632 if [ -z "$DBGFILE" ]
633 then
634         DBGFILE=/dev/null                       # will try this again below
635 fi
636
637 changerfile=`amgetconf changerfile 2>/dev/null`
638 if [ -z "$changerfile" ]; then
639         Exit 2 \
640              `_ '<none>'` \
641              "changerfile must be specified in amanda.conf"
642 fi
643
644 rawtape=`amgetconf tapedev 2>/dev/null`
645 if [ -z "$rawtape" ]; then
646         Exit 2 \
647              `_ '<none>'` \
648              "tapedev may not be empty"
649 fi
650 tape=`tape_device_filename "$rawtape"`
651 if [ -z "$tape" ]; then
652         Exit 2 \
653              ` _ '<none>'` \
654              "tapedev $rawtape is not a tape device."
655 elif [ $tape = "/dev/null" -o `expr "$tape" : 'null:'` -eq 5 ]; then
656         Exit 2 \
657              `_ '<none>'` \
658              "tapedev ($tape) may not be the null device"
659 fi
660 # Confusingly, TAPE is the name of the changer device...
661 TAPE=`amgetconf changerdev 2>/dev/null`
662 if [ -z "$TAPE" ]; then
663         Exit 2 \
664              `_ '<none>'` \
665              "changerdev may not be empty"
666 elif [ $TAPE = "/dev/null" ]; then
667         Exit 2 \
668              `_ '<none>'` \
669              "changerdev ($TAPE) may not be the null device"
670 fi
671 export TAPE                                     # for mtx command
672
673 CHANGER=$TAPE 
674 export CHANGER                                  # for mtx command
675
676 #### Set up the various config files.
677
678 conf_match=`expr "$changerfile" : .\*\.conf\$`
679 if [ $conf_match -ge 6 ]; then
680         configfile=$changerfile
681         changerfile=`echo $changerfile | sed 's/.conf$//g'`
682 else
683         configfile=$changerfile.conf
684 fi
685
686 if [ ! -e $configfile ]; then
687         Exit 2 \
688              `_ '<none>'` \
689              "configuration file \"$configfile\" doesn't exist"
690 fi
691 if [ ! -f $configfile ]; then
692         Exit 2 \
693              `_ '<none>'` \
694              "configuration file \"$configfile\" is not a file"
695 fi
696
697 cleanfile=$changerfile-clean
698 accessfile=$changerfile-access
699 slotfile=$changerfile-slot
700 labelfile=$changerfile-barcodes
701 [ ! -s $cleanfile ] && echo 0 > $cleanfile
702 [ ! -s $accessfile ] && echo 0 > $accessfile
703 [ ! -s $slotfile ] && echo -1 > $slotfile
704 [ ! -f $labelfile ] && > $labelfile
705 cleancount=`cat $cleanfile`
706 accesscount=`cat $accessfile`
707
708 #### Dig out of the config file what is needed
709
710 varlist=
711 varlist="$varlist firstslot"
712 varlist="$varlist lastslot"
713 varlist="$varlist cleanslot"
714 varlist="$varlist cleancycle"
715 varlist="$varlist OFFLINE_BEFORE_UNLOAD"        # old name
716 varlist="$varlist offline_before_unload"
717 varlist="$varlist unloadpause"
718 varlist="$varlist AUTOCLEAN"                    # old name
719 varlist="$varlist autoclean"
720 varlist="$varlist autocleancount"
721 varlist="$varlist havereader"
722 varlist="$varlist driveslot"
723 varlist="$varlist poll_drive_ready"
724 varlist="$varlist initial_poll_delay"
725 varlist="$varlist max_drive_wait"
726
727 for var in $varlist
728 do
729         val="`cat $configfile 2>/dev/null | sed -n '
730 # Ignore comment lines (anything starting with a #).
731 /^[     ]*#/d
732 # Find the first var=val line in the file, print the value and quit.
733 /^[     ]*'$var'[       ]*=[    ]*\([^  ][^     ]*\).*/ {
734         s/^[    ]*'$var'[       ]*=[    ]*\([^  ][^     ]*\).*/\1/p
735         q
736 }
737 '`"
738         eval $var=\"$val\"
739 done
740
741 # Deal with driveslot first so we can get DBGFILE set if we are still
742 # using the old amgetconf.
743
744 if [ -z "$driveslot" ]; then
745         driveslot=0;
746 fi
747
748 # Get DBGFILE set if it is not already.
749
750 if [ $DBGFILE = /dev/null ]; then
751         if [ -d "$DBGDIR" ]; then
752                 DBGFILE=$DBGDIR/changer.debug.drive$driveslot
753         else
754                 DBGFILE=/dev/null
755         fi
756         Log `_ '=== Start %s ===' "\`date\`"`
757 fi
758
759 stdout=$TMPDIR/$myname.1.$$
760 stderr=$TMPDIR/$myname.2.$$
761 mtx_status=$TMPDIR/$myname.status.$$
762 trap "rm -f $stdout $stderr $mtx_status" 0      # exit cleanup
763
764 Log `_ 'Using config file %s' "$configfile"`
765
766 # Log the argument list.
767
768 Log `_ "Arg info:"`
769 LogAppend "\$# = $#"
770 i=0
771 LogAppend "\$$i = \"$argv0\""
772 for arg in "$@"; do
773         i=`expr $i + 1`
774         LogAppend "\$$i = \"$arg\""
775 done
776
777 # Set the default config values for those not in the file.  Log the
778 # results and make sure each is valid (numeric).
779
780 firstslot=${firstslot:-'-1'}                            # default: mtx status
781 lastslot=${lastslot:-'-1'}                              # default: mtx status
782 cleanslot=${cleanslot:-'-1'}                            # default: -1
783 cleancycle=${cleancycle:-'120'}                         # default: two minutes
784 if [ -z "$offline_before_unload" -a -n "$OFFLINE_BEFORE_UNLOAD" ]; then
785         offline_before_unload=$OFFLINE_BEFORE_UNLOAD    # (old name)
786 fi
787 offline_before_unload=${offline_before_unload:-'0'}     # default: 0
788 unloadpause=${unloadpause:-'0'}                         # default: 0
789 if [ -z "$autoclean" -a -n "$AUTOCLEAN" ]; then
790         autoclean=$AUTOCLEAN                            # (old name)
791 fi
792 autoclean=${autoclean:-'0'}                             # default: 0
793 autocleancount=${autocleancount:-'99'}                  # default: 99
794 havereader=${havereader:-'0'}                           # default: 0
795 poll_drive_ready=${poll_drive_ready:-'3'}               # default: three seconds
796 initial_poll_delay=${initial_poll_delay:-'0'}           # default: zero zeconds
797 max_drive_wait=${max_drive_wait:-'120'}                 # default: two minutes
798
799 # check MT and MTX for sanity
800 if test "${MTX:0:1}" = "/"; then
801     if ! test -f "${MTX}"; then
802         Exit 2 \
803             `_ '<none>'` \
804             `_ "mtx binary at '%s' not found" "$MTX"`
805     fi
806     if ! test -x "${MTX}"; then
807         Exit 2 \
808             `_ '<none>'` \
809             `_ "mtx binary at '%s' is not executable" "$MTX"`
810     fi
811 else
812     # try running it to see if the shell can find it
813     "$MTX" >/dev/null 2>/dev/null
814     if test $? -eq 127 -o $? -eq 126; then
815         Exit 2 \
816             `_ '<none>'` \
817             `_ "Could not run mtx binary at '%s'" "$MTX"`
818     fi
819 fi
820
821 error=`try_find_mt`
822 if test $? -ne 0; then
823     Exit 2 '<none>' $error
824 fi
825
826 get_slot_list
827
828 Log `_ "Config info:"`
829 for var in $varlist; do
830         if [ $var = "OFFLINE_BEFORE_UNLOAD" ]; then
831                 continue                        # old name
832         elif [ $var = "AUTOCLEAN" ]; then
833                 continue                        # old name
834         fi
835         eval val=\"'$'$var\"
836         if [ -z "$val" ]; then
837                 Exit 2 \
838                      `_ '<none>'` \
839                      `_ '%s missing in %s' "$var" "$configfile"`
840         fi
841         if IsNumeric "$val" ; then
842                 :
843         else
844                 Exit 2 \
845                      `_ '<none>'` \
846                      `_ '%s (%s) not numeric in %s' "$var" "$val" "$configfile"`
847         fi
848         LogAppend $var = \"$val\"
849 done
850
851 # Run the rest of the config file sanity checks.
852
853 if [ $firstslot -gt $lastslot ]; then
854         Exit 2 \
855              `_ '<none>'` \
856              `_ 'firstslot (%s) greater than lastslot (%s) in %s' "$firstslot" "$lastslot" "$configfile"`
857 fi
858 if [ $autoclean -ne 0 -a $cleanslot -lt 0 ]; then
859         Exit 2 \
860              `_ '<none>'` \
861              `_ 'autoclean set but cleanslot not valid (%s)' "$cleanslot"`
862 fi
863
864 # Set up the current slot
865
866 currentslot=`cat $slotfile`
867 if IsNumeric "$currentslot" ; then
868         if [ $currentslot -lt $firstslot ]; then
869                 Log `_ 'SETUP    -> current slot %s less than %s ... resetting to %s' "$currentslot" "$firstslot" "$firstslot"`
870                 currentslot=$firstslot
871         elif [ $currentslot -gt $lastslot ]; then
872                 Log `_ 'SETUP    -> current slot %s greater than %s ... resetting to %s' "$currentslot" "$lastslot" "$lastslot"`
873                 currentslot=$lastslot
874         fi
875 else
876         Log `_ 'SETUP    -> contents of %s (%s) invalid, setting current slot to first slot (%s)' "$slotfile" "$currentslot" "$firstslot"`
877         currentslot=$firstslot
878 fi
879
880 found_current=0
881 first_slot_in_list=-1
882 next_slot_after_current=-1
883 for slot in $slot_list; do
884         if [ $first_slot_in_list -lt 0 ]; then
885                 first_slot_in_list=$slot        # in case $firstslot is missing
886         fi
887         if [ $slot -eq $currentslot ]; then
888                 found_current=1
889                 break
890         elif [ $slot -gt $currentslot ]; then
891                 next_slot_after_current=$slot   # $currentslot is missing
892                 break
893         fi
894 done
895 if [ $found_current -eq 0 ]; then
896         if [ $next_slot_after_current -lt 0 ]; then
897                 new_currentslot=$first_slot_in_list
898         else
899                 new_currentslot=$next_slot_after_current
900         fi
901         Log `_ 'WARNING  -> current slot %s not available, setting current slot to next slot (%s)' "$currentslot" "$new_currentslot"`
902         currentslot=$new_currentslot
903 fi
904
905 # More routines.
906
907 ###
908 # Eject the current tape and put it away.
909 ###
910
911 eject() {
912         test -n "$DEBUG" && set -x
913         Log `_ 'EJECT    -> ejecting tape from %s' "$tape"`
914         get_loaded_info 
915         if [ $loadedslot -gt 0 ]; then
916                 Log `_ 'EJECT    -> moving tape from drive %s to storage slot %s' "$driveslot" "$loadedslot"`
917                 if [ $offline_before_unload -ne 0 ]; then
918                         Run try_eject_device $tape
919                 fi
920                 sleep $unloadpause
921                 result=`Run $MTX unload $loadedslot $driveslot 2>&1`
922                 status=$?
923                 Log `_ '         -> status %s, result "%s"' "$status" "$result"`
924                 mtx_status_valid=0
925                 if [ $status -ne 0 ]; then
926                         answer="$result"
927                         code=2
928                 else
929                         answer="$rawtape"
930                         code=0
931                 fi
932         else
933                 answer=`_ 'Drive was not loaded'`
934                 code=1
935         fi
936         Exit $code "$loadedslot" "$answer"
937         return $?                               # in case we are internal
938 }
939
940 ###
941 # Reset the robot back to the first slot.
942 ###
943
944 reset() {
945         test -n "$DEBUG" && set -x
946         Log `_ 'RESET    -> loading tape from slot %s to drive %s (%s)' "$firstslot" "$driveslot" "$tape"`
947         # Call loadslot without doing it as an internal and let it finish
948         # things up.
949         loadslot $firstslot
950         # NOTREACHED
951         Exit 2 `_ '<none>'` `_ 'reset: should not get here'`
952         return $?                               # in case we are internal
953 }
954
955 ###
956 # Unload the current tape (if necessary) and load a new one (unless
957 # "advance").  If no tape is loaded, get the value of "current" from
958 # $slotfile.
959 ###
960
961 loadslot() {
962         test -n "$DEBUG" && set -x
963         if [ $# -lt 1 ]; then
964                 Exit 2 `_ '<none>'` `_ 'Missing -slot argument'`
965                 return $?                       # in case we are internal
966         fi
967         whichslot=$1
968         Log `_ 'LOADSLOT -> load drive %s (%s) from slot %s' "$driveslot" "$tape" "$whichslot"`
969
970         numeric=`echo $whichslot | sed 's/[^0-9]//g'`
971         case $whichslot in
972         current|prev|next|advance)
973                 find_slot=$currentslot
974                 ;;
975         first)
976                 find_slot=$firstslot
977                 ;;
978         last)
979                 find_slot=$lastslot
980                 ;;
981         $numeric)
982                 find_slot=$numeric
983                 ;;
984         clean)
985                 find_slot=$cleanslot
986                 ;;
987         *)
988                 Exit 2 `_ '<none>'` `_ 'Illegal slot: "%s"' "$whichslot"`
989                 return $?                       # in case we are internal
990                 ;;
991         esac
992
993         # Find the requested slot in the slot list.  By loading the "set"
994         # command with multiple copies, we guarantee that if the slot is
995         # found, we can look both forward and backward without running
996         # off the end.  Putting $cleanslot at the end allows us to find
997         # that slot since it is not in $slot_list.
998         get_slot_list
999         set x $slot_list $slot_list $slot_list $cleanslot
1000         shift                                   # get rid of the "x"
1001         prev_slot=$1
1002         shift
1003         while [ $# -gt 0 ]; do
1004                 if [ $1 -eq $find_slot ]; then
1005                         break
1006                 fi
1007                 prev_slot=$1
1008                 shift
1009         done
1010         if [ $# -le 0 ]; then
1011                 Exit 2 \
1012                      `_ '<none>'` \
1013                      `_ 'Cannot find slot %s in slot list (%s)' "$find_slot " "$slot_list"`
1014                 return $?                       # in case we are internal
1015         fi
1016
1017         # Determine the slot to load.
1018         case $whichslot in
1019         next|advance)
1020                 shift
1021                 loadslot=$1
1022                 ;;
1023         prev)
1024                 loadslot=$prev_slot
1025                 ;;
1026         *)
1027                 loadslot=$find_slot
1028         esac
1029
1030         # If the desired slot is already loaded, we are done.  Only update
1031         # current slot if this is not the cleaning slot.
1032         get_loaded_info
1033         if [ $loadslot = $loadedslot ]; then
1034                 if [ $loadslot -ne $cleanslot ]; then
1035                         rm -f $slotfile
1036                         echo $loadslot > $slotfile
1037                 fi
1038                 Exit 0 "$loadedslot" "$rawtape"
1039                 return $?                       # in case we are internal
1040         fi
1041         if [ $loadedslot -eq -2 ]; then
1042                 Exit 0 "$loadedslot" "$rawtape"
1043                 return $?                       # in case we are internal
1044         fi
1045
1046         # If we are loading the cleaning tape, bump the cleaning count
1047         # and reset the access count.  Otherwise, bump the access count
1048         # and see if it is time to do a cleaning.
1049         if [ $loadslot = $cleanslot ]; then
1050                 rm -f $cleanfile $accessfile
1051                 expr $cleancount + 1 > $cleanfile
1052                 echo 0 > $accessfile
1053         else
1054                 rm -f $accessfile
1055                 expr $accesscount + 1 > $accessfile
1056                 if [ $autoclean -ne 0 -a $accesscount -gt $autocleancount ]
1057                 then
1058                         internal_call=`expr $internal_call + 1`
1059                         loadslot clean > /dev/null 2>&1
1060                         status=$?
1061                         internal_call=`expr $internal_call - 1`
1062                         if [ $status -ne 0 ]; then
1063                                 Exit $status "$loadslot" "$exit_answer"
1064                                 return $?       # in case we are internal
1065                         fi
1066
1067                         # Slot $cleanslot might contain an ordinary tape
1068                         # rather than a cleaning tape.  A cleaning tape
1069                         # *MIGHT* auto-eject; an ordinary tape does not.
1070                         # We therefore have to read the status again to
1071                         # check what actually happened.
1072                         mtx_status_valid=0
1073                         get_loaded_info
1074                 fi
1075         fi
1076
1077         # Unload whatever tape is in the drive.
1078         internal_call=`expr $internal_call + 1`
1079         eject > /dev/null 2>&1
1080         status=$?
1081         internal_call=`expr $internal_call - 1`
1082         if [ $status -gt 1 ]; then
1083                 Exit $status "$exit_slot" "$exit_answer"
1084                 return $?                       # in case we are internal
1085         fi
1086
1087         # If we were doing an "advance", we are done.
1088         if [ $whichslot = advance ]; then
1089                 if [ $loadslot -ne $cleanslot ]; then
1090                         rm -f $slotfile
1091                         echo $loadslot > $slotfile
1092                 fi
1093                 Exit 0 "$loadslot" "/dev/null"
1094                 return $?                       # in case we are internal
1095         fi
1096
1097         # Load the tape, finally!
1098         Log `_ "LOADSLOT -> loading tape from slot %s to drive %s (%s)" "$loadslot" "$driveslot" "$tape"`
1099         result=`Run $MTX load $loadslot $driveslot 2>&1`
1100         status=$?
1101         Log `_ '         -> status %s, result "%s"' "$status" "$result"`
1102         mtx_status_valid=0
1103         if [ $status -ne 0 ]; then
1104                 Exit 2 "$loadslot" "$result"
1105                 return $?                       # in case we are internal
1106         fi
1107
1108         ###
1109         # Cleaning tapes never go "ready", so instead we just sit here
1110         # for "long enough" (as determined empirically by the user),
1111         # then return success.
1112         ###
1113         if [ $loadslot -eq $cleanslot ]; then
1114                 Run sleep $cleancycle
1115                 Exit 0 "$loadslot" "$rawtape"
1116                 return $?                       # in case we are internal
1117         fi
1118
1119         ###
1120         # Wait for the drive to go online.
1121         ###
1122         waittime=0
1123         ready=0
1124         sleep $initial_poll_delay
1125         while [ $waittime -lt $max_drive_wait ]; do
1126                 amdevcheck_status $tape
1127                 if [ $? -eq 0 ]; then
1128                         ready=1
1129                         break
1130                 fi
1131                 sleep $poll_drive_ready
1132                 waittime=`expr $waittime + $poll_drive_ready`
1133         done
1134         if [ $ready -eq 0 ]; then
1135                 Exit 2 "$loadslot" `_ 'Drive not ready after %s seconds, rewind said "%s"' "$max_drive_wait" "$result"`
1136                 return $?                       # in case we are internal
1137         fi
1138
1139         if [ $loadslot -ne $cleanslot ]; then
1140                 rm -f $slotfile
1141                 echo $loadslot > $slotfile
1142         fi
1143         Exit 0 "$loadslot" "$rawtape"
1144         return $?                               # in case we are internal
1145 }
1146
1147 ###
1148 # Return information about how the changer is configured and the current
1149 # state of the robot.
1150 ###
1151
1152 info() {
1153         test -n "$DEBUG" && set -x
1154         get_loaded_info
1155         get_slot_list
1156         Log       `_ 'INFO     -> first slot: %s' "$firstslot"`
1157         LogAppend `_ '         -> current slot: %s' "$currentslot"`
1158         LogAppend `_ '         -> loaded slot: %s' "$loadedslot"`
1159         LogAppend `_ '         -> last slot: %s' "$lastslot"`
1160         LogAppend `_ '         -> slot list: %s' "$slot_list"`
1161         LogAppend `_ '         -> can go backwards: 1'`
1162         LogAppend `_ '         -> havereader: %s' "$havereader"`
1163
1164         ###
1165         # Check if a barcode reader is configured or not.  If so, it
1166         # passes the 4th item in the echo back to amtape signifying it
1167         # can search based on barcodes.
1168         ###
1169         reader=
1170         if [ $havereader -eq 1 ]; then
1171                 reader=1
1172         fi
1173
1174         if [ $currentslot -lt $firstslot -o $currentslot -gt $lastslot ]; then
1175                 currentslot=$firstslot          # what "current" will get
1176         fi
1177         numslots=`expr $lastslot - $firstslot + 1`
1178         Exit 0 "$currentslot" "$numslots 1 $reader"
1179         return $?                               # in case we are internal
1180 }
1181
1182 ###
1183 # Read the labelfile and scan for a particular entry.
1184 ###
1185
1186 read_labelfile() {
1187         labelfile_entry_found=0
1188         labelfile_label=
1189         labelfile_barcode=
1190
1191         lbl_search=$1
1192         bc_search=$2
1193
1194         line=0
1195         while read lbl bc junk; do
1196                 line=`expr $line + 1`
1197                 if [ -z "$lbl" -o -z "$bc" -o -n "$junk" ]; then
1198                         Log       `_ 'ERROR    -> Line %s malformed: %s %s %s' "$line" "$lbl" "$bc" "$junk"`
1199                         LogAppend `_ '         -> Remove %s and run "%s %s update"' "$labelfile" "$sbindir/amtape" "$config"`
1200                         Exit 2 \
1201                              `_ '<none>'` \
1202                              `_ 'Line %s malformed in %s: %s %s %s' "$line" "$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: %s line %s' "$labelfile" "$line"`
1208                                 LogAppend `_ '         -> Remove %s and run "%s %s update"' "$labelfile" "$sbindir/amtape" "$config"`
1209                                 Exit 2 \
1210                                      `_ '<none>'` \
1211                                      `_ 'Duplicate entries: %s line %s' "$labelfile" "$line"`
1212                                 return $?       # in case we are internal
1213                         fi
1214                         labelfile_entry_found=1
1215                         labelfile_label=$lbl
1216                         labelfile_barcode=$bc
1217                 fi
1218         done
1219 }
1220
1221 ###
1222 # Adds the label and barcode for the currently loaded tape to the
1223 # barcode file.  Return an error if the database is messed up.
1224 ###
1225
1226 addlabel() {
1227         test -n "$DEBUG" && set -x
1228         if [ $# -lt 1 ]; then
1229                 Exit 2 `_ '<none>'` `_ 'Missing -label argument'`
1230                 return $?                       # in case we are internal
1231         fi
1232         tapelabel=$1
1233         if [ $havereader -eq 0 ]; then
1234                 Exit 2 `_ '<none>'` `_ 'Not configured with barcode reader'`
1235                 return $?                       # in case we are internal
1236         fi
1237         get_loaded_info
1238         if [ $loadedslot -lt 0 ]; then
1239                 Exit 1 `_ '<none>'` `_ 'No tape currently loaded'`
1240                 return $?                       # in case we are internal
1241         fi
1242         if [ -z "$loadedbarcode" ]; then
1243                 Exit 1 `_ '<none>'` `_ 'No barcode found for tape %s.' $tapelabel`
1244                 return $?                       # in case we are internal
1245         fi
1246         Log       `_ 'LABEL    -> Adding label "%s" with barcode "%s" for slot %s into %s' "$tapelabel" "$loadedbarcode" "$loadedslot" "$labelfile"`
1247         read_labelfile "$tapelabel" "$loadedbarcode" < $labelfile
1248         if [ $labelfile_entry_found -ne 0 ]; then
1249                 lf_val=
1250                 if [ "$labelfile_barcode" != "$loadedbarcode" ]; then
1251                         lf_type=label
1252                         lf_val=$tapelabel
1253                         val_type=barcode
1254                         old_val=$labelfile_barcode
1255                         new_val=$loadedbarcode
1256                 elif [ "$labelfile_label" != "$tapelabel" ]; then
1257                         lf_type=barcode
1258                         lf_val=$loadedbarcode
1259                         val_type=label
1260                         old_val=$labelfile_label
1261                         new_val=$tapelabel
1262                 fi
1263                 if [ -n "$lf_val" ]; then
1264                         LogAppend `_ 'ERROR    -> !!! Label database corrupted !!!'`
1265                         LogAppend `_ '         -> "%s" conflicts with new %s "%s" for %s "%s"' "$old_val" "$val_type" "$new_val" "$lf_type" "$lf_val"`
1266                         Exit 2 \
1267                              `_ '<none>'` \
1268                              `_ '%s: "%s" conflicts with new %s "%s" for %s "%s"' "$tapelabel" "$old_val" "$val_type" "$new_val" "$lf_type" "$lf_val"`
1269                         return $?               # in case we are internal
1270                 fi
1271                 LogAppend `_ "         -> already synced"`
1272         else
1273                 echo "$tapelabel $loadedbarcode" >> $labelfile
1274                 LogAppend `_ '         -> appended %s entry: %s %s' "$labelfile" "$tapelabel" "$loadedbarcode"`
1275         fi
1276         Exit 0 "$loadedslot" "$rawtape"
1277         return $?                               # in case we are internal
1278 }
1279
1280 ###
1281 # Look for a label in the barcode file.  If found, locate the slot it's
1282 # in by looking for the barcode in the mtx output, then load that tape.
1283 ###
1284
1285 searchtape() {
1286         test -n "$DEBUG" && set -x
1287         if [ $# -lt 1 ]; then
1288                 Exit 2 `_ '<none>'` `_ 'Missing -search argument'`
1289                 return $?                       # in case we are internal
1290         fi
1291         tapelabel=$1
1292         if [ $havereader -eq 0 ]; then
1293                 Exit 2 `_ '<none>'` `_ 'Not configured with barcode reader'`
1294                 return $?                       # in case we are internal
1295         fi
1296         Log `_ 'SEARCH   -> Hunting for label "%s"' "$tapelabel"`
1297         read_labelfile "$tapelabel" "" < $labelfile
1298         if [ $labelfile_entry_found -eq 0 ]; then
1299                 LogAppend `_ '         -> !!! label "%s" not found in %s !!!' "$tapelabel" "$labelfile"`
1300                 LogAppend `_ '         -> Remove %s and run "%s %s update"' "$labelfile" "$sbindir/amtape" "$config"`
1301                 Exit 2 \
1302                      `_ '<none>'` \
1303                      `_ '%s: label "%s" not found in %s' "$tapelabel" "$tapelabel" "$labelfile"`
1304                 return $?                       # in case we are internal
1305         fi
1306         LogAppend `_ '         -> barcode is "%s"' "$labelfile_barcode"`
1307         get_mtx_status
1308         foundslot=`sed -n '
1309 /VolumeTag *= *'$labelfile_barcode' *$/                 {
1310         s/.*Storage Element \([0-9][0-9]*\).*/\1/p
1311         q
1312 }
1313 ' < $mtx_status`
1314         LogAppend `_ '         -> foundslot is %s' "$foundslot"`
1315         if [ -z "$foundslot" ]; then
1316                 LogAppend `_ 'ERROR    -> !!! Could not find slot for barcode "%s"!!!' "$labelfile_barcode"`
1317                 LogAppend `_ '         -> Remove %s and run "%s %s update"' "$labelfile" "$sbindir/amtape" "$config"`
1318                 Exit 2 \
1319                      `_ '<none>'` \
1320                      `_ 'barcode "%s" not found in mtx status output' "$labelfile_barcode"`
1321                 return $?                       # in case we are internal
1322         fi
1323         # Call loadslot without doing it as an internal and let it finish
1324         # things up.
1325         loadslot $foundslot
1326         # NOTREACHED
1327         Exit 2 `_ '<none>'` `_ 'searchtape: should not get here'`
1328         return $?                               # in case we are internal
1329 }
1330
1331 ###
1332 # Program invocation begins here
1333 ###
1334
1335 if [ $# -lt 1 ]; then
1336         Exit 2 `_ '<none>'` `_ 'Usage: %s -command args' "$myname"`
1337 fi
1338 cmd=$1
1339 shift
1340 case "$cmd" in
1341 -slot)
1342         loadslot "$@"
1343         ;;
1344 -info)
1345         info "$@"
1346         ;;
1347 -reset)
1348         reset "$@"
1349         ;;
1350 -eject)
1351         eject "$@"
1352         ;;
1353 -label) 
1354         addlabel "$@"
1355         ;;
1356 -search)
1357         searchtape "$@"
1358         ;;
1359 -clean)
1360         loadslot clean
1361         ;;
1362 *)
1363         Exit 2 `_ '<none>'` `_ 'unknown option: %s' "$cmd"`
1364         ;;
1365 esac
1366
1367 Exit 2 `_ '<none>'` `_ '%s: should not get here' "$myname"`