6dcfaa9d273132d20bb1eb437e53e29ea1824c04
[debian/amanda] / changer-src / chg-manual.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
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 EGREP='@EGREP@'
34
35 if [ -x $sbindir/ammt$SUF ]; then
36         MT=$sbindir/ammt$SUF
37         MTF=-f
38 elif [ -x "@MT@" ]; then
39         MT=@MT@
40         MTF=@MT_FILE_FLAG@
41 else
42         answer="<none> $myname: mt program not found"
43         code=1
44         echo "Exit -> $answer" >> $logfile
45         echo "$answer"
46         exit $code
47 fi
48 echo MT "->" $MT $MTF >> $logfile
49
50 if [ -x $sbindir/amdd$SUF ]; then
51         DD=$sbindir/amdd$SUF
52 elif [ -x "@DD@" ]; then
53         DD=@DD@
54 else
55         answer="<none> $myname: dd program not found"
56         code=1
57         echo "Exit -> $answer" >> $logfile
58         echo "$answer"
59         exit $code
60 fi
61 echo DD "->" $DD >> $logfile
62
63 MAILER=@MAILER@
64 ONLINEREGEX="ONLINE|READY|sense[_ ]key[(]0x0[)]|sense key error = 0|^er=0$|, mt_erreg: 0x0|^Current Driver State: at rest$"
65 REPORTTO=`amgetconf$SUF mailto`
66 tape=`amgetconf$SUF tapedev`
67 ORG=`amgetconf$SUF ORG`
68
69 firstslot=1
70 lastslot=99
71
72 changerfile=`amgetconf$SUF changerfile`
73
74 cleanfile=$changerfile-clean
75 accessfile=$changerfile-access
76 slotfile=$changerfile-slot
77 [ ! -f $cleanfile ] && echo 0 > $cleanfile
78 [ ! -f $accessfile ] && echo 0 > $accessfile
79 [ ! -f $slotfile ] && echo $firstslot > $slotfile
80 cleancount=`cat $cleanfile`
81 accesscount=`cat $accessfile`
82 slot=`cat $slotfile`
83
84 request() {
85         echo "insert tape into slot $1 and press return" >/dev/tty
86         read ANSWER </dev/tty
87 }
88
89 ###
90 # If $changerfile exists, source it into this script.  One reason is to
91 # override the request() function above which gets called to request
92 # that a tape be mounted.  Here is an alternate versions of request()
93 # that does things more asynchronous:
94 #
95 #  request() {
96 #       # Send E-mail about the mount request and wait for the drive
97 #       # to go ready by checking the status once a minute.  Repeat
98 #       # the E-mail once an hour in case it gets lost.
99 #       timeout=0
100 #       while true;do
101 #           if [ $timeout -le 0 ]; then
102 #               echo "insert Amanda tape into slot $1 ($tape)" \
103 #                 | $MAILER -s "$ORG AMANDA TAPE MOUNT REQUEST FOR SLOT $1" \
104 #                         $REPORTTO
105 #               timeout=`expr 60 \* 60`
106 #           fi
107 #           echo "     -> rewind $tape" >> $logfile
108 #           $MT $MTF $tape rewind >> $logfile 2>&1
109 #           echo "     -> status $tape" >> $logfile
110 #           used=`$MT $MTF $tape status 2>&1 | tee -a $logfile | $EGREP "$ONLINEREGEX"`
111 #           echo "     -> loaded <$used>" >> $logfile
112 #           if [ ! -z "$used" ];then
113 #               break
114 #           fi
115 #           sleep 60
116 #           timeout=`expr $timeout - 60`
117 #       done
118 #  }
119 #
120 # Instead of sending mail, you might write the message to /dev/console
121 # or use "logger" to send it via syslog, etc.
122 ###
123
124 if [ -f $changerfile ]; then
125         . $changerfile
126 fi
127
128 #
129
130 eject() { 
131         echo "     -> rewind $tape" >> $logfile
132         $MT $MTF $tape rewind >> $logfile 2>&1
133         echo "     -> status $tape" >> $logfile
134         used=`$MT $MTF $tape status 2>&1 | tee -a $logfile | $EGREP "$ONLINEREGEX"`
135         echo "     -> loaded <$used>" >> $logfile
136         if [ ! -z "$used" ];then
137                 echo "     -> offline $tape" >> $logfile
138                 $MT $MTF $tape offline >> $logfile 2>&1
139                 echo 0 > $slotfile
140                 answer="$slot $tape"
141                 code=0
142         else
143                 answer="<none> $myname: Drive was not loaded"
144                 code=1
145         fi
146         echo "Exit -> $answer" >> $logfile
147         echo "$answer"
148         exit $code
149 }
150
151 #
152
153 reset() {
154         echo "     -> rewind $tape" >> $logfile
155         $MT $MTF $tape rewind >> $logfile 2>&1
156         echo "     -> status $tape" >> $logfile
157         used=`$MT $MTF $tape status 2>&1 | tee -a $logfile | $EGREP "$ONLINEREGEX"`
158         echo "     -> loaded <$used>" >> $logfile
159         if [ ! -z "$used" ];then
160                 answer="$slot $tape"
161         else
162                 answer="0 $tape"
163         fi
164         echo "Exit -> $answer" >> $logfile
165         echo "$answer"
166         exit 0
167 }
168
169 # load #
170
171 loadslot() {
172         echo "     -> rewind $tape" >> $logfile
173         $MT $MTF $tape rewind >> $logfile 2>&1
174         echo "     -> status $tape" >> $logfile
175         used=`$MT $MTF $tape status 2>&1 | tee -a $logfile | $EGREP "$ONLINEREGEX"`
176         echo "     -> loaded <$used>" >> $logfile
177         whichslot=$1
178         case $whichslot in
179         current)
180                 load=$slot
181                 [ $load -eq 0 ] && load=$firstslot
182                 [ $load -gt $lastslot ] && load=$firstslot
183                 [ $load -lt $firstslot ] && load=$lastslot
184                 ;;
185         next|advance)
186                 load=`expr $slot + 1`
187                 [ $load -gt $lastslot ] && load=$firstslot
188                 ;;
189         prev)
190                 load=`expr $slot - 1`
191                 [ $load -lt $firstslot ] && load=$lastslot
192                 ;;
193         first)
194                 load=$firstslot
195                 ;;
196         last)
197                 load=$lastslot
198                 ;;
199         [0-9]|[0-9][0-9])
200                 if [ $1 -lt $firstslot -o $1 -gt $lastslot ]; then
201                         answer="<none> $myname: slot must be $firstslot .. $lastslot"
202                         echo "Exit -> $answer" >> $logfile
203                         echo "$answer"
204                         exit 1
205                 fi
206                 load=$1
207                 ;;
208         *)
209                 answer="<none> $myname: illegal slot: $1"
210                 echo "Exit -> $answer" >> $logfile
211                 echo "$answer"
212                 exit 1
213                 ;;
214         esac
215         #
216         if [ ! -z "$used" -a $load = $slot ];then
217                 # already loaded
218                 answer="$slot $tape"
219                 echo "Exit -> $answer" >> $logfile
220                 echo "$answer"
221                 exit 0
222         fi
223
224         # if [ $load = $ecleanslot ]; then
225         # expr $cleancount + 1 > $cleanfile
226         # echo 0 > $accessfile
227         # else
228         expr $accesscount + 1 > $accessfile
229         # if [ $accesscount -gt 9 ]; then
230         # $myname -slot $cleanslot >/dev/null
231         # used=0
232         # fi
233         # fi    
234
235         #
236         if [ ! -z "$used" ]; then
237                 echo "     -> offline $tape" >> $logfile
238                 $MT $MTF $tape offline >> $logfile 2>&1
239                 used=""
240         fi
241         if [ $whichslot = advance ]; then
242                 tape=/dev/null
243         else
244                 echo "     -> load   $load" >> $logfile
245                 while [ -z "$used" ]; do
246                         request $load
247                         echo "     -> rewind $tape" >> $logfile
248                         $MT $MTF $tape rewind >> $logfile 2>&1
249                         echo "     -> status $tape" >> $logfile
250                         used=`$MT $MTF $tape status 2>&1 | tee -a $logfile | $EGREP "$ONLINEREGEX"`
251                         echo "     -> loaded <$used>" >> $logfile
252                 done
253                 $DD if=$tape bs=32k count=1 >> $logfile 2>&1
254         fi
255         echo $load > $slotfile
256         answer="$load $tape"
257         echo "Exit -> $answer" >> $logfile
258         echo "$answer"
259         exit 0
260 }
261
262 #
263
264 info() {
265         echo "     -> rewind $tape" >> $logfile
266         $MT $MTF $tape rewind >> $logfile 2>&1
267         echo "     -> status $tape" >> $logfile
268         used=`$MT $MTF $tape status 2>&1 | tee -a $logfile | $EGREP "$ONLINEREGEX"`
269         echo "     -> loaded <$used>" >> $logfile
270         if [ -z "$used" ];then
271                 answer="0 $lastslot 1"
272         else
273                 answer="$slot $lastslot 1"
274         fi
275         echo "Exit -> $answer" >> $logfile
276         echo "$answer"
277         exit 0
278 }
279
280 #
281 # main part
282 #
283
284 echo Args "->" "$@" >> $logfile
285 while [ $# -ge 1 ];do
286         case $1 in
287         -slot)
288                 shift
289                 loadslot $*
290                 ;;
291         -info)
292                 shift
293                 info
294                 ;;
295         -reset)
296                 shift
297                 reset
298                 ;;
299         -eject)
300                 shift
301                 eject
302                 ;;
303         *)
304                 echo "<none> $myname: Unknown option $1"
305                 exit 2
306                 ;;
307         esac
308 done
309
310 exit 0