]> git.sur5r.net Git - bacula/bacula/blob - bacula/examples/autochangers/locking-mtx-changer
Add Solaris regress comments
[bacula/bacula] / bacula / examples / autochangers / locking-mtx-changer
1 #!/bin/ksh
2 #
3 # Bacula interface to mtx autoloader
4 #
5 #  This script is not needed with Bacula version 1.38 or later
6 #  since the Storage daemon automatically ensures that only one
7 #  thread accesses the script at a time.     
8 #
9 #
10 #  $Id$
11 #
12 #  If you set in your Device resource
13 #
14 #  Changer Command = "path-to-this-script/mtx-changer %c %o %S %a %d
15 #    you will have the following input to this script:
16 #
17 #  mtx-changer "changer-device" "command" "slot" "archive-device" "drive-index"
18 #                  $1              $2       $3        $4               $5
19 #
20 #  for example:
21 #
22 #  mtx-changer /dev/sg0 load 1 /dev/nst0 0 (on a Linux system)
23 #
24 #  If you need to an offline, refer to the drive as $4
25 #    e.g.   mt -f $4 offline
26 #
27 #  Many changers need an offline after the unload. Also many
28 #   changers need a sleep 60 after the mtx load.
29 #
30 #  N.B. If you change the script, take care to return either 
31 #   the mtx exit code or a 0. If the script exits with a non-zero
32 #   exit code, Bacula will assume the request failed.
33 #
34
35 MTX=/lysator/bin/mtx
36 LOCKDIR=/tmp
37
38 if test $# -lt 2 ; then
39   echo "usage: mtx-changer ctl-device command slot archive-device drive"
40   echo "  Insufficient number of arguments arguments given."
41   echo "  Mimimum usage is first two arguments ..."
42   exit 1
43 fi
44
45 # Setup arguments
46 ctl=$1
47 cmd="$2"
48 slot=$3
49 device=$4
50 # If drive not given, default to 0
51 if test $# = 5 ; then
52   drive=$5
53 else
54   drive=0
55 fi
56
57 wait_for_drive() {
58     while ! mt -f $1 status >/dev/null 2>/dev/null; do
59 #       echo "Device $1 - not ready, retrying..."
60         sleep 5
61     done
62 }
63
64 LOCKFILE="${LOCKDIR}/mtx-changer:`echo $ctl | tr / _'"
65
66 changer_lock() {
67     echo "$$" >$LOCKFILE.$$
68     
69     while ! ln -n $LOCKFILE.$$ $LOCKFILE 2>/dev/null; do
70        echo "$0: changer lock busy, retrying in 30 seconds..."
71        sleep 30
72     done
73
74     rm $LOCKFILE.$$
75 }
76
77 changer_unlock() {
78     LOCKPID="`cat $LOCKFILE 2>/dev/null`"
79     if [ "$LOCKPID" != $$ ]; then
80         echo "$0: Invalid lock file (${LOCKFILE}) - not owned by us!"
81         exit 1
82     fi
83     rm -f $LOCKFILE
84 }
85
86
87
88 #
89 # Check for special cases where only 2 arguments are needed, 
90 #  all others are a minimum of 3
91 case $cmd in
92    loaded)
93      ;;
94    unload)
95      ;;
96    list)
97      ;;
98    slots)
99      ;;
100    *)
101      if test $# -lt 3; then
102         echo "usage: mtx-changer ctl-device command slot archive-device drive"
103         echo "  Insufficient number of arguments arguments given."
104         echo "  Mimimum usage is first three arguments ..."
105         exit 1
106      fi
107      ;;
108 esac
109
110 changer_lock $ctl
111
112 case $cmd in 
113    unload)
114 #     echo "Doing mtx -f $ctl unload $slot $drive"
115 #
116 # enable the following line if you need to eject the cartridge
117       mt -f $device offline
118       if test x$slot = x; then
119          ${MTX} -f $ctl unload
120          rtn=$?
121       else
122          ${MTX} -f $ctl unload $slot $drive
123          rtn=$?
124       fi
125       ;;
126
127    load)
128 #     echo "Doing mtx -f $ctl load $slot $drive"
129       ${MTX} -f $ctl load $slot $drive
130       rtn=$?
131
132       wait_for_drive $device
133       changer_unlock $ctl
134       exit $rtn
135       ;;
136
137    list) 
138 #     echo "Requested list"
139       ${MTX} -f $ctl status | tr ':=' '  ' | nawk '($1 == "Storage" && $2 == "Element" && $4 == "Full") { printf "%s:%s\n", $3, $6 }'
140       rtn=$?
141       ;;
142
143    loaded)
144       ${MTX} -f $ctl status >/tmp/mtx.$$
145       rtn=$?
146       cat /tmp/mtx.$$ | grep "^Data Transfer Element $drive:Full" | awk "{print \$7}"
147       cat /tmp/mtx.$$ | grep "^Data Transfer Element $drive:Empty" | awk "{print 0}"
148       rm -f /tmp/mtx.$$
149       changer_unlock $ctl
150       exit $rtn
151       ;;
152
153    slots)
154 #     echo "Request slots"
155       ${MTX} -f $ctl status | grep " *Storage Changer" | awk "{print \$5}"
156       rtn=$?
157       ;;
158 esac
159
160 changer_unlock $ctl
161 exit $rtn