Imported Upstream version 3.1.0
[debian/amanda] / changer-src / chg-mtx.sh
1 #!@SHELL@ 
2 #
3 # Exit Status:
4 # 0 Alles Ok
5 # 1 Illegal Request
6 # 2 Fatal Error
7 #
8
9 # source utility functions and values from configure
10 prefix=@prefix@
11 exec_prefix=@exec_prefix@
12 amlibexecdir=@amlibexecdir@
13 . ${amlibexecdir}/chg-lib.sh
14
15 if [ -d "@AMANDA_DBGDIR@" ]; then
16         logfile=@AMANDA_DBGDIR@/changer.debug
17 else
18         logfile=/dev/null
19 fi
20
21 myname=$0
22
23 tape=`amgetconf tapedev`
24 if [ -z "$tape" ]; then
25   echo "<none> tapedev not specified in amanda.conf";
26   exit 2;
27 fi
28
29 TAPE=`amgetconf changerdev`; export TAPE # for mtx command
30 if [ -z "$TAPE" ]; then
31   echo "<none> changerdev not specified in amanda.conf";
32   exit 2;
33 fi
34
35 if [ "$tape" = "/dev/null" -o "$TAPE" = "/dev/null" ]; then
36   echo "<none> Both tapedev and changerdev must be specified in config file";
37   exit 2;
38 fi
39
40 firstslot=1
41 lastslot=5
42 # counted from 1 !!!
43 cleanslot=6
44
45 changerfile=`amgetconf changerfile`
46
47 cleanfile=$changerfile-clean
48 accessfile=$changerfile-access
49 [ ! -f $cleanfile ] && echo 0 > $cleanfile
50 [ ! -f $accessfile ] && echo 0 > $accessfile
51 cleancount=`cat $cleanfile`
52 accesscount=`cat $accessfile`
53 #
54
55 readstatus() {
56   used=`$MTX -s |
57     sed -n 's/Drive: No tape Loaded/-1/p;s/Drive: tape \(.\) loaded/\1/p'`
58
59   if [ -z "$used" ]; then
60     used="-1";
61   fi
62 }
63
64
65 eject() {
66   readstatus 
67   if [ $used -gt 0 ];then
68     $MTX -u $used
69     answer="0 $tape"
70     echo `_ 'Exit ->'` $answer >> $logfile
71     echo $answer
72     exit 0
73   else
74     answer=`_ '<none> %s: Drive was not loaded' "$myname"`      
75     echo `_ 'Exit ->'` $answer >> $logfile
76     echo $answer
77     exit 1
78   fi
79 }
80
81 reset() {
82   readstatus
83   if [ $used -gt 0 ];then
84     $MTX -u $used
85   fi
86   res=`$MTX -l 1`
87   if [ $? -eq 0 ];then
88     answer="1 $tape"
89     echo `_ 'Exit ->'` $answer >> $logfile
90     echo $answer
91     exit 0
92   else
93     answer="1 $res"
94     echo `_ 'Exit ->'` $answer >> $logfile
95     echo $answer
96     exit 1
97   fi
98 }
99 #
100 #
101 loadslot() {
102   readstatus
103   echo "     -> loaded $used" >> $logfile
104   whichslot=$1
105   case $whichslot in
106     current)
107              if [ $used -lt 0 ];then
108                $MTX -l 1
109                used=1
110              fi
111              answer="$used $tape"
112              echo `_ 'Exit ->'` $answer >> $logfile
113              echo $answer
114              exit 0
115              ;;
116     next|advance)
117           load=`expr $used + 1`
118           [ $load -gt $lastslot ] && load=$firstslot
119           ;;
120     prev)
121           load=`expr $used - 1`
122           [ $load -lt $firstslot ] && load=$lastslot
123           ;;
124     first)
125           load=$firstslot
126           ;;
127     last)
128           load=$lastslot
129           ;;
130     [$firstslot-$lastslot])
131           load=$1
132           ;;
133     clean)
134           load=$cleanslot
135           ;;
136     *)
137        answer=`_ '<none> %s: illegal request: "%s"' "$myname" "$whichslot"`
138        echo `_ 'Exit ->'` $answer >> $logfile
139        echo $answer
140        exit 1
141        ;;
142     esac
143
144     if [ $load = $used ]; then
145         answer="$used $tape"
146         echo `_ 'Exit ->'` $answer >> $logfile
147         echo $answer
148         exit 0
149     fi
150
151     if [ $load = $cleanslot ]; then
152         expr $cleancount + 1 > $cleanfile
153         echo 0 > $accessfile
154     else
155         expr $accesscount + 1 > $accessfile
156         if [ $accesscount -gt 9 ]; then
157                 $myname -slot clean >/dev/null
158         fi
159     fi
160
161     # Slot 6 might contain an ordinary tape rather than a cleaning
162     # tape. A cleaning tape auto-ejects; an ordinary tape does not.
163     # We therefore have to read the status again to check what
164     # actually happened.
165     readstatus
166         
167
168     if [ $used -gt 0 ];then
169       echo "     -> unload $used" >> $logfile
170       res=`$MTX -u $used`
171       status=$?
172       echo "     -> status $status" >> $logfile
173       echo "     -> res    $res" >> $logfile
174       if [ $status -ne 0 ];then
175         answer=`_ '<none> %s: %s' "$myname" "$res"`
176         echo `_ 'Exit ->'` $answer >> $logfile
177         echo $answer
178         exit 2
179       fi
180     fi
181     if [ $whichslot = advance ];then
182       answer="$load /dev/null"
183       echo `_ 'Exit ->'` $answer >> $logfile
184       echo $answer
185       exit 0
186     fi
187     echo `_ '     -> load   %s' "$load"` >> $logfile
188     res=`$MTX -l $load`
189     status=$?
190     echo `_ '     -> status %s' "$status"` >> $logfile
191     echo `_ '     -> result %s' "$res"` >> $logfile
192     if [ $status -eq 0 ];then
193       amdevcheck_status $tape
194       answer="$load $tape"
195       code=0
196     else
197       answer="$load $res"
198       code=2
199     fi
200     echo `_ 'Exit ->'` $answer >> $logfile
201     echo $answer
202     exit $code
203 }
204 #
205 info() {
206   readstatus
207   echo "     -> info   $used" >> $logfile
208   if [ $used -lt 0 ];then
209     used=0
210   fi
211   answer="$used $lastslot 1"
212   echo `_ 'Exit ->'` $answer >> $logfile
213   echo $answer
214   exit 0
215 }
216 #
217 echo Args "->" "$@" >> $logfile
218 while [ $# -ge 1 ];do
219   case $1 in
220     -slot)
221            shift
222            loadslot $*
223            ;;
224     -info)
225            shift
226            info
227            ;;
228     -reset)
229             shift
230             reset
231             ;;
232     -eject)
233             shift
234             eject
235             ;;
236     *)
237        answer=`_ '<none> %s: Unknown option %s' "$myname" "$1"`
238        echo `_ 'Exit ->'` $answer >> $logfile
239        echo $answer
240        exit 2
241        ;;
242   esac
243 done