3 # Bacula interface to chio autoloader
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.
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"
19 # for example (on a FreeBSD system):
20 # chio-changer /dev/ch0 load 1 /dev/nsa0 0
22 # If you need an offline, refer to the drive as $4, for example:
25 # Many changers need an offline before the chio unload.
26 # Also many changers need to sleep some time after the chio load.
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.
33 PROGNAME=`basename $0`
38 Usage: ${PROGNAME} <changer-device> <cmd> [slot] [tape-device] [drive-index]
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.
48 ${PROGNAME} /dev/ch0 load 1 Loads a tape from first slot 1.
53 # This simulates a barcode reader in the changer:
54 #FAKE_BARCODES=/usr/local/etc/bacula-barcodes
56 # Time to wait for (un)loading
76 if [ "${SLOT}" = slot ]; then
77 # btape says "... slot 1 drive 0"
90 # Enable the following line(s) if you need to eject the cartridge.
91 #mt -f ${TAPE} offline
93 if [ -z "${SLOT}" ]; then
94 ${CHIO} -f ${CHANGER} return drive ${DRIVE}
96 ${CHIO} -f ${CHANGER} move drive ${DRIVE} slot $((${SLOT} - 1))
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}
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}
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 }'
120 if [ -f "${FAKE_BARCODES}" ]; then
121 grep -v -e "^#" -e "^$" < ${FAKE_BARCODES}
123 echo "${PROGNAME}: Barcode file ${FAKE_BARCODES} is missing"
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 } \
138 ${CHIO} -f ${CHANGER} status | grep -c "^slot "