]> git.sur5r.net Git - bacula/bacula/blob - bacula/scripts/mtx-changer.in
Add hostname to traceback
[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 #  Note Debian has a different mt than the standard Linux version. 
56 #    When no tape is in the drive it waits 2 minutes.  
57 #    When a tape is in the drive, it prints user unfriendly output.
58 #
59
60 OS=`uname`
61 case ${OS} in
62   SunOS)
63     ready="No Additional Sense"
64     ;;
65   FreeBSD)
66     ready="Current Driver State: at rest."
67     ;;
68   Linux)
69     if test -e /etc/debian_version ; then
70        ready="drive status"
71     else
72        ready="ONLINE"
73     fi
74   ;;
75 esac
76
77 #
78 # log whats done
79 #
80 # to turn on logging, uncomment the following line
81 #touch @working_dir@/mtx.log
82 #
83 dbgfile="@working_dir@/mtx.log"
84 debug() {
85     if test -f $dbgfile; then
86         echo "`date +\"%Y%m%d-%H:%M:%S\"` $*" >> $dbgfile
87     fi
88 }
89
90
91 #
92 # Create a temporary file
93 #
94 make_temp_file() {
95   TMPFILE=`mktemp @working_dir@/mtx.XXXXXXXXXX`
96   if test x${TMPFILE} = x; then
97      TMPFILE="@working_dir@/mtx.$$"
98      if test -f ${TMPFILE}; then
99         echo "Temp file security problem on: ${TMPFILE}"
100         exit 1
101      fi
102   fi
103 }
104
105 #
106 # The purpose of this function to wait a maximum 
107 #   time for the drive. It will
108 #   return as soon as the drive is ready, or after
109 #   waiting a maximum of 300 seconds.
110 # Note, this is very system dependent, so if you are
111 #   not running on Linux, you will probably need to
112 #   re-write it, or at least change the grep target.
113 #   We've attempted to get the appropriate OS grep targets
114 #   in the code at the top of this script.
115 #
116 wait_for_drive() {
117   i=0 
118   while [ $i -le 300 ]; do  # Wait max 300 seconds
119     if mt -f $1 status 2>&1 | grep "${ready}" >/dev/null 2>&1; then
120       break
121     fi
122     debug "Device $1 - not ready, retrying..."
123     sleep 1
124     i=`expr $i + 1`
125   done
126 }
127
128 # check parameter count on commandline
129 #
130 check_parm_count() {
131     pCount=$1
132     pCountNeed=$2
133     if test $pCount -lt $pCountNeed; then
134         echo "usage: mtx-changer ctl-device command [slot archive-device drive-index]"
135         echo "  Insufficient number of arguments given."
136         if test $pCount -lt 2; then
137             echo "  Mimimum usage is first two arguments ..."
138         else
139             echo "  Command expected $pCountNeed arguments"
140         fi
141         exit 1
142     fi
143 }
144
145 # Check for special cases where only 2 arguments are needed, 
146 #  all others are a minimum of 5
147 #
148 case $2 in
149     list)
150         check_parm_count $# 2
151         ;;
152     slots)
153         check_parm_count $# 2
154         ;;
155     *)
156         check_parm_count $# 5
157         ;;
158 esac
159
160
161 # Setup arguments
162 ctl=$1
163 cmd="$2"
164 slot=$3
165 device=$4
166 drive=$5
167
168 debug "Parms: $ctl $cmd $slot $device $drive"
169
170 case $cmd in 
171    unload)
172       debug "Doing mtx -f $ctl unload $slot $drive"
173 #
174 # enable the following line if you need to eject the cartridge
175 #     mt -f $device offline
176 #     sleep 10
177       ${MTX} -f $ctl unload $slot $drive
178       ;;
179
180    load)
181       debug "Doing mtx -f $ctl load $slot $drive"
182       ${MTX} -f $ctl load $slot $drive
183       rtn=$?
184 #
185 # Increase the sleep time if you have a slow device
186 # or remove the sleep and add the following:
187 #     sleep 15
188       wait_for_drive $device
189       exit $rtn
190       ;;
191
192    list) 
193       debug "Doing mtx -f $ctl -- to list volumes"
194       make_temp_file
195 # Enable the following if you are using barcodes and need an inventory
196 #     ${MTX} -f $ctl inventory
197       ${MTX} -f $ctl status >${TMPFILE}
198       rtn=$?
199       cat ${TMPFILE} | grep " Storage Element [0-9]*:.*Full" | awk "{print \$3 \$4}" | sed "s/Full *\(:VolumeTag=\)*//"
200
201 # If you want/can use IMPORT/EXPORT slots as normal slots within bacula
202 # replace the previous line by this one
203 # sed "s/ IMPORT\/EXPORT//" "${TMPFILE}" | grep " Storage Element [0-9]*:.*Full" | awk "{print \$3 \$4}" | sed "s/Full *\(:VolumeTag=\)*//"
204 #
205 # If you have a VXA PacketLoader and the above does not work, try
206 #  turning it off and enabling the following line.
207 #     cat ${TMPFILE} | grep " *Storage Element [0-9]*:.*Full" | sed "s/ Storage Element //" | sed "s/Full :VolumeTag=//"
208 #
209       cat ${TMPFILE} | grep "^Data Transfer Element [0-9]*:Full (Storage Element [0-9]" | awk '{printf "%s:%s\n",$7,$10}'
210       rm -f ${TMPFILE} >/dev/null 2>&1
211       exit $rtn
212       ;;
213
214    loaded)
215       debug "Doing mtx -f $ctl $drive -- to find what is loaded"
216       make_temp_file
217       ${MTX} -f $ctl status >${TMPFILE}
218       rtn=$?
219       cat ${TMPFILE} | grep "^Data Transfer Element $drive:Full" | awk "{print \$7}"
220       cat ${TMPFILE} | grep "^Data Transfer Element $drive:Empty" | awk "{print 0}"
221       rm -f ${TMPFILE} >/dev/null 2>&1
222       exit $rtn
223       ;;
224
225    slots)
226       debug "Doing mtx -f $ctl -- to get count of slots"
227       ${MTX} -f $ctl status | grep " *Storage Changer" | awk "{print \$5}"
228       ;;
229 esac