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