]> git.sur5r.net Git - bacula/bacula/blob - bacula/examples/autochangers/rc-chio-changer
7007842b48646b357b30e366001038733498ae7b
[bacula/bacula] / bacula / examples / autochangers / rc-chio-changer
1 #!/bin/sh
2 #
3 # Bacula interface to chio autoloader
4 # (by Rudolf Cejka <cejkar@fit.vutbr.cz>)
5 #
6 # $Id$
7 #
8 # If you set in your Device resource
9 #   Changer Command = "path-to-this-script/chio-changer %c %o %S %a %d"
10 # you will have the following input to this script:
11 #   chio-changer "changer-device" "command" "slot" "tape-device" "drive-index"
12 #                       $1           $2       $3         $4            $5
13 # for example (on a FreeBSD system):
14 #   chio-changer /dev/ch0 load 1 /dev/nsa0 0
15 #
16 # If you change the script, take care to return either the chio exit
17 # code or a 0. If the script exits with a non-zero exit code, Bacula
18 # will assume the request failed.
19 #
20
21 # Uncomment the following line, if you need to eject a tape before moving
22 # it from the drive.
23 #OFFLINE=yes
24
25 # Uncomment the following line, if you need to wait for some time
26 # (in seconds) after (un)loading a tape.
27 #SLEEP=10
28
29 # Uncomment the following line, if you do not have a changer with volume
30 # reader.
31 #FAKE_BARCODES=/usr/local/etc/bacula-barcodes
32
33 PROGNAME=`basename $0`
34
35 usage()
36 {
37   cat <<EOF
38 Usage: ${PROGNAME} <changer-device> <cmd> [slot] [tape-device] [drive-index]
39
40 Commands (<cmd>):
41   unload          Unloads a tape into the slot from where it was loaded.
42   load <slot>     Loads a tape from the slot <slot> (1-based).
43   list            Lists full storage slots.
44   loaded          Gives slot from where the tape was loaded (0 = empty drive).
45   slots           Gives number of available slots.
46
47 Example:
48   ${PROGNAME} /dev/ch0 load 1        Loads a tape from first slot 1.
49
50 EOF
51 }
52
53 # Default settings
54 CHANGER=/dev/ch0
55 TAPE=/dev/nsa0
56 DRIVE=0
57
58 CHIO=/bin/chio
59 MT=/usr/bin/mt
60
61 if [ $# -lt 2 ]; then
62   usage
63   exit 1
64 fi
65
66 if [ -n "$1" ]; then
67   CHANGER=$1;
68 fi
69 COMMAND=$2
70 SLOT=$3
71 if [ -n "$4" ]; then
72   TAPE=$4
73 fi
74 if [ -n "$5" ]; then
75   DRIVE=$5
76 fi
77
78 case ${COMMAND} in
79 unload)
80   if [ "${OFFLINE}" = yes ]; then
81     ${MT} -f ${TAPE} offline
82   fi
83   if [ -n "${SLEEP}" ]; then
84     sleep ${SLEEP}
85   fi
86   if [ -z "${SLOT}" ]; then
87     ${CHIO} -f ${CHANGER} return drive ${DRIVE}
88   else
89     ${CHIO} -f ${CHANGER} move drive ${DRIVE} slot $((${SLOT} - 1))
90   fi
91   if [ $? -ne 0 ]; then
92     # Try to unload the cartridge to the first free slot.
93     FREE=`${CHIO} -f ${CHANGER} status slot | \
94       sed -ne '/FULL/d;s/^slot *\([0-9]*\):.*/\1/p' | head -1`
95     if [ -n "${FREE}" ]; then
96       ${CHIO} -f ${CHANGER} move drive ${DRIVE} slot ${FREE}
97     else
98       exit 1
99     fi
100   fi
101   ;;
102 load)
103   ${CHIO} -f ${CHANGER} move slot $((${SLOT} - 1)) drive ${DRIVE}
104   RET=$?
105   if [ -n "${SLEEP}" ]; then
106     sleep ${SLEEP}
107   fi
108   exit ${RET}
109   ;;
110 list)
111   if [ -z "${FAKE_BARCODES}" ]; then
112     ${CHIO} -f ${CHANGER} status -v slot | \
113       sed -ne 's/^slot *\([0-9]*:\).*FULL.*voltag.*<\(.*\):.*/\1\2/p' | \
114       awk -F: '{print $1 + 1 ":" $2 }'
115   else
116     if [ -f "${FAKE_BARCODES}" ]; then
117       grep -v -e "^#" -e "^$" < ${FAKE_BARCODES}
118     else
119       echo "${PROGNAME}: Barcode file ${FAKE_BARCODES} is missing"
120       exit 1
121     fi
122   fi
123   ;;
124 loaded)
125   FREE=`${CHIO} -f ${CHANGER} status slot | \
126     sed -ne '/FULL/d;s/^slot *\([0-9]*\):.*/\1/p' | \
127     awk 'BEGIN { n = 0 } { n = $1 + 1 ; exit } END { print n }'`
128   ${CHIO} -f ${CHANGER} status -S drive | \
129     sed -ne 's/^drive *'${DRIVE}':.*FULL.*source.*<[^0-9]*\([0-9]*\)>.*/\1/p' \
130     | awk 'BEGIN { n = 0 } { n = ($1 == "") ? '${FREE}' : $1 + 1 } \
131       END { print n }'
132   ;;
133 slots)
134   ${CHIO} -f ${CHANGER} status | grep -c "^slot "
135   ;;
136 *)
137   usage
138   ;;
139 esac