> Someone was going to release an updated mtx script on the list...did I
> miss it?
 
Probably. Here's the latest.

-- 
Joe Rhett                                         Chief Technology Officer
[EMAIL PROTECTED]                                      ISite Services, Inc.

PGP keys and contact information:          http://www.noc.isite.net/Staff/
#!/bin/sh 
#
# Exit Status:
# 0 Alles Ok
# 1 Illegal Request
# 2 Fatal Error
#
# Contributed by Eric DOUTRELEAU <[EMAIL PROTECTED]>
# This is supposed to work with Zubkoff/Dandelion version of mtx
#
# Modified by Joe Rhett <[EMAIL PROTECTED]>
# to work with MTX 1.2.9 by Eric Lee Green http://mtx.sourceforge.net
#

# You may need to customize these things
MT=/usr/bin/mt
MTF=-f
MTX=/opt/amanda/bin/mtx
DD=/usr/bin/dd
firstslot=1
lastslot=7
cleanslot=8
accessbeforeclean=119
# No user-level customized required beyond this point.

# try to hit all the possibilities here
prefix=/opt/amanda
exec_prefix=${prefix}
sbindir=${exec_prefix}/sbin
libexecdir=${exec_prefix}/libexec
 
PATH=$sbindir:$libexecdir:/usr/bin:/bin:/usr/sbin:/sbin:/usr/ucb:/usr/local/bin
export PATH

if [ -d "/tmp/amanda" ]; then
        DBGFILE=/tmp/amanda/changer.debug
else
        DBGFILE=/dev/null
fi

USE_VERSION_SUFFIXES="no"
if test "$USE_VERSION_SUFFIXES" = "yes"; then
        SUF="-2.4.2b2"
else
        SUF=
fi

myname=$0
tape=`amgetconf$SUF tapedev`
TAPE=`amgetconf$SUF changerdev`; export TAPE # for mtx command
if [ "$tape" = "/dev/null" -o "$TAPE" = "/dev/null" ]; then
        echo "Both tapedev and changerdev must be specified in config file";
        exit 2;
fi

changerfile=`amgetconf$SUF changerfile`

cleanfile=$changerfile-clean
accessfile=$changerfile-access
slotfile=$changerfile-slot
[ ! -f $cleanfile ] && echo 0 > $cleanfile
[ ! -f $accessfile ] && echo 0 > $accessfile
[ ! -f $slotfile ] && echo 0 > $slotfile
cleancount=`cat $cleanfile`
accesscount=`cat $accessfile`

# Routines start here
readstatus() {
        usedslot=`$MTX status |
                sed -n 's/Data Transfer Element 0:Empty/-1/p;s/Data Transfer Element 
0:Full (Storage Element \(.\) Loaded)/\1/p'`

        if [ "$usedslot" -eq "-1" ]; then
                echo '-1' > $slotfile
        fi
        echo "STATUS -> currently loaded slot = $usedslot" >> $DBGFILE
}


eject() {
        readstatus 
        echo "EJECT -> ejecting tape from slot $usedslot" >> $DBGFILE
        if [ $usedslot -gt 0 ]; then
                $MTX unload $usedslot 2>/dev/null
                echo "0 $tape"
                exit 0
        else
                echo "0 Drive was not loaded"
                exit 1
        fi
}

reset() {
        readstatus
        if [ $usedslot -gt 0 ]; then
                echo "RESET -> ejecting tape from slot $usedslot" >> $DBGFILE
                $MTX unload $usedslot 2>/dev/null
        fi

        echo "RESET -> loading tape from slot 1" >> $DBGFILE
        result=`$MTX load 1 2>&1`
        if [ $? -eq 0 ]; then
                echo "1 $tape"
                exit 0
        else
                echo "1 $result"
                exit 1
        fi
}

