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