]> git.sur5r.net Git - bacula/bacula/blob - bacula/examples/autochangers/chio-changer
15Jan06
[bacula/bacula] / bacula / examples / autochangers / chio-changer
1 #!/bin/sh
2 #
3 # Bacula interface to autoloader
4 #
5 #  By Pascal Pederiva <freebsd@paped.com>
6 #
7 #  Known to work on FreeBSD 5.2 with a TZ875 changer.
8 #
9 #  This script mimics mtx-changer with the following differences
10 # - it automatically stows the cartridge to the slot it came from when
11 #   unloading.
12 # - a load will automatically unload the drive if there is a
13     different cartridge loaded.
14 # - it uses chio instead of mtx (which is
15     available as a package) 
16 #
17 #  If you set in your Device resource
18 #
19 #  Changer Command = "path-to-this-script/chio-changer" %c %o %S %a
20 #    you will have the following input to this script:
21 #
22 #  chio-changer "changer-device" "command" "slot" "archive-device"
23 #
24 #  for example:
25 #
26 #  chio-changer /dev/sg0 load 1 /dev/nst0 (on a Linux system)
27 #
28 #  If you need to to an offline, refer to the drive as $4
29 #    e.g.   mt -f $4 offline
30 #
31 #  Many changers need an offline after the unload. Also many
32 #   changers need a sleep 60 after the chio load.
33 #
34 #  N.B. If you change the script, take care to return either 
35 #   the chio exit code or a 0. If the script exits with a non-zero
36 #   exit code, Bacula will assume the request failed.
37 #
38 # Examples:
39 # chio-changer load 1                      ; load slot 1 into drive 0 (and unload old cartridge if necessary)
40 # chio-changer unload N                    ; unload drive into source slot (slot number is ignored)
41 # chio-changer loaded N                    ; Return loaded slot #
42 # chio-changer /dev/ch0 loaded N /dev/nsa0 ; has the same effect
43
44
45 #echo `date` "chio:  $*" >>/tmp/changer
46
47 # If the first parameter is not a device, assume it is a command
48 # and you want to use the default changer
49
50 if [ -c $1 ]; then
51    CHANGER=$1
52    shift
53 else
54    CHANGER=/dev/ch0
55 fi
56
57 COMMAND=$1
58 ELEMENT=$2
59 DRIVE=$3
60
61 MTX=chio
62
63 ##############################################################################
64
65 case "$COMMAND" in 
66    unload)
67
68 # enable the following line if you need to eject the cartridge
69 #     mt -f $DRIVE offline
70       SOURCE=`${MTX} status -S | grep drive | grep FULL | cut -d: -f 3 | tr -d '<>a-z '`
71       if [ ! -z "$SOURCE" ]; then
72         echo -n "Unloading Data Transfer Element into Storage Element $ELEMENT..."
73         ${MTX} -f $CHANGER move drive 0 slot $SOURCE
74         rtn=$?
75         echo "done"
76         else
77         echo "Storage Element $ELEMENT is Already Full"
78       fi
79       exit $rtn
80       ;;
81
82    load)
83       if [ -z "$ELEMENT" ]; then
84          echo "ERROR: load <element> reguired"
85          return 1
86       fi
87       TARGET=$ELEMENT
88       if [ $TARGET -le 0 ]; then
89          TARGET=1
90       fi
91       TARGET=`expr $TARGET - 1`
92
93       SOURCE=`${MTX} status -S | grep drive | grep FULL | cut -d: -f 3 | tr -d '<>a-z '`
94       if [ ! -z "$SOURCE" ]; then
95         if [ "$SOURCE" != "$TARGET" ]; then
96            # Only unload if there is something different in the drive
97            ${MTX} -f $CHANGER move drive 0 slot $SOURCE
98         fi
99       fi
100
101       if [ "$SOURCE" != "$TARGET" ]; then
102         ${MTX} -f $CHANGER move slot $TARGET drive 0
103         rtn=$? 
104       fi
105       exit $rtn
106       ;;
107
108    list) 
109       ${MTX} -f $CHANGER status slot | grep "FULL" | awk '{print $2+1":"}'
110       ;;
111
112    loaded)
113       SOURCE=`${MTX} status -S | grep drive | grep FULL | cut -d: -f 3 | tr -d '<>a-z '`
114       rtn=$?
115       if [ -z "$SOURCE" ]; then
116          SOURCE=-1
117       fi
118       echo `expr 1 + ${SOURCE}`
119       exit $rtn
120       ;;
121
122    slots)
123       ${MTX} -f $CHANGER status slot  | wc -l
124       ;;
125 esac