8 # Contributed by Eric DOUTRELEAU <Eric.Doutreleau@int-evry.fr>
9 # This is supposed to work with Zubkoff/Dandelion version of mtx
11 # Modified by Joe Rhett <jrhett@isite.net>
12 # to work with MTX 1.2.9 by Eric Lee Green http://mtx.sourceforge.net
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.
19 ################################################################################
20 # Here are the things you need to do and know to configure this script:
22 # * Figure out what the robot device name is and what the tape drive
23 # device name is. They will be different!
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.
29 # For instance, here is what mtx has to say about my current robot:
31 # Product Type: Medium Changer
33 # Product ID: 'ACL2640 206 '
35 # Attached Changer: No
37 # and here is what it says about a tape drive:
39 # Product Type: Tape Drive
40 # Vendor ID: 'Quantum '
41 # Product ID: 'DLT4000 '
43 # Attached Changer: No
45 # Note the "Product Type" value makes it clear which is which.
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.
52 # Once you have this figured out, set "changerdev" in amanda.conf
53 # to the changer device and "tapedev" to the tape device.
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):
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
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.
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).
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.
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.
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
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).
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.
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).
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.
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
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.
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.
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.
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.
159 # Now you are ready to set up the variables in the changer configuration
162 # All variables are in "changerfile".conf where "changerfile" is set
163 # in amanda.conf. For example, if amanda.conf has:
165 # changerfile="/etc/amanda/Dailyset1/CHANGER"
166 # or changerfile="/etc/amanda/Dailyset1/CHANGER.conf"
168 # the variables must be in "/etc/amanda/Dailyset1/CHANGER.conf".
169 # The ".conf" is appended only if it's not there".
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.
175 # Here is a commented out example file with all the variables and showing
176 # their default value (if any):
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
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.
191 # autoclean=0 #### Set to '1' or greater to enable
193 # autocleancount=99 #### Number of access before a clean.
195 # cleancycle=120 #### Time (seconds) to clean drive (default 120)
197 # havereader=0 #### If you have a barcode reader, set to 1.
199 # offline_before_unload=0 #### Does your robot require an
200 # #### 'mt offline' before mtx unload?
202 # poll_drive_ready=NN #### Time (seconds) between tests to see if
203 # #### the tape drive has gone ready (default: 3).
205 # max_drive_wait=NN #### Maximum time (seconds) to wait for the
206 # #### tape drive to become ready (default: 120).
208 # initial_poll_delay=NN #### initial delay after load before polling for
214 # Now it is time to test the setup. Do all of the following in the
215 # directory that contains the amanda.conf file, and do all of it as
220 # .../chg-zd-mtx -info
221 # echo $? #### (or "echo $status" if you use csh/tcsh)
223 # You should get a single line from the script like this (the actual
224 # numbers will vary):
228 # The first number (5) is the "current" slot. This may or may not be
229 # the slot actually loaded at the moment (if any). It is the slot
230 # Amanda will try to use next.
232 # The second number (9) is the number of slots.
234 # The third number will always be "1" and indicates the changer is
235 # capable of going backward.
237 # The fourth number is optional. If you set $havereader to 1, it
238 # will be "1", otherwise it will not be present.
240 # The exit code ($? or $status) should be zero.
244 # .../chg-zd-mtx -reset
247 # The script should output a line like this:
251 # The number at the first should match $firstslot. The device name
252 # after that should be your tape device.
254 # The exit code ($? or $status) should be zero.
258 # .../chg-zd-mtx -slot next
261 # The script should output a line like this:
265 # The number at the first should be one higher than $firstslot.
266 # The device name after that should be your tape device.
268 # The exit code ($? or $status) should be zero.
272 # .../chg-zd-mtx -slot current
275 # Assuming the tape is still loaded from the previous test, the
276 # robot should not move and the script should report the same thing
277 # the previous command did.
279 # * If you continue to run "-slot next" commands, the robot should load
280 # each tape in turn then wrap back around to the first when it
281 # reaches $lasttape. If $cleanslot is within the $firstslot to
282 # $lastslot range, the script will skip over that entry.
284 # * Finally, try some of the amtape commands and make sure they work:
286 # amtape <config> reset
287 # amtape <config> slot next
288 # amtape <config> slot current
290 # * If you set $havereader non-zero, now would be a good time to create
291 # the initial barcode database:
293 # amtape <config> update
296 ################################################################################
297 # To debug this script, first look in @AMANDA_DBGDIR@. The script
298 # uses one of two log files there, depending on what version of Amanda
299 # is calling it. It may be chg-zd-mtx.YYYYMMDD*.debug, or it may be
300 # changer.debug.driveN where 'N' is the drive number.
302 # If the log file does not help, try running the script, **as the Amanda
303 # user**, in the amanda.conf directory with whatever set of args the log
304 # said were used when you had a problem. If nothing else useful shows up
305 # in the output, try running the script with the DEBUG environment variable
306 # set non-null, e.g.:
308 # env DEBUG=yes .../chg-zd-mtx ...
309 ################################################################################
311 # source utility functions and values from configure
312 exec_prefix=@exec_prefix@
313 libexecdir=@libexecdir@
314 . ${libexecdir}/chg-lib.sh
316 test -n "$DEBUG" && set -x
317 TMPDIR="@AMANDA_TMPDIR@"
318 DBGDIR="@AMANDA_DBGDIR@"
321 myname=`expr "$argv0" : '.*/\(.*\)'`
323 config=`pwd 2>/dev/null`
324 config=`expr "$config" : '.*/\(.*\)'`
327 # Functions to write a new log file entry and append more log information.
330 ds=`date '+%H:%M:%S' 2>/dev/null`
331 if [ $? -eq 0 -a -n "$ds" ]; then
332 logprefix=`echo "$ds" | sed 's/./ /g'`
338 if [ -z "$logprefix" ]; then
339 echo "$@" >> $DBGFILE
341 echo "$logprefix" "$@" >> $DBGFILE
346 if [ -z "$logprefix" ]; then
347 echo "===" "`date`" "===" >> $DBGFILE
348 echo "$@" >> $DBGFILE
350 ds=`date '+%H:%M:%S' 2>/dev/null`
351 echo "$ds" "$@" >> $DBGFILE
356 # Common exit function.
360 # $3 = additional information (error message, tape devive, etc)
365 if [ $internal_call -gt 0 ]; then
375 Log $call_type "($code) -> $exit_slot $@"
376 echo "$exit_slot" "$@"
377 if [ $call_type = Return ]; then
380 amgetconf$SUF dbclose.$argv0:$DBGFILE > /dev/null 2>&1
385 # Function to run another command and log it.
389 Log `_ 'Running: %s' "$@"`
390 rm -f $stdout $stderr
391 "$@" > $stdout 2> $stderr
393 Log `_ 'Exit code: %s' "$exitcode"`
397 cat $stdout >> $DBGFILE
402 cat $stderr >> $DBGFILE
410 # Return success if the arg is numeric.
414 test -z "$1" && return 1
415 x="`expr "$1" : '\([-0-9][0-9]*\)' 2>/dev/null`"
416 return `expr X"$1" != X"$x"`
420 # Run $MTX status unless the previous output is still valid.
425 test -n "$DEBUG" && set -x
426 if [ $mtx_status_valid -ne 0 ]; then
430 Run $MTX status > $mtx_status 2>&1
432 if [ $status -eq 0 ]; then
439 # Determine the slot currently loaded. Set $loadedslot to the slot
440 # currently loaded, or "-1", and $loadedbarcode to the corresponding
441 # barcode (or nothing).
445 test -n "$DEBUG" && set -x
449 /^Data Transfer Element:Empty/ {
453 /^Data Transfer Element '$driveslot':Empty/ {
457 /^Data Transfer Element:Full (Storage Element \([0-9][0-9]*\) Loaded):VolumeTag *= *\([^ ]*\)/ {
458 s/.*(Storage Element \([0-9][0-9]*\) Loaded):VolumeTag *= *\([^ ]*\)/\1 \2/p
461 /^Data Transfer Element '$driveslot':Full (Storage Element \([0-9][0-9]*\) Loaded):VolumeTag *= *\([^ ]*\)/ {
462 s/.*(Storage Element \([0-9][0-9]*\) Loaded):VolumeTag *= *\([^ ]*\)/\1 \2/p
465 /^Data Transfer Element '$driveslot':Full (Unknown Storage Element Loaded):VolumeTag *= *\([^ ]*\)/ {
466 s/.*:VolumeTag *= *\([^ ]*\)/-2 \1/p
469 /^Data Transfer Element:Full (Storage Element \([0-9][0-9]*\) Loaded)/ {
470 s/.*(Storage Element \([0-9][0-9]*\) Loaded).*/\1/p
473 /^Data Transfer Element '$driveslot':Full (Storage Element \([0-9][0-9]*\) Loaded)/ {
474 s/.*Storage Element \([0-9][0-9]*\) Loaded.*/\1/p
477 /^Data Transfer Element '$driveslot':Full (Unknown Storage Element Loaded)/ {
481 ' < $mtx_status 2>&1`
482 shift # get rid of the "x"
485 if [ -z "$loadedslot" ]; then
488 "could not determine current slot, are you sure your drive slot is $driveslot"
489 return $? # in case we are internal
492 #Use the current slot if it's empty and we don't know which slot is loaded'
493 if [ $loadedslot -eq -2 ]; then
496 /^.*Storage Element '$currentslot':Empty/ {
497 s/.*Storage Element \([0-9][0-9]*\):Empty/\1/p
500 /^.*Storage Element '$currentslot':Full/ {
501 s/.*Storage Element \([0-9][0-9]*\):Full/-2/p
504 /^.*Storage Element '$currentslot' IMPORT\/EXPORT:Empty/ {
505 s/.*Storage Element \([0-9][0-9]*\) IMPORT\/EXPORT:Empty/\1/p
508 /^.*Storage Element '$currentslot' IMPORT\/EXPORT:Full/ {
509 s/.*Storage Element \([0-9][0-9]*\) IMPORT\/EXPORT:Full/-2/p
513 ' < $mtx_status 2>& 1`
514 shift # get rid of the "x"
516 if [ ! -z $loadedslotx ]; then
517 loadedslot=$loadedslotx
521 #Use the first empty slot if we don't know which slot is loaded'
522 if [ $loadedslot -eq -2 ]; then
525 /^.*Storage Element \([0-9][0-9]*\):Empty/ {
526 s/.*Storage Element \([0-9][0-9]*\):Empty/\1/p
529 /^.*Storage Element \([0-9][0-9]*\) IMPORT\/EXPORT:Empty/ {
530 s/.*Storage Element \([0-9][0-9]*\) IMPORT\/EXPORT:Empty/\1/p
534 ' < $mtx_status 2>& 1`
535 shift # get rid of the "x"
539 if IsNumeric "$loadedslot" ; then
544 "currently loaded slot ($loadedslot) not numeric"
545 return $? # in case we are internal
547 Log `_ 'STATUS -> currently loaded slot = %s' "$loadedslot"`
548 LogAppend `_ ' -> currently loaded barcode = "%s"' "$loadedbarcode"`
552 # Get a list of slots between $firstslot and $lastslot, if they are set.
553 # If they are not set, set them to the first and last slot seen on the
554 # assumption the entire robot is to be used (???).
559 test -n "$DEBUG" && set -x
560 if [ -n "$slot_list" ]; then
565 /^Data Transfer Element:Full (Storage Element \([0-9][0-9]*\) Loaded)/ {
566 s/.*(Storage Element \([0-9][0-9]*\) Loaded).*/\1/p
568 /^Data Transfer Element '$driveslot':Full (Storage Element \([0-9][0-9]*\) Loaded)/ {
569 s/.*Storage Element \([0-9][0-9]*\) Loaded.*/\1/p
571 /^Data Transfer Element '$driveslot':Full (Unknown Storage Element Loaded)/ {
574 /^.*Storage Element \([0-9][0-9]*\):Full/ {
575 s/.*Storage Element \([0-9][0-9]*\):Full.*/\1/p
578 /^.*Storage Element \([0-9][0-9]*\):Empty/ {
579 s/.*Storage Element \([0-9][0-9]*\):Empty/\1/p
582 /^.*Storage Element \([0-9][0-9]*\):Full/ {
583 s/.*Storage Element \([0-9][0-9]*\):Full.*/\1/p
585 /^.*Storage Element \([0-9][0-9]*\) IMPORT\/EXPORT:Full/ {
586 s/.*Storage Element \([0-9][0-9]*\) IMPORT\/EXPORT:Full.*/\1/p
588 ' < $mtx_status 2>&1 | grep -v "^${cleanslot}\$" | sort -n`
589 slot_list=`echo $slot_list` # remove the newlines
590 if [ $firstslot -lt 0 -o $lastslot -lt 0 ]; then
592 for slot in $slot_list; do
593 if [ $firstslot -lt 0 ]; then
594 Log `_ 'SLOTLIST -> firstslot set to %s' "$slot"`
597 if [ $lastslot -lt 0 ]; then
601 if [ $lastslot -lt 0 -a $last -ge 0 ]; then
602 Log `_ 'SLOTLIST -> lastslot set to %s' "$last"`
605 if [ $firstslot -lt 0 ]; then
608 `_ 'cannot determine first slot'`
609 return $? # in case we are internal
610 elif [ $lastslot -lt 0 ]; then
613 `_ 'cannot determine last slot'`
614 return $? # in case we are internal
618 for slot in $slot_list; do
619 if [ $slot -ge $firstslot -a $slot -le $lastslot ]; then
620 amanda_slot_list="$amanda_slot_list $slot"
623 if [ -z "$amanda_slot_list" ]; then
627 return $? # in case we are internal
629 slot_list="$amanda_slot_list"
634 exec_prefix=@exec_prefix@
636 libexecdir=@libexecdir@
638 # try to hit all the possibilities here
639 PATH=$sbindir:$libexecdir:/usr/bin:/bin:/usr/sbin:/sbin:/usr/ucb:/usr/local/bin
642 USE_VERSION_SUFFIXES="@USE_VERSION_SUFFIXES@"
643 if test "$USE_VERSION_SUFFIXES" = "yes"; then
649 DBGFILE=`amgetconf$SUF dbopen.$argv0 2>/dev/null`
652 DBGFILE=/dev/null # will try this again below
655 changerfile=`amgetconf$SUF changerfile 2>/dev/null`
656 if [ -z "$changerfile" ]; then
659 "changerfile must be specified in amanda.conf"
662 tape=`amgetconf$SUF tapedev 2>/dev/null`
663 if [ -z "$tape" ]; then
666 "tapedev may not be empty"
667 elif [ $tape = "/dev/null" -o `expr "$tape" : 'null:'` -eq 5 ]; then
670 "tapedev ($tape) may not be the null device"
672 TAPE=`amgetconf$SUF changerdev 2>/dev/null`
673 if [ -z "$TAPE" ]; then
676 "changerdev may not be empty"
677 elif [ $TAPE = "/dev/null" ]; then
680 "changerdev ($TAPE) may not be the null device"
682 export TAPE # for mtx command
685 export CHANGER # for mtx command
687 #### Set up the various config files.
689 conf_match=`expr "$changerfile" : .\*\.conf\$`
690 if [ $conf_match -ge 6 ]; then
691 configfile=$changerfile
692 changerfile=`echo $changerfile | sed 's/.conf$//g'`
694 configfile=$changerfile.conf
697 if [ ! -e $configfile ]; then
700 "configuration file \"$configfile\" doesn't exist"
702 if [ ! -f $configfile ]; then
705 "configuration file \"$configfile\" is not a file"
708 cleanfile=$changerfile-clean
709 accessfile=$changerfile-access
710 slotfile=$changerfile-slot
711 labelfile=$changerfile-barcodes
712 [ ! -s $cleanfile ] && echo 0 > $cleanfile
713 [ ! -s $accessfile ] && echo 0 > $accessfile
714 [ ! -s $slotfile ] && echo -1 > $slotfile
715 [ ! -f $labelfile ] && > $labelfile
716 cleancount=`cat $cleanfile`
717 accesscount=`cat $accessfile`
719 test -z "$MT" && Exit 2 "<none>" "No mt command defined"
720 test ! -f "$MT" && Exit 2 "<none>" "mt command ($MT) doesn't exist"
721 test -z "$MTX" && Exit 2 "<none>" "No mtx command defined"
722 test ! -f "$MTX" && Exit 2 "<none>" "mtx command ($MTX) doesn't exist"
724 #### Dig out of the config file what is needed
727 varlist="$varlist firstslot"
728 varlist="$varlist lastslot"
729 varlist="$varlist cleanslot"
730 varlist="$varlist cleancycle"
731 varlist="$varlist OFFLINE_BEFORE_UNLOAD" # old name
732 varlist="$varlist offline_before_unload"
733 varlist="$varlist unloadpause"
734 varlist="$varlist AUTOCLEAN" # old name
735 varlist="$varlist autoclean"
736 varlist="$varlist autocleancount"
737 varlist="$varlist havereader"
738 varlist="$varlist driveslot"
739 varlist="$varlist poll_drive_ready"
740 varlist="$varlist initial_poll_delay"
741 varlist="$varlist max_drive_wait"
745 val="`cat $configfile 2>/dev/null | sed -n '
746 # Ignore comment lines (anything starting with a #).
748 # Find the first var=val line in the file, print the value and quit.
749 /^[ ]*'$var'[ ]*=[ ]*\([^ ][^ ]*\).*/ {
750 s/^[ ]*'$var'[ ]*=[ ]*\([^ ][^ ]*\).*/\1/p
757 # Deal with driveslot first so we can get DBGFILE set if we are still
758 # using the old amgetconf.
760 if [ -z "$driveslot" ]; then
764 # Get DBGFILE set if it is not already.
766 if [ $DBGFILE = /dev/null ]; then
767 if [ -d "$DBGDIR" ]; then
768 DBGFILE=$DBGDIR/changer.debug.drive$driveslot
772 Log `_ '=== Start %s ===' "\`date\`"`
774 if [ -z "$driveslot" ]; then
777 `_ 'cannot determine drive slot from %s' "$tape"`
780 stdout=$TMPDIR/$myname.1.$$
781 stderr=$TMPDIR/$myname.2.$$
782 mtx_status=$TMPDIR/$myname.status.$$
783 trap "rm -f $stdout $stderr $mtx_status" 0 # exit cleanup
785 Log `_ 'Using config file %s' "$configfile"`
787 # Log the argument list.
792 LogAppend "\$$i = \"$argv0\""
795 LogAppend "\$$i = \"$arg\""
798 # Set the default config values for those not in the file. Log the
799 # results and make sure each is valid (numeric).
801 firstslot=${firstslot:-'-1'} # default: mtx status
802 lastslot=${lastslot:-'-1'} # default: mtx status
803 cleanslot=${cleanslot:-'-1'} # default: -1
804 cleancycle=${cleancycle:-'120'} # default: two minutes
805 if [ -z "$offline_before_unload" -a -n "$OFFLINE_BEFORE_UNLOAD" ]; then
806 offline_before_unload=$OFFLINE_BEFORE_UNLOAD # (old name)
808 offline_before_unload=${offline_before_unload:-'0'} # default: 0
809 unloadpause=${unloadpause:-'0'} # default: 0
810 if [ -z "$autoclean" -a -n "$AUTOCLEAN" ]; then
811 autoclean=$AUTOCLEAN # (old name)
813 autoclean=${autoclean:-'0'} # default: 0
814 autocleancount=${autocleancount:-'99'} # default: 99
815 havereader=${havereader:-'0'} # default: 0
816 poll_drive_ready=${poll_drive_ready:-'3'} # default: three seconds
817 initial_poll_delay=${initial_poll_delay:-'0'} # default: zero zeconds
818 max_drive_wait=${max_drive_wait:-'120'} # default: two minutes
822 Log `_ "Config info:"`
823 for var in $varlist; do
824 if [ $var = "OFFLINE_BEFORE_UNLOAD" ]; then
826 elif [ $var = "AUTOCLEAN" ]; then
830 if [ -z "$val" ]; then
833 `_ '%s missing in %s' "$var" "$configfile"`
835 if IsNumeric "$val" ; then
840 `_ '%s (%s) not numeric in %s' "$var" "$val" "$configfile"`
842 LogAppend $var = \"$val\"
845 # Run the rest of the config file sanity checks.
847 if [ $firstslot -gt $lastslot ]; then
850 `_ 'firstslot (%s) greater than lastslot (%s) in %s' "$firstslot" "$lastslot" "$configfile"`
852 if [ $autoclean -ne 0 -a $cleanslot -lt 0 ]; then
855 `_ 'autoclean set but cleanslot not valid (%s)' "$cleanslot"`
858 # Set up the current slot
860 currentslot=`cat $slotfile`
861 if IsNumeric "$currentslot" ; then
862 if [ $currentslot -lt $firstslot ]; then
863 Log `_ 'SETUP -> current slot %s less than %s ... resetting to %s' "$currentslot" "$firstslot" "$firstslot"`
864 currentslot=$firstslot
865 elif [ $currentslot -gt $lastslot ]; then
866 Log `_ 'SETUP -> current slot %s greater than %s ... resetting to %s' "$currentslot" "$lastslot" "$lastslot"`
867 currentslot=$lastslot
870 Log `_ 'SETUP -> contents of %s (%s) invalid, setting current slot to first slot (%s)' "$slotfile" "$currentslot" "$firstslot"`
871 currentslot=$firstslot
875 first_slot_in_list=-1
876 next_slot_after_current=-1
877 for slot in $slot_list; do
878 if [ $first_slot_in_list -lt 0 ]; then
879 first_slot_in_list=$slot # in case $firstslot is missing
881 if [ $slot -eq $currentslot ]; then
884 elif [ $slot -gt $currentslot ]; then
885 next_slot_after_current=$slot # $currentslot is missing
889 if [ $found_current -eq 0 ]; then
890 if [ $next_slot_after_current -lt 0 ]; then
891 new_currentslot=$first_slot_in_list
893 new_currentslot=$next_slot_after_current
895 Log `_ 'WARNING -> current slot %s not available, setting current slot to next slot (%s)' "$currentslot" "$new_currentslot"`
896 currentslot=$new_currentslot
902 # Eject the current tape and put it away.
906 test -n "$DEBUG" && set -x
907 Log `_ 'EJECT -> ejecting tape from %s' "$tape"`
909 if [ $loadedslot -gt 0 ]; then
910 Log `_ 'EJECT -> moving tape from drive %s to storage slot %s' "$driveslot" "$loadedslot"`
911 if [ $offline_before_unload -ne 0 ]; then
912 Run $MT $MTF $tape offline > /dev/null 2>&1
915 result=`Run $MTX unload $loadedslot $driveslot 2>&1`
917 Log `_ ' -> status %s, result "%s"' "$status" "$result"`
919 if [ $status -ne 0 ]; then
927 answer=`_ 'Drive was not loaded'`
930 Exit $code "$loadedslot" "$answer"
931 return $? # in case we are internal
935 # Reset the robot back to the first slot.
939 test -n "$DEBUG" && set -x
940 Log `_ 'RESET -> loading tape from slot %s to drive %s (%s)' "$firstslot" "$driveslot" "$tape"`
941 # Call loadslot without doing it as an internal and let it finish
945 Exit 2 `_ '<none>'` `_ 'reset: should not get here'`
946 return $? # in case we are internal
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
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
962 Log `_ 'LOADSLOT -> load drive %s (%s) from slot %s' "$driveslot" "$tape" "$whichslot"`
964 numeric=`echo $whichslot | sed 's/[^0-9]//g'`
966 current|prev|next|advance)
967 find_slot=$currentslot
982 Exit 2 `_ '<none>'` `_ 'Illegal slot: "%s"' "$whichslot"`
983 return $? # in case we are internal
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.
993 set x $slot_list $slot_list $slot_list $cleanslot
994 shift # get rid of the "x"
997 while [ $# -gt 0 ]; do
998 if [ $1 -eq $find_slot ]; then
1004 if [ $# -le 0 ]; then
1007 `_ 'Cannot find slot %s in slot list (%s)' "$find_slot " "$slot_list"`
1008 return $? # in case we are internal
1011 # Determine the slot to load.
1024 # If the desired slot is already loaded, we are done. Only update
1025 # current slot if this is not the cleaning slot.
1027 if [ $loadslot = $loadedslot ]; then
1028 if [ $loadslot -ne $cleanslot ]; then
1030 echo $loadslot > $slotfile
1032 Exit 0 "$loadedslot" "$tape"
1033 return $? # in case we are internal
1035 if [ $loadedslot -eq -2 ]; then
1036 Exit 0 "$loadedslot" "$tape"
1037 return $? # in case we are internal
1040 # If we are loading the cleaning tape, bump the cleaning count
1041 # and reset the access count. Otherwise, bump the access count
1042 # and see if it is time to do a cleaning.
1043 if [ $loadslot = $cleanslot ]; then
1044 rm -f $cleanfile $accessfile
1045 expr $cleancount + 1 > $cleanfile
1046 echo 0 > $accessfile
1049 expr $accesscount + 1 > $accessfile
1050 if [ $autoclean -ne 0 -a $accesscount -gt $autocleancount ]
1052 internal_call=`expr $internal_call + 1`
1053 loadslot clean > /dev/null 2>&1
1055 internal_call=`expr $internal_call - 1`
1056 if [ $status -ne 0 ]; then
1057 Exit $status "$loadslot" "$exit_answer"
1058 return $? # in case we are internal
1061 # Slot $cleanslot might contain an ordinary tape
1062 # rather than a cleaning tape. A cleaning tape
1063 # *MIGHT* auto-eject; an ordinary tape does not.
1064 # We therefore have to read the status again to
1065 # check what actually happened.
1071 # Unload whatever tape is in the drive.
1072 internal_call=`expr $internal_call + 1`
1073 eject > /dev/null 2>&1
1075 internal_call=`expr $internal_call - 1`
1076 if [ $status -gt 1 ]; then
1077 Exit $status "$exit_slot" "$exit_answer"
1078 return $? # in case we are internal
1081 # If we were doing an "advance", we are done.
1082 if [ $whichslot = advance ]; then
1083 if [ $loadslot -ne $cleanslot ]; then
1085 echo $loadslot > $slotfile
1087 Exit 0 "$loadslot" "/dev/null"
1088 return $? # in case we are internal
1091 # Load the tape, finally!
1092 Log `_ "LOADSLOT -> loading tape from slot %s to drive %s (%s)" "$loadslot" "$driveslot" "$tape"`
1093 result=`Run $MTX load $loadslot $driveslot 2>&1`
1095 Log `_ ' -> status %s, result "%s"' "$status" "$result"`
1097 if [ $status -ne 0 ]; then
1098 Exit 2 "$loadslot" "$result"
1099 return $? # in case we are internal
1103 # Cleaning tapes never go "ready", so instead we just sit here
1104 # for "long enough" (as determined empirically by the user),
1105 # then return success.
1107 if [ $loadslot -eq $cleanslot ]; then
1108 Run sleep $cleancycle
1109 Exit 0 "$loadslot" "$tape"
1110 return $? # in case we are internal
1114 # Wait for the drive to go online.
1118 sleep $initial_poll_delay
1119 while [ $waittime -lt $max_drive_wait ]; do
1120 result=`Run $MT $MTF $tape rewind 2>&1`
1121 if [ $? -eq 0 ]; then
1125 sleep $poll_drive_ready
1126 waittime=`expr $waittime + $poll_drive_ready`
1128 if [ $ready -eq 0 ]; then
1129 Exit 2 "$loadslot" `_ 'Drive not ready after %s seconds, rewind said "%s"' "$max_drive_wait" "$result"`
1130 return $? # in case we are internal
1133 if [ $loadslot -ne $cleanslot ]; then
1135 echo $loadslot > $slotfile
1137 Exit 0 "$loadslot" "$tape"
1138 return $? # in case we are internal
1142 # Return information about how the changer is configured and the current
1143 # state of the robot.
1147 test -n "$DEBUG" && set -x
1150 Log `_ 'INFO -> first slot: %s' "$firstslot"`
1151 LogAppend `_ ' -> current slot: %s' "$currentslot"`
1152 LogAppend `_ ' -> loaded slot: %s' "$loadedslot"`
1153 LogAppend `_ ' -> last slot: %s' "$lastslot"`
1154 LogAppend `_ ' -> slot list: %s' "$slot_list"`
1155 LogAppend `_ ' -> can go backwards: 1'`
1156 LogAppend `_ ' -> havereader: %s' "$havereader"`
1159 # Check if a barcode reader is configured or not. If so, it
1160 # passes the 4th item in the echo back to amtape signifying it
1161 # can search based on barcodes.
1164 if [ $havereader -eq 1 ]; then
1168 if [ $currentslot -lt $firstslot -o $currentslot -gt $lastslot ]; then
1169 currentslot=$firstslot # what "current" will get
1171 numslots=`expr $lastslot - $firstslot + 1`
1172 Exit 0 "$currentslot" "$numslots 1 $reader"
1173 return $? # in case we are internal
1177 # Read the labelfile and scan for a particular entry.
1181 labelfile_entry_found=0
1189 while read lbl bc junk; do
1190 line=`expr $line + 1`
1191 if [ -z "$lbl" -o -z "$bc" -o -n "$junk" ]; then
1192 Log `_ 'ERROR -> Line %s malformed: %s %s %s' "$line" "$lbl" "$bc" "$junk"`
1193 LogAppend `_ ' -> Remove %s and run "%s %s update"' "$labelfile" "$sbindir/amtape" "$config"`
1196 `_ 'Line %s malformed in %s: %s %s %s' "$line" "$labelfile" "$lbl" "$bc" "$junk"`
1197 return $? # in case we are internal
1199 if [ $lbl = "$lbl_search" -o $bc = "$bc_search" ]; then
1200 if [ $labelfile_entry_found -ne 0 ]; then
1201 Log `_ 'ERROR -> Duplicate entries: %s line %s' "$labelfile" "$line"`
1202 LogAppend `_ ' -> Remove $labelfile and run "$sbindir/amtape $config update"' "$labelfile" "$sbindir/amtape" "$config"`
1205 `_ 'Duplicate entries: %s line %s' "$labelfile" "$line"`
1206 return $? # in case we are internal
1208 labelfile_entry_found=1
1209 labelfile_label=$lbl
1210 labelfile_barcode=$bc
1216 # Adds the label and barcode for the currently loaded tape to the
1217 # barcode file. Return an error if the database is messed up.
1221 test -n "$DEBUG" && set -x
1222 if [ $# -lt 1 ]; then
1223 Exit 2 `_ '<none>'` `_ 'Missing -label argument'`
1224 return $? # in case we are internal
1227 if [ $havereader -eq 0 ]; then
1228 Exit 2 `_ '<none>'` `_ 'Not configured with barcode reader'`
1229 return $? # in case we are internal
1232 if [ $loadedslot -lt 0 ]; then
1233 Exit 1 `_ '<none>'` `_ 'No tape currently loaded'`
1234 return $? # in case we are internal
1236 Log `_ 'LABEL -> Adding label "%s" with barcode "%s" for slot %s into %s' "$tapelabel" "$loadedbarcode" "$loadedslot" "$labelfile"`
1237 read_labelfile "$tapelabel" "$loadedbarcode" < $labelfile
1238 if [ $labelfile_entry_found -ne 0 ]; then
1240 if [ "$labelfile_barcode" != "$loadedbarcode" ]; then
1244 old_val=$labelfile_barcode
1245 new_val=$loadedbarcode
1246 elif [ "$labelfile_label" != "$tapelabel" ]; then
1248 lf_val=$loadedbarcode
1250 old_val=$labelfile_label
1253 if [ -n "$lf_val" ]; then
1254 LogAppend `_ 'ERROR -> !!! Label database corrupted !!!'`
1255 LogAppend `_ ' -> "%s" conflicts with new %s "%s" for %s "%s"' "$old_val" "$val_type" "$new_val" "$lf_type" "$lf_val"`
1258 `_ '%s: "%s" conflicts with new %s "%s" for %s "%s"' "$tapelabel" "$old_val" "$val_type" "$new_val" "$lf_type" "$lf_val"`
1259 return $? # in case we are internal
1261 LogAppend `_ " -> already synced"`
1263 echo "$tapelabel $loadedbarcode" >> $labelfile
1264 LogAppend `_ ' -> appended %s entry: %s %s' "$labelfile" "$tapelabel" "$loadedbarcode"`
1266 Exit 0 "$loadedslot" "$tape"
1267 return $? # in case we are internal
1271 # Look for a label in the barcode file. If found, locate the slot it's
1272 # in by looking for the barcode in the mtx output, then load that tape.
1276 test -n "$DEBUG" && set -x
1277 if [ $# -lt 1 ]; then
1278 Exit 2 `_ '<none>'` `_ 'Missing -search argument'`
1279 return $? # in case we are internal
1282 if [ $havereader -eq 0 ]; then
1283 Exit 2 `_ '<none>'` `_ 'Not configured with barcode reader'`
1284 return $? # in case we are internal
1286 Log `_ 'SEARCH -> Hunting for label "%s"' "$tapelabel"`
1287 read_labelfile "$tapelabel" "" < $labelfile
1288 if [ $labelfile_entry_found -eq 0 ]; then
1289 LogAppend `_ ' -> !!! label "%s" not found in %s !!!' "$tapelabel" "$labelfile"`
1290 LogAppend `_ ' -> Remove $labelfile and run "%s %s update"' "$labelfile" "$sbindir/amtape" "$config"`
1293 `_ '%s: label "%s" not found in %s' "$tapelabel" "$tapelabel" "$labelfile"`
1294 return $? # in case we are internal
1296 LogAppend `_ ' -> barcode is "%s"' "$labelfile_barcode"`
1299 /VolumeTag *= *'$labelfile_barcode' *$/ {
1300 s/.*Storage Element \([0-9][0-9]*\).*/\1/p
1304 LogAppend `_ ' -> foundslot is %s' "$foundslot"`
1305 if [ -z "$foundslot" ]; then
1306 LogAppend `_ 'ERROR -> !!! Could not find slot for barcode "%s"!!!' "$labelfile_barcode"`
1307 LogAppend `_ ' -> Remove %s and run "%s %s update"' "$labelfile" "$sbindir/amtape" "$config"`
1310 `_ 'barcode "%s" not found in mtx status output' "$labelfile_barcode"`
1311 return $? # in case we are internal
1313 # Call loadslot without doing it as an internal and let it finish
1317 Exit 2 `_ '<none>'` `_ 'searchtape: should not get here'`
1318 return $? # in case we are internal
1322 # Program invocation begins here
1325 if [ $# -lt 1 ]; then
1326 Exit 2 `_ '<none>'` `_ 'Usage: %s -command args' "$myname"`
1353 Exit 2 `_ '<none>'` `_ 'unknown option: %s' "$cmd"`
1357 Exit 2 `_ '<none>'` `_ '%s: should not get here' "$myname"`