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