]> git.sur5r.net Git - bacula/bacula/blob - bacula/scripts/mtx-changer.in
e1c0d4a98bd6d53a07e8c54757d4aba1bddbbf79
[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.
40 #
41 wait_for_drive() {
42   for i in $(seq 180); do   # Wait max 180 seconds
43     if mt -f $1 status >/dev/null 2>&1; then
44       break
45     fi
46 #   echo "Device $1 - not ready, retrying..."
47     sleep 1
48   done
49 }
50
51
52 if test $# -lt 2 ; then
53   echo "usage: mtx-changer ctl-device command slot archive-device drive"
54   echo "  Insufficient number of arguments arguments given."
55   echo "  Mimimum usage is first two arguments ..."
56   exit 1
57 fi
58
59 # Setup arguments
60 ctl=$1
61 cmd="$2"
62 slot=$3
63 device=$4
64 # If drive not given, default to 0
65 if test $# = 5 ; then
66   drive=$5
67 else
68   drive=0
69 fi
70
71 #
72 # Check for special cases where only 2 arguments are needed, 
73 #  all others are a minimum of 3
74 case $cmd in
75    loaded)
76      ;;
77    unload)
78      ;;
79    list)
80      ;;
81    slots)
82      ;;
83    *)
84      if test $# -lt 3; then
85         echo "usage: mtx-changer ctl-device command slot archive-device drive"
86         echo "  Insufficient number of arguments arguments given."
87         echo "  Mimimum usage is first three arguments ..."
88         exit 1
89      fi
90      ;;
91 esac
92
93
94 case $cmd in 
95    unload)
96 #     echo "Doing mtx -f $ctl unload $slot $drive"
97 #
98 # enable the following line if you need to eject the cartridge
99 #     mt -f $device offline
100       if test x$slot = x; then
101          ${MTX} -f $ctl unload
102       else
103          ${MTX} -f $ctl unload $slot $drive
104       fi
105       ;;
106
107    load)
108 #     echo "Doing mtx -f $ctl load $slot $drive"
109       ${MTX} -f $ctl load $slot $drive
110       rtn=$?
111 #
112 # Increase the sleep time if you have a slow device
113 # or remove the sleep and add the following:
114 #     wait_for_drive $device
115       sleep 15
116       exit $rtn
117       ;;
118
119    list) 
120 #     echo "Requested list"
121       ${MTX} -f $ctl status | grep " *Storage Element [0-9]*:.*Full" | awk "{print \$3 \$4}" | sed "s/Full *\(:VolumeTag=\)*//"
122 # Comment out the previous line and add a line here
123 # to print "fake" barcodes.
124       ;;
125
126    loaded)
127       ${MTX} -f $ctl status >/tmp/mtx.$$
128       rtn=$?
129       cat /tmp/mtx.$$ | grep "^Data Transfer Element $drive:Full" | awk "{print \$7}"
130       cat /tmp/mtx.$$ | grep "^Data Transfer Element $drive:Empty" | awk "{print 0}"
131       rm -f /tmp/mtx.$$
132       exit $rtn
133       ;;
134
135    slots)
136 #     echo "Request slots"
137       ${MTX} -f $ctl status | grep " *Storage Changer" | awk "{print \$5}"
138       ;;
139 esac