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