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