]> git.sur5r.net Git - bacula/bacula/blob - bacula/scripts/mtx-changer.in
ebl Ok in the current trunk
[bacula/bacula] / bacula / scripts / mtx-changer.in
1 #!/bin/sh
2 #
3 # Bacula interface to mtx autoloader
4 #
5 #  $Id$
6 #
7 #  If you set in your Device resource
8 #
9 #  Changer Command = "path-to-this-script/mtx-changer %c %o %S %a %d"
10 #    you will have the following input to this script:
11 #
12 #  So Bacula will always call with all the following arguments, even though
13 #    in come cases, not all are used.
14 #
15 #  mtx-changer "changer-device" "command" "slot" "archive-device" "drive-index"
16 #                  $1              $2       $3        $4               $5
17 #
18 #  for example:
19 #
20 #  mtx-changer /dev/sg0 load 1 /dev/nst0 0 (on a Linux system)
21
22 #  will request to load the first cartidge into drive 0, where
23 #   the SCSI control channel is /dev/sg0, and the read/write device
24 #   is /dev/nst0.
25 #
26 #  The commands are:
27 #      Command            Function
28 #      unload             unload a given slot
29 #      load               load a given slot
30 #      loaded             which slot is loaded?
31 #      list               list Volume names (requires barcode reader)
32 #      slots              how many slots total?
33 #
34 #  Slots are numbered from 1 ...
35 #  Drives are numbered from 0 ...
36 #
37 #
38 #  If you need to an offline, refer to the drive as $4
39 #    e.g.   mt -f $4 offline
40 #
41 #  Many changers need an offline after the unload. Also many
42 #   changers need a sleep 60 after the mtx load.
43 #
44 #  N.B. If you change the script, take care to return either 
45 #   the mtx exit code or a 0. If the script exits with a non-zero
46 #   exit code, Bacula will assume the request failed.
47 #
48
49 MTX=@MTX@
50
51 # mt status output
52 # SunOS     No Additional Sense
53 # FreeBSD   Current Driver State: at rest.
54 # Linux     ONLINE
55
56 OS=`uname`
57 case ${OS} in
58   SunOS)
59     ready="No Additional Sense"
60     # Sun sed/awk etc are not sufficient, working versions are in /usr/xpg4/bin
61     export PATH="/usr/local/sbin:/usr/local/bin:/usr/sfw/bin:/usr/xpg4/bin:/usr/bin"
62     ;;
63   FreeBSD)
64     ready="Current Driver State: at rest."
65     ;;
66   *)
67     ready="ONLINE"
68   ;;
69 esac
70
71 #
72 # log whats done
73 #
74 # to turn on logging, uncomment the following line
75 #touch @working_dir@/mtx.log
76 #
77 dbgfile="@working_dir@/mtx.log"
78 debug() {
79     if test -f $dbgfile; then
80         echo "`date +\"%Y%m%d-%H:%M:%S\"` $*" >> $dbgfile
81     fi
82 }
83
84
85 #
86 # Create a temporary file
87 #
88 make_temp_file() {
89   TMPFILE=`mktemp @working_dir@/mtx.XXXXXXXXXX`
90   if test x${TMPFILE} = x; then
91      TMPFILE="@working_dir@/mtx.$$"
92      if test -f ${TMPFILE}; then
93         echo "Temp file security problem on: ${TMPFILE}"
94         exit 1
95      fi
96   fi
97 }
98
99 #
100 # The purpose of this function to wait a maximum 
101 #   time for the drive. It will
102 #   return as soon as the drive is ready, or after
103 #   waiting a maximum of 300 seconds.
104 # Note, this is very system dependent, so if you are
105 #   not running on Linux, you will probably need to
106 #   re-write it, or at least change the grep target.
107 #   We've attempted to get the appropriate OS grep targets
108 #   in the code at the top of this script.
109 #
110 wait_for_drive() {
111   i=0 
112   while [ $i -le 300 ]; do  # Wait max 300 seconds
113     if mt -f $1 status 2>&1 | grep "${ready}" >/dev/null 2>&1; then
114       break
115     fi
116     debug "Device $1 - not ready, retrying..."
117     sleep 1
118     i=`expr $i + 1`
119   done
120 }
121
122 # check parameter count on commandline
123 #
124 check_parm_count() {
125     pCount=$1
126     pCountNeed=$2
127     if test $pCount -lt $pCountNeed; then
128         echo "usage: mtx-changer ctl-device command [slot archive-device drive-index]"
129         echo "  Insufficient number of arguments given."
130         if test $pCount -lt 2; then
131             echo "  Mimimum usage is first two arguments ..."
132         else
133             echo "  Command expected $pCountNeed arguments"
134         fi
135         exit 1
136     fi
137 }
138
139 # Check for special cases where only 2 arguments are needed, 
140 #  all others are a minimum of 5
141 #
142 case $2 in
143     list)
144         check_parm_count $# 2
145         ;;
146     slots)
147         check_parm_count $# 2
148         ;;
149     *)
150         check_parm_count $# 5
151         ;;
152 esac
153
154
155 # Setup arguments
156 ctl=$1
157 cmd="$2"
158 slot=$3
159 device=$4
160 drive=$5
161
162 debug "Parms: $ctl $cmd $slot $device $drive"
163
164 case $cmd in 
165    unload)
166       debug "Doing mtx -f $ctl unload $slot $drive"
167 #
168 # enable the following line if you need to eject the cartridge
169 #     mt -f $device offline
170 #     sleep 10
171       ${MTX} -f $ctl unload $slot $drive
172       ;;
173
174    load)
175       debug "Doing mtx -f $ctl load $slot $drive"
176       ${MTX} -f $ctl load $slot $drive
177       rtn=$?
178 #
179 # Increase the sleep time if you have a slow device
180 # or remove the sleep and add the following:
181 #     sleep 15
182       wait_for_drive $device
183       exit $rtn
184       ;;
185
186    list) 
187       debug "Doing mtx -f $ctl -- to list volumes"
188       make_temp_file
189 # Enable the following if you are using barcodes and need an inventory
190 #     ${MTX} -f $ctl inventory
191       ${MTX} -f $ctl status >${TMPFILE}
192       rtn=$?
193       cat ${TMPFILE} | grep " Storage Element [0-9]*:.*Full" | awk "{print \$3 \$4}" | sed "s/Full *\(:VolumeTag=\)*//"
194 #
195 # If you have a VXA PacketLoader and the above does not work, try
196 #  turning it off and enabling the following line.
197 #     cat ${TMPFILE} | grep " *Storage Element [0-9]*:.*Full" | sed "s/ Storage Element //" | sed "s/Full :VolumeTag=//"
198 #
199       cat ${TMPFILE} | grep "^Data Transfer Element [0-9]*:Full (Storage Element [0-9]" | awk '{printf "%s:%s\n",$7,$10}'
200       rm -f ${TMPFILE} >/dev/null 2>&1
201       exit $rtn
202       ;;
203
204    loaded)
205       debug "Doing mtx -f $ctl $drive -- to find what is loaded"
206       make_temp_file
207       ${MTX} -f $ctl status >${TMPFILE}
208       rtn=$?
209       cat ${TMPFILE} | grep "^Data Transfer Element $drive:Full" | awk "{print \$7}"
210       cat ${TMPFILE} | grep "^Data Transfer Element $drive:Empty" | awk "{print 0}"
211       rm -f ${TMPFILE} >/dev/null 2>&1
212       exit $rtn
213       ;;
214
215    slots)
216       debug "Doing mtx -f $ctl -- to get count of slots"
217       ${MTX} -f $ctl status | grep " *Storage Changer" | awk "{print \$5}"
218       ;;
219 esac