]> git.sur5r.net Git - bacula/bacula/blob - bacula/scripts/mtx-changer.in
Make filed/job.c downward compatible
[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 #  mtx-changer "changer-device" "command" "slot" "archive-device" "drive-index"
13 #                  $1              $2       $3        $4               $5
14 #
15 #  for example:
16 #
17 #  mtx-changer /dev/sg0 load 1 /dev/nst0 0 (on a Linux system)
18 #
19 #  If you need to an offline, refer to the drive as $4
20 #    e.g.   mt -f $4 offline
21 #
22 #  Many changers need an offline after the unload. Also many
23 #   changers need a sleep 60 after the mtx load.
24 #
25 #  N.B. If you change the script, take care to return either 
26 #   the mtx exit code or a 0. If the script exits with a non-zero
27 #   exit code, Bacula will assume the request failed.
28 #
29
30 MTX=@MTX@
31
32 #
33 # The purpose of this function to wait a maximum 
34 #   time for the drive. It will
35 #   return as soon as the drive is ready, or after
36 #   waiting a maximum of 180 seconds.
37 # Note, this is very system dependent, so if you are
38 #   not running on Linux, you will probably need to
39 #   re-write it, or at least change the grep target.
40 #
41 wait_for_drive() {
42   i=0 
43   while [ $i -le 180 ]; do  # Wait max 180 seconds
44     if mt -f $1 status | grep ONLINE  >/dev/null 2>&1; then
45       break
46     fi
47 #   echo "Device $1 - not ready, retrying..."
48     sleep 1
49     i=`expr $i + 1`
50   done
51 }
52
53
54 if test $# -lt 2 ; then
55   echo "usage: mtx-changer ctl-device command slot archive-device drive"
56   echo "  Insufficient number of arguments arguments given."
57   echo "  Mimimum usage is first two arguments ..."
58   exit 1
59 fi
60
61 # Setup arguments
62 ctl=$1
63 cmd="$2"
64 slot=$3
65 device=$4
66 # If drive not given, default to 0
67 if test $# = 5 ; then
68   drive=$5
69 else
70   drive=0
71 fi
72
73 #
74 # Check for special cases where only 2 arguments are needed, 
75 #  all others are a minimum of 3
76 case $cmd in
77    loaded)
78      ;;
79    unload)
80      ;;
81    list)
82      ;;
83    slots)
84      ;;
85    *)
86      if test $# -lt 3; then
87         echo "usage: mtx-changer ctl-device command slot archive-device drive"
88         echo "  Insufficient number of arguments arguments given."
89         echo "  Mimimum usage is first three arguments ..."
90         exit 1
91      fi
92      ;;
93 esac
94
95
96 case $cmd in 
97    unload)
98 #     echo "Doing mtx -f $ctl unload $slot $drive"
99 #
100 # enable the following line if you need to eject the cartridge
101 #     mt -f $device offline
102       if test x$slot = x; then
103          ${MTX} -f $ctl unload
104       else
105          ${MTX} -f $ctl unload $slot $drive
106       fi
107       ;;
108
109    load)
110 #     echo "Doing mtx -f $ctl load $slot $drive"
111       ${MTX} -f $ctl load $slot $drive
112       rtn=$?
113 #
114 # Increase the sleep time if you have a slow device
115 # or remove the sleep and add the following:
116 #     wait_for_drive $device
117       sleep 15
118       exit $rtn
119       ;;
120
121    list) 
122 #     echo "Requested list"
123       ${MTX} -f $ctl status | grep " *Storage Element [0-9]*:.*Full" | awk "{print \$3 \$4}" | sed "s/Full *\(:VolumeTag=\)*//"
124 # Comment out the previous line and add a line here
125 # to print "fake" barcodes.
126 #
127 # If you have a VXA PacketLoader and the above does not work, try
128 #  turning it off and enabling the following line.
129 #     ${MTX} -f $ctl status | grep " *Storage Element [0-9]*:.*Full" | sed "s/*Storage Element //" | sed "s/Full :VolumeTag=//"
130       ;;
131
132    loaded)
133       ${MTX} -f $ctl status >/tmp/mtx.$$
134       rtn=$?
135       cat /tmp/mtx.$$ | grep "^Data Transfer Element $drive:Full" | awk "{print \$7}"
136       cat /tmp/mtx.$$ | grep "^Data Transfer Element $drive:Empty" | awk "{print 0}"
137       rm -f /tmp/mtx.$$
138       exit $rtn
139       ;;
140
141    slots)
142 #     echo "Request slots"
143       ${MTX} -f $ctl status | grep " *Storage Changer" | awk "{print \$5}"
144       ;;
145 esac