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