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