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