]> git.sur5r.net Git - bacula/bacula/blob - bacula/examples/autochangers/rc-chio-changer
829a5ba6982f739f3a1ed2a755ad287b2ec6b74a
[bacula/bacula] / bacula / examples / autochangers / rc-chio-changer
1 #!/bin/sh
2 #
3 # Bacula interface to chio autoloader
4 #
5 # This script was written by Rudolf Cejka
6 #   I'm sending rewrite of examples/autochangers/chio-bacula for
7 #   FreeBSD under name chio-changer, which tries to save all features
8 #   from original code and add the possibility to list real barcodes
9 #   from library.  I hope that this version is somewhat nicer.
10 #
11 #
12 # $Id$
13 #
14 # If you set in your Device resource
15 #   Changer Command = "path-to-this-script/chio-changer %c %o %S %a %d"
16 # you will have the following input to this script:
17 #   chio-changer "changer-device" "command" "slot" "tape-device" "drive-index"
18 #                       $1           $2       $3         $4            $5
19 # for example (on a FreeBSD system):
20 #   chio-changer /dev/ch0 load 1 /dev/nsa0 0
21 #
22 # If you need an offline, refer to the drive as $4, for example:
23 #   mt -f $4 offline
24 #
25 # Many changers need an offline before the chio unload.
26 # Also many changers need to sleep some time after the chio load.
27 #
28 # If you change the script, take care to return either the chio exit
29 # code or a 0. If the script exits with a non-zero exit code, Bacula
30 # will assume the request failed.
31 #
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 # This simulates a barcode reader in the changer:
54 #FAKE_BARCODES=/usr/local/etc/bacula-barcodes
55
56 # Time to wait for (un)loading
57 SLEEP=10
58
59 # Default settings
60 CHANGER=/dev/ch0
61 TAPE=/dev/nsa0
62 DRIVE=0
63
64 CHIO=/bin/chio
65
66 if [ $# -lt 2 ]; then
67   usage
68   exit 1
69 fi
70
71 if [ -n "$1" ]; then
72   CHANGER=$1;
73 fi
74 COMMAND=$2
75 SLOT=$3
76 if [ "${SLOT}" = slot ]; then
77   # btape says "... slot 1 drive 0"
78   shift
79   SLOT=$3
80 fi
81 if [ -n "$4" ]; then
82   TAPE=$4
83 fi
84 if [ -n "$5" ]; then
85   DRIVE=$5
86 fi
87
88 case ${COMMAND} in
89 unload)
90   # Enable the following line(s) if you need to eject the cartridge.
91   #mt -f ${TAPE} offline
92   #sleep ${SLEEP}
93   if [ -z "${SLOT}" ]; then
94     ${CHIO} -f ${CHANGER} return drive ${DRIVE}
95   else
96     ${CHIO} -f ${CHANGER} move drive ${DRIVE} slot $((${SLOT} - 1))
97   fi
98   if [ $? -ne 0 ]; then
99     # Try to unload the cartridge to the first free slot.
100     FREE=`${CHIO} -f ${CHANGER} status slot | \
101       sed -ne '/FULL/d;s/^slot *\([0-9]*\):.*/\1/p' | head -1`
102     if [ -n "${FREE}" ]; then
103       ${CHIO} -f ${CHANGER} move drive ${DRIVE} slot ${FREE}
104     else
105       exit 1
106     fi
107   fi
108   ;;
109 load)
110   ${CHIO} -f ${CHANGER} move slot $((${SLOT} - 1)) drive ${DRIVE}
111   # Enable the following line if you need to wait after chio load.
112   #RET=$? ; sleep ${SLEEP} ; exit ${RET}
113   ;;
114 list)
115   if [ -z "${FAKE_BARCODES}" ]; then
116     ${CHIO} -f ${CHANGER} status -v slot | \
117       sed -ne 's/^slot *\([0-9]*:\).*FULL.*voltag.*<\(.*\):.*/\1\2/p' | \
118       awk -F: '{print $1 + 1 ":" $2 }'
119   else
120     if [ -f "${FAKE_BARCODES}" ]; then
121       grep -v -e "^#" -e "^$" < ${FAKE_BARCODES}
122     else
123       echo "${PROGNAME}: Barcode file ${FAKE_BARCODES} is missing"
124       exit 1
125     fi
126   fi
127   ;;
128 loaded)
129   FREE=`${CHIO} -f ${CHANGER} status slot | \
130     sed -ne '/FULL/d;s/^slot *\([0-9]*\):.*/\1/p' | \
131     awk 'BEGIN { n = 0 } { n = $1 + 1 ; exit } END { print n }'`
132   ${CHIO} -f ${CHANGER} status -S drive | \
133     sed -ne 's/^drive *'${DRIVE}':.*FULL.*source.*<[^0-9]*\([0-9]*\)>.*/\1/p' \
134     | awk 'BEGIN { n = 0 } { n = ($1 == "") ? '${FREE}' : $1 + 1 } \
135       END { print n }'
136   ;;
137 slots)
138   ${CHIO} -f ${CHANGER} status | grep -c "^slot "
139   ;;
140 *)
141   usage
142   ;;
143 esac