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