]> git.sur5r.net Git - bacula/bacula/blob - bacula/examples/autochangers/locking-mtx-changer
451a36de982a651ee4c5842a1b2721bbdfc4adb2
[bacula/bacula] / bacula / examples / autochangers / locking-mtx-changer
1 #!/bin/ksh
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=/lysator/bin/mtx
31 LOCKDIR=/tmp
32
33 if test $# -lt 2 ; then
34   echo "usage: mtx-changer ctl-device command slot archive-device drive"
35   echo "  Insufficient number of arguments arguments given."
36   echo "  Mimimum usage is first two arguments ..."
37   exit 1
38 fi
39
40 # Setup arguments
41 ctl=$1
42 cmd="$2"
43 slot=$3
44 device=$4
45 # If drive not given, default to 0
46 if test $# = 5 ; then
47   drive=$5
48 else
49   drive=0
50 fi
51
52 wait_for_drive() {
53     while ! mt -f $1 status >/dev/null 2>/dev/null; do
54 #       echo "Device $1 - not ready, retrying..."
55         sleep 5
56     done
57 }
58
59 LOCKFILE="${LOCKDIR}/mtx-changer:`echo $ctl | tr / _'"
60
61 changer_lock() {
62     echo "$$" >$LOCKFILE.$$
63     
64     while ! ln -n $LOCKFILE.$$ $LOCKFILE 2>/dev/null; do
65        echo "$0: changer lock busy, retrying in 30 seconds..."
66        sleep 30
67     done
68
69     rm $LOCKFILE.$$
70 }
71
72 changer_unlock() {
73     LOCKPID="`cat $LOCKFILE 2>/dev/null`"
74     if [ "$LOCKPID" != $$ ]; then
75         echo "$0: Invalid lock file (${LOCKFILE}) - not owned by us!"
76         exit 1
77     fi
78 }
79
80
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 changer_lock $ctl
105
106 case $cmd in 
107    unload)
108 #     echo "Doing mtx -f $ctl unload $slot $drive"
109 #
110 # enable the following line if you need to eject the cartridge
111       mt -f $device offline
112       if test x$slot = x; then
113          ${MTX} -f $ctl unload
114          rtn=$?
115       else
116          ${MTX} -f $ctl unload $slot $drive
117          rtn=$?
118       fi
119       ;;
120
121    load)
122 #     echo "Doing mtx -f $ctl load $slot $drive"
123       ${MTX} -f $ctl load $slot $drive
124       rtn=$?
125
126       wait_for_drive $device
127       changer_unlock $ctl
128       exit $rtn
129       ;;
130
131    list) 
132 #     echo "Requested list"
133       ${MTX} -f $ctl status | tr ':=' '  ' | nawk '($1 == "Storage" && $2 == "Element" && $4 == "Full") { printf "%s:%s\n", $3, $6 }'
134       rtn=$?
135       ;;
136
137    loaded)
138       ${MTX} -f $ctl status >/tmp/mtx.$$
139       rtn=$?
140       cat /tmp/mtx.$$ | grep "^Data Transfer Element $drive:Full" | awk "{print \$7}"
141       cat /tmp/mtx.$$ | grep "^Data Transfer Element $drive:Empty" | awk "{print 0}"
142       rm -f /tmp/mtx.$$
143       changer_unlock $ctl
144       exit $rtn
145       ;;
146
147    slots)
148 #     echo "Request slots"
149       ${MTX} -f $ctl status | grep " *Storage Changer" | awk "{print \$5}"
150       rtn=$?
151       ;;
152 esac
153
154 changer_unlock $ctl
155 exit $rtn