loadslot() {
        readstatus

        whichslot=$1
        echo "LOADSLOT -> load tape from slot $whichslot" >> $DBGFILE
        case $whichslot in
                current)
                        if [ $usedslot -lt 0 ]; then
                                loadslot=$firstslot
                        else 
                                echo "$usedslot $tape"
                                exit 0
                        fi
                        ;;
                next|advance)
                        if [ $usedslot -lt 0 ]; then
                                loadslot=$firstslot
                        else
                                loadslot=`expr $usedslot + 1`
                                if [ $loadslot -gt $lastslot ]; then
                                        loadslot=$firstslot
                                fi
                        fi
                        ;;
                prev)
                        loadslot=`expr $usedslot - 1`
                        if [ $loadslot -lt $firstslot ]; then
                                loadslot=$lastslot
                        fi
                        ;;
                first)
                        loadslot=$firstslot
                        ;;
                last)
                        loadslot=$lastslot
                        ;;
                [${firstslot}-${lastslot}])
                        loadslot=$1
                        ;;
                clean)
                        loadslot=$cleanslot
                        ;;
                *)
                        echo "0 illegal request"
                        exit 1
                        ;;
        esac

        # Is this already the current slot?
        if [ $loadslot = $usedslot ]; then
                echo "$usedslot $tape"
                exit 0
        fi

        # Is this a cleaning request?
        if [ $loadslot = $cleanslot ]; then
                expr $cleancount + 1 > $cleanfile
                echo 0 > $accessfile
        else
                expr $accesscount + 1 > $accessfile
                if [ $accesscount -gt $accessbeforeclean ]; then
                        $myname -slot clean >/dev/null

                        # Slot $cleanslot might contain an ordinary tape rather than a 
cleaning
                        # tape. A cleaning tape *MIGHT* auto-eject; an ordinary tape 
does not.
                        # We therefore have to read the status again to check what 
actually happened.
                        readstatus
                fi
        fi

        # Unload any previous tape
        if [ $usedslot -ne "-1" ]; then
                echo "         -> unload $usedslot" >> $DBGFILE
                result=`$MTX unload $usedslot 2>&1`
                status=$?
                echo "         -> status $status, result '$result'" >> $DBGFILE
                if [ $status -ne 0 ]; then
                        echo "$loadslot $result"
                        exit 2
                fi
        fi

        # Load the tape, finally!
        echo "         -> loading tape from slot $loadslot" >> $DBGFILE
        result=`$MTX load $loadslot 2>&1`
        status=$?
        echo "         -> status $status, result '$result'" >> $DBGFILE

        # If there is an error, abort unless the slot is simply empty
        if [ $status -ne 0 ]; then
                empty=`echo $result | grep "is Empty"`
                if [ -z "$empty" ]; then
                        echo "$loadslot $result"
                        exit 2
                else
                        loadslot next
                fi
        else
                # Don't assume the drive is ready until we stop getting tape offline
                readyError="offline"
                while [ -n "$readyError" ]; do
                        readyStatus=`$MT $MTF $tape status 2>&1`
                        echo "         -> readyStatus = $readyStatus" >> $DBGFILE
                        readyError=`echo $readyStatus | grep "offline"`
                done

                # Now rewind and check
                echo "         -> rewind $loadslot" >> $DBGFILE
                $MT $MTF $tape rewind
                echo "$loadslot" > $slotfile
                echo "$loadslot $tape"
                exit 0
        fi
}

info() {
        readstatus
        echo "INFO -> current slot $usedslot, last slot $lastslot, can go backwards 1" 
>> $DBGFILE
        if [ $usedslot -lt 0 ]; then
                echo "0 $lastslot 1"
        else
                echo "$usedslot $lastslot 1"
        fi
        exit 0
}

# Program invocation begins here
echo "\n`date` Invoked with args '$@'" >> $DBGFILE
while [ $# -ge 1 ];do
        case $1 in
                -slot)
                        shift
                        loadslot $*
                        ;;
                -info)
                        shift
                        info
                        ;;
                -reset)
                        shift
                        reset
                        ;;
                -eject)
                        shift
                        eject
                        ;;
                *)
                        echo "Unknown option $1"
                        exit 2
                        ;;
        esac
done

Reply via email to