Imported Upstream version 2.5.2p1
[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 #
19 #      Changer config file (changerfile)
20 #
21 #      resend_mail=900         # 15 minutes
22 #      timeout_mail=604800     # 7 days
23 #      request="tty"           # Use the tty to ask the user to change tape.
24 #                              # Can't be use by cron
25 #      request="email"         # Send an email to ask the user to change tape.
26 #      request="tty_email"     # Use the tty if it exist or send an email.
27 #                      #Default is "tty_email"
28 #
29 #
30
31 if [ -d "@AMANDA_DBGDIR@" ]; then
32         logfile=@AMANDA_DBGDIR@/changer.debug
33 else
34         logfile=/dev/null
35 fi
36
37 USE_VERSION_SUFFIXES="@USE_VERSION_SUFFIXES@"
38 if test "$USE_VERSION_SUFFIXES" = "yes"; then
39         SUF="-@VERSION@"
40 else
41         SUF=
42 fi
43
44 myname=$0
45
46 EGREP='@EGREP@'
47
48 if [ -x $sbindir/ammt$SUF ]; then
49         MT=$sbindir/ammt$SUF
50         MTF=-f
51 elif [ -x "@MT@" ]; then
52         MT=@MT@
53         MTF=@MT_FILE_FLAG@
54 else
55         answer="<none> $myname: mt program not found"
56         code=1
57         echo "Exit -> $answer" >> $logfile
58         echo "$answer"
59         exit $code
60 fi
61 echo MT "->" $MT $MTF >> $logfile
62
63 if [ -x $sbindir/amdd$SUF ]; then
64         DD=$sbindir/amdd$SUF
65 elif [ -x "@DD@" ]; then
66         DD=@DD@
67 else
68         answer="<none> $myname: dd program not found"
69         code=1
70         echo "Exit -> $answer" >> $logfile
71         echo "$answer"
72         exit $code
73 fi
74 echo DD "->" $DD >> $logfile
75
76 MAILER=@MAILER@
77 ONLINEREGEX="ONLINE|READY|sense[_ ]key[(]0x0[)]|sense key error = 0|^er=0$|, mt_erreg: 0x0|^Current Driver State: at rest$"
78 REPORTTO=`amgetconf$SUF mailto`
79 tape=`amgetconf$SUF tapedev`
80
81 if [ -z "$tape" ]; then
82   echo "<none> tapedev not specified in amanda.conf."
83   exit 2
84 fi
85
86 ORG=`amgetconf$SUF ORG`
87
88 firstslot=1
89 lastslot=99
90 resend_mail=900         # 15 minutes
91 timeout_mail=604800     # 7 days
92
93 changerfile=`amgetconf$SUF changerfile`
94
95 conf_match=`expr "$changerfile" : .\*\.conf\$`
96 if [ $conf_match -ge 6 ]; then
97         configfile=$changerfile
98         changerfile=`echo $changerfile | sed 's/.conf$//g'`
99 else
100         configfile=$changerfile.conf
101 fi
102
103 cleanfile=$changerfile-clean
104 accessfile=$changerfile-access
105 slotfile=$changerfile-slot
106 [ ! -f $cleanfile ] && echo 0 > $cleanfile
107 [ ! -f $accessfile ] && echo 0 > $accessfile
108 [ ! -f $slotfile ] && echo $firstslot > $slotfile
109 cleancount=`cat $cleanfile`
110 accesscount=`cat $accessfile`
111 slot=`cat $slotfile`
112
113 request_tty() {
114         if > /dev/tty; then
115                 echo -n "Insert tape into slot $1 and press return" > /dev/tty
116                 read ANSWER < /dev/tty
117         else
118                 echo "no /dev/tty to ask to change tape"
119                 exit 1
120         fi
121 }
122
123
124 ###
125 # If $changerfile exists, source it into this script.  One reason is to
126 # override the request() function above which gets called to request
127 # that a tape be mounted.  Here is an alternate versions of request()
128 # that does things more asynchronous:
129 #
130 request_email() {
131         # Send E-mail about the mount request and wait for the drive
132         # to go ready by checking the status once a minute.  Repeat
133         # the E-mail once an hour in case it gets lost.
134         timeout=0
135         gtimeout=$timeout_mail
136         while true;do
137                 if [ $gtimeout -le 0 ]; then
138                         echo "timeout waiting for tape online"
139                         exit 1;
140                 fi
141                 if [ $timeout -le 0 ]; then
142                         msg="insert Amanda tape into slot $1 ($tape)"
143                         subject="$ORG AMANDA TAPE MOUNT REQUEST FOR SLOT $1"
144                         echo "$msg" | $MAILER -s "$subject" $REPORTTO
145                         timeout=$resend_mail
146                 fi
147                 echo "     -> rewind $tape" >> $logfile
148                 $MT $MTF $tape rewind >> $logfile 2>&1
149                 echo "     -> status $tape" >> $logfile
150                 used=`$MT $MTF $tape status 2>&1 | tee -a $logfile | $EGREP "$ONLINEREGEX"`
151                 echo "     -> loaded <$used>" >> $logfile
152                 if [ ! -z "$used" ];then
153                 break
154                 fi
155                 sleep 60
156                 timeout=`expr $timeout - 60`
157                 gtimeout=`expr $gtimeout - 60`
158         done
159 }
160 request_tty_email() {
161         if > /dev/tty; then
162                 echo -n "Insert tape into slot $1 and press return" > /dev/tty
163                 read ANSWER < /dev/tty
164         else
165                 request_email "$1"
166         fi
167 }
168
169 request() {
170         if [ X"$request" = X"tty" ]; then
171                 request_tty "$1"
172         else if [ X"$request" = X"email" ]; then
173                 request_email "$1"
174         else
175                 request_tty_email "$1"
176         fi
177         fi
178 }
179
180 if [ -f $configfile ]; then
181         . $configfile
182 fi
183
184 #
185
186 eject() { 
187         echo "     -> rewind $tape" >> $logfile
188         $MT $MTF $tape rewind >> $logfile 2>&1
189         echo "     -> status $tape" >> $logfile
190         used=`$MT $MTF $tape status 2>&1 | tee -a $logfile | $EGREP "$ONLINEREGEX"`
191         echo "     -> loaded <$used>" >> $logfile
192         if [ ! -z "$used" ];then
193                 echo "     -> offline $tape" >> $logfile
194                 $MT $MTF $tape offline >> $logfile 2>&1
195                 answer="$slot $tape"
196                 code=0
197         else
198                 answer="<none> $myname: Drive was not loaded"
199                 code=1
200         fi
201         echo "Exit -> $answer" >> $logfile
202         echo "$answer"
203         exit $code
204 }
205
206 #
207
208 reset() {
209         echo "     -> rewind $tape" >> $logfile
210         $MT $MTF $tape rewind >> $logfile 2>&1
211         echo "     -> status $tape" >> $logfile
212         used=`$MT $MTF $tape status 2>&1 | tee -a $logfile | $EGREP "$ONLINEREGEX"`
213         echo "     -> loaded <$used>" >> $logfile
214         if [ ! -z "$used" ];then
215                 answer="$slot $tape"
216         else
217                 answer="0 $tape"
218         fi
219         echo "Exit -> $answer" >> $logfile
220         echo "$answer"
221         exit 0
222 }
223
224 # load #
225
226 loadslot() {
227         echo "     -> rewind $tape" >> $logfile
228         $MT $MTF $tape rewind >> $logfile 2>&1
229         echo "     -> status $tape" >> $logfile
230         used=`$MT $MTF $tape status 2>&1 | tee -a $logfile | $EGREP "$ONLINEREGEX"`
231         echo "     -> loaded <$used>" >> $logfile
232         whichslot=$1
233         case $whichslot in
234         current)
235                 load=$slot
236                 [ $load -eq 0 ] && load=$firstslot
237                 [ $load -gt $lastslot ] && load=$firstslot
238                 [ $load -lt $firstslot ] && load=$lastslot
239                 ;;
240         next|advance)
241                 load=`expr $slot + 1`
242                 [ $load -gt $lastslot ] && load=$firstslot
243                 ;;
244         prev)
245                 load=`expr $slot - 1`
246                 [ $load -lt $firstslot ] && load=$lastslot
247                 ;;
248         first)
249                 load=$firstslot
250                 ;;
251         last)
252                 load=$lastslot
253                 ;;
254         [0-9]|[0-9][0-9])
255                 if [ $1 -lt $firstslot -o $1 -gt $lastslot ]; then
256                         answer="<none> $myname: slot must be $firstslot .. $lastslot"
257                         echo "Exit -> $answer" >> $logfile
258                         echo "$answer"
259                         exit 1
260                 fi
261                 load=$1
262                 ;;
263         *)
264                 answer="<none> $myname: illegal slot: $1"
265                 echo "Exit -> $answer" >> $logfile
266                 echo "$answer"
267                 exit 1
268                 ;;
269         esac
270         #
271         if [ ! -z "$used" -a $load = $slot ];then
272                 # already loaded
273                 answer="$slot $tape"
274                 echo "Exit -> $answer" >> $logfile
275                 echo "$answer"
276                 exit 0
277         fi
278
279         # if [ $load = $ecleanslot ]; then
280         # expr $cleancount + 1 > $cleanfile
281         # echo 0 > $accessfile
282         # else
283         expr $accesscount + 1 > $accessfile
284         # if [ $accesscount -gt 9 ]; then
285         # $myname -slot $cleanslot >/dev/null
286         # used=0
287         # fi
288         # fi    
289
290         #
291         if [ ! -z "$used" ]; then
292                 echo "     -> offline $tape" >> $logfile
293                 $MT $MTF $tape offline >> $logfile 2>&1
294                 used=""
295         fi
296         if [ $whichslot = advance ]; then
297                 tape=/dev/null
298         else
299                 echo "     -> load   $load" >> $logfile
300                 while [ -z "$used" ]; do
301                         request $load
302                         echo "     -> rewind $tape" >> $logfile
303                         $MT $MTF $tape rewind >> $logfile 2>&1
304                         echo "     -> status $tape" >> $logfile
305                         used=`$MT $MTF $tape status 2>&1 | tee -a $logfile | $EGREP "$ONLINEREGEX"`
306                         echo "     -> loaded <$used>" >> $logfile
307                 done
308                 $DD if=$tape bs=32k count=1 >> $logfile 2>&1
309         fi
310         echo $load > $slotfile
311         answer="$load $tape"
312         echo "Exit -> $answer" >> $logfile
313         echo "$answer"
314         exit 0
315 }
316
317 #
318
319 info() {
320         echo "     -> rewind $tape" >> $logfile
321         $MT $MTF $tape rewind >> $logfile 2>&1
322         echo "     -> status $tape" >> $logfile
323         used=`$MT $MTF $tape status 2>&1 | tee -a $logfile | $EGREP "$ONLINEREGEX"`
324         echo "     -> loaded <$used>" >> $logfile
325         if [ -z "$used" ];then
326                 answer="0 $lastslot 1"
327         else
328                 answer="$slot $lastslot 1"
329         fi
330         echo "Exit -> $answer" >> $logfile
331         echo "$answer"
332         exit 0
333 }
334
335 #
336 # main part
337 #
338
339 echo Args "->" "$@" >> $logfile
340 while [ $# -ge 1 ];do
341         case $1 in
342         -slot)
343                 shift
344                 loadslot $*
345                 ;;
346         -info)
347                 shift
348                 info
349                 ;;
350         -reset)
351                 shift
352                 reset
353                 ;;
354         -eject)
355                 shift
356                 eject
357                 ;;
358         *)
359                 echo "<none> $myname: Unknown option $1"
360                 exit 2
361                 ;;
362         esac
363 done
364
365 exit 0