]> git.sur5r.net Git - bacula/bacula/blob - bacula/examples/autochangers/solaris-mtx-changer
Make out of freespace non-fatal for removable devices -- i.e. behaves like tape
[bacula/bacula] / bacula / examples / autochangers / solaris-mtx-changer
1 #!/bin/bash
2 #
3 # /bin/sh isn't always compatible so use /bin/bash
4 #
5 # Bacula interface to mtx autoloader
6 #
7 #  $Id$
8 #
9 #  If you set in your Device resource
10 #
11 #  Changer Command = "path-to-this-script/mtx-changer  %c %o %S %a %d"
12 #    you will have the following input to this script:
13 #
14 #  mtx-changer "changer-device" "command" "slot" "archive-device" "drive-index"
15 #                  $1              $2       $3        $4               $5
16 #
17 #  for example:
18 #
19 #  mtx-changer /dev/sg0 load 1 /dev/nst0 0 (on a Linux system)
20 #
21 #  If you need to an offline, refer to the drive as $4
22 #    e.g.   mt -f $4 offline
23 #
24 #  Many changers need an offline after the unload. Also many
25 #   changers need a sleep 60 after the mtx load.
26 #
27 #  N.B. If you change the script, take care to return either 
28 #   the mtx exit code or a 0. If the script exits with a non-zero
29 #   exit code, Bacula will assume the request failed.
30 #
31
32 # Sun sed/awk etc are not sufficient, working versions are in /usr/xpg4/bin
33 export PATH="/usr/local/bin:/usr/sfw/bin:/usr/xpg4/bin:/usr/bin"
34
35 MTX=mtx
36
37 TMPDIR=/tmp
38
39 make_temp_file() 
40 {
41   TMPFILE=`mktemp ${TMPDIR}/mtx$1.XXXXXXXXXX 2> /dev/null`
42   if test $? -ne 0 || test x${TMPFILE} = x; then
43      TMPFILE="${TMPDIR}/mtx$1.$$"
44      if test -f ${TMPFILE}; then
45         echo "ERROR: Temp file security problem on: ${TMPFILE}"
46         exit 1
47      fi
48   fi
49 }
50
51
52
53
54 #
55 # The purpose of this function to wait a maximum 
56 #   time for the drive. It will
57 #   return as soon as the drive is ready, or after
58 #   waiting a maximum of 180 seconds.
59 # Note, this is very system dependent, so if you are
60 #   not running on Linux, you will probably need to
61 #   re-write it.
62 #
63 # If you have a FreeBSD system, you might want to change
64 #  the $(seq 180) to $(jot 180) -- tip from Brian McDonald
65 #
66 wait_for_drive() {
67   for i in $(seq 180); do   # Wait max 180 seconds
68     if ( mt -f $1 status | grep 0x0 ) >/dev/null 2>&1; then
69       #echo "Device $1 READY"
70       break
71     fi
72     #echo "Device $1 - not ready, retry ${i}..."
73     sleep 1
74   done
75 }
76
77
78 if test $# -lt 2 ; then
79   echo "usage: mtx-changer ctl-device command slot archive-device drive"
80   echo "  Insufficient number of arguments arguments given."
81   echo "  Mimimum usage is first two arguments ..."
82   exit 1
83 fi
84
85 # Setup arguments
86 ctl=$1
87 cmd="$2"
88 slot=$3
89 device=$4
90 # If drive not given, default to 0
91 if test $# = 5 ; then
92   drive=$5
93 else
94   drive=0
95 fi
96
97 #
98 # Check for special cases where only 2 arguments are needed, 
99 #  all others are a minimum of 3
100 case $cmd in
101    loaded)
102      ;;
103    unload)
104      ;;
105    list)
106      ;;
107    slots)
108      ;;
109    *)
110      if test $# -lt 3; then
111         echo "usage: mtx-changer ctl-device command slot archive-device drive"
112         echo "  Insufficient number of arguments arguments given."
113         echo "  Mimimum usage is first three arguments ..."
114         exit 1
115      fi
116      ;;
117 esac
118
119
120 case $cmd in 
121    unload)
122 #     echo "Doing mtx -f $ctl unload $slot $drive"
123 #
124 # enable the following line if you need to eject the cartridge
125       #mt -f $device offline
126       mt -f $device rewoffl
127       if test x$slot = x; then
128          ${MTX} -f $ctl unload
129       else
130          ${MTX} -f $ctl unload $slot $drive
131       fi
132       ;;
133
134    load)
135 #     echo "Doing mtx -f $ctl load $slot $drive"
136       ${MTX} -f $ctl load $slot $drive
137       rtn=$?
138 #
139 # Increase the sleep time if you have a slow device
140 # or remove the sleep and add the following:
141       #sleep 15
142       wait_for_drive $device
143       exit $rtn
144       ;;
145
146    list) 
147 #     echo "Requested list"
148       # Some tape changers lose track of their inventory (well, mine does) so 
149       # do one before trying to get a status out of it.
150       ${MTX} -f $ctl inventory
151       ${MTX} -f $ctl status | grep " *Storage Element [0-9]*:.*Full" | awk "{print \$3 \$4}" | sed "s/Full *\(:VolumeTag=\)*//"
152 # Comment out the previous line and add a line here
153 # to print "fake" barcodes.
154 #
155 # If you have a VXA PacketLoader and the above does not work, try
156 #  turning it off and enabling the following line.
157 #     ${MTX} -f $ctl status | grep " *Storage Element [0-9]*:.*Full" | sed "s/*Storage Element //" | sed "s/Full :VolumeTag=//"
158       ;;
159
160    loaded)
161       make_temp_file
162       ${MTX} -f $ctl status >${TMPFILE}
163       rtn=$?
164       cat ${TMPFILE} | grep "^Data Transfer Element $drive:Full" | awk "{print \$7}"
165       cat ${TMPFILE} | grep "^Data Transfer Element $drive:Empty" | awk "{print 0}"
166       rm -f ${TMPFILE}
167       exit $rtn
168       ;;
169
170    slots)
171 #     echo "Request slots"
172       ${MTX} -f $ctl status | grep " *Storage Changer" | awk "{print \$5}"
173       ;;
174 esac