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