]> git.sur5r.net Git - bacula/bacula/blob - bacula/examples/autochangers/chio-changer.Sony-TSL-SA300C
Fix bug #1812 cannot run Copy/Migrate jobs from bat
[bacula/bacula] / bacula / examples / autochangers / chio-changer.Sony-TSL-SA300C
1 #!/bin/sh
2 #
3 # Bacula interface to mtx autoloader
4 # (By Lars Köller, lars+bacula@koellers.net)
5 #
6 # Modified by Jesse D. Guardiani (jesse@wingnet.net) in Feb 2004
7 # to be more error resistant and compatible with my 4 tape SONY
8 # AIT-1 TSL-SA300C autoloader.
9 #
10 #  If you set in your Device resource
11 #
12 #  Changer Command = "path-to-this-script/chio-bacula %c %o %S %a"
13 #    you will have the following input to this script:
14 #
15 #  chio-bacula "changer-device" "command" "slot" "archive-device"
16 #
17 #  for example:
18 #
19 #  chio-bacula /dev/ch0 load 1 /dev/nst0 (on a FreeBSD system)
20 #
21 #  If you need to to an offline, refer to the drive as $4
22 #    e.g.   mt -f $f offline
23 #
24 #  Many changers need an offline after the unload. Also many
25 #   changers need a sleep 60 after the mtx load.
26 #
27 #  N.B. If you change the script, take care to return either
28 #   the mtx exit code or a 0. If the script exits with a non-zero
29 #   exit code, Bacula will assume the request failed.
30 #
31
32 # This simulates a barcode reader in the changer.
33 # The labels of the virtual barcode reader are located in the BARCODE_FILE
34 SIMULATE_BARCODE=true
35 BARCODE_FILE=/usr/local/etc/bacula-barcodes
36
37 TMPDIR=/tmp
38
39 make_temp_file() 
40 {
41   TMPFILE=`mktemp ${TMPDIR}/mtx$1.XXXXXXXXXX 2> /dev/null`
42   if test $? -ne 0 || test x${TMPFILE} = x; then
43      TMPFILE="${TMPDIR}/mtx$1.$$"
44      if test -f ${TMPFILE}; then
45         echo "ERROR: Temp file security problem on: ${TMPFILE}"
46         exit 1
47      fi
48   fi
49 }
50
51
52 me=$(basename $0)
53 fullpath_me=$0
54
55 # Debug
56 logger -p user.err "$fullpath_me $@"
57
58 if [ -z "$1" ] ; then
59     usage;
60 fi
61
62 if [ -z "$2" ] ; then
63     usage;
64 fi
65
66 MTX=/bin/chio
67 CHANGER=$1
68 COMMAND=$2
69 if [ ! -z "$3" ]; then
70     SLOT=$3
71 fi
72 if [ ! -z "$4" ]; then
73     TAPE=$4
74 else
75     TAPE=/dev/nrsa1
76 fi
77
78 # Time to wait for loading
79 SLEEP=20
80 # What drive of the autochanger should be used primary
81 # At the moment bacula (1.31a) could not deal with more than one drive
82 DRIVE=0
83
84 usage()
85 {
86   echo ""
87   echo "The $me script for bacula"
88   echo "--------------------------------------"
89   echo ""
90   echo "usage: $me <changer-device> <command> [slot] [devicename of tapedrive]"
91   echo ""
92   echo "Valid commands:"
93   echo ""
94   echo "unload          Unloads a tape into the slot"
95   echo "                from where it was loaded."
96   echo "load <slot>     Loads a tape from the slot <slot>"
97   echo "                (slot-base is calculated to 1 as first slot)"
98   echo "list            Lists full storage slots"
99   echo "loaded          Gives slot from where the tape was loaded."
100   echo "                0 means the tape drive is empty."
101   echo "slots           Gives Number of aviable slots."
102   echo ""
103   echo "Example:"
104   echo "  mtx-changer /dev/changer load 1   loads a tape from slot 1"
105   echo ""
106   exit 2
107 }
108
109
110 case ${COMMAND} in
111     unload)
112         # enable the following line if you need to eject the cartridge
113         #mt -f ${TAPE} off
114         #sleep 2
115         # If the changer is power cycled with a tape loaded in a drive
116         if [ `${fullpath_me} ${CHANGER} loaded` -gt 0 ]; then
117                 free_slot=`${fullpath_me} ${CHANGER} loaded`
118                 free_slot=`expr $free_slot - 1`
119                 ${MTX} -f ${CHANGER} move drive ${DRIVE} slot $free_slot
120         fi
121         ;;
122
123     load)
124         ${MTX} -f ${CHANGER} move slot $((${SLOT}-1)) drive ${DRIVE}
125         rtn=$?
126         # Increase the sleep time if you have a slow device
127         sleep $SLEEP
128         exit $rtn
129         ;;
130
131     list)
132         if [ "${SIMULATE_BARCODE}" = "true" ]; then
133             if [ -f "$BARCODE_FILE" ]; then
134                 cat $BARCODE_FILE | grep -v "^#"
135                 exit 0
136             else
137                 echo "Barcode file $BARCODE_FILE missing ... exiting!"
138                 exit 1
139             fi
140         else
141             ${MTX} -f ${CHANGER} status | grep "^slot .*: .*FULL>" | awk '{print $2}' | awk -F: '{print $1+1" "}' | tr -d "[\r\n]"
142         fi
143       ;;
144
145     loaded)
146         # echo "Request loaded"
147         make_temp_file
148         ${MTX} -f ${CHANGER} status -S > ${TMPFILE}
149         rtn=$?
150         cat ${TMPFILE} | grep "^slot .: <ACCESS>" | awk '{print $2+1}' | tr -d ":"
151         drive=`cat ${TMPFILE}| grep "^drive .: <ACCESS>"`
152         if [ -n "$drive" ]; then
153                 echo 0
154         fi
155         rm -f ${TMPFILE}
156         exit $rtn
157         ;;
158
159     slots)
160         # echo "Request slots"
161         ${MTX} -f ${CHANGER} status | grep "^slot " | tail -1 | awk '{print $2+1}' | tr -d ":"
162       ;;
163
164     *)
165         usage
166       ;;
167 esac