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