]> git.sur5r.net Git - bacula/bacula/blob - bacula/examples/autochangers/chio-changer-openbsd
Apply patch for chio-changer-openbsd from bug #1984 -- Implements listall
[bacula/bacula] / bacula / examples / autochangers / chio-changer-openbsd
1 #!/bin/sh
2 #
3 # Bacula interface to chio(1) autoloader for OpenBSD
4 #
5 # Adapted from NetBSD pkgsrc and examples/autochangers in bacula source)
6 # by Antoine Jacoutot <ajacoutot@openbsd.org> for OpenBSD.
7 # Tested on an LTO-4 device with 1 drive and 8 slots.
8 # The user Bacula is running as needs rw access to the ch(4) and st(4)
9 # devices.
10 #
11 # If you set in your Device resource:
12 #      Changer Command = "/path/to/chio-changer-openbsd %c %o %S %a %d"
13 # you will have the following input to this script:
14 #      chio-changer-openbsd "changer-device" "command" "slot" "archive-device" "drive-index"
15 #                            $1               $2        $3     $4               $5
16 #
17 # So Bacula will always call with all the following arguments, even though
18 #     in come cases, not all are used.
19 #
20 # N.B. If you change the script, take care to return either 
21 #      the chio exit code or a 0. If the script exits with a non-zero
22 #      exit code, Bacula will assume the request failed.
23
24 # time (in seconds) for the unit to settle after (un)loading a tape
25 SLEEP=1
26
27 CHIO=/bin/chio
28
29 usage() {
30         echo "usage: ${0##*/} ctl-device command [slot archive-device drive-index]"
31 }
32
33 # check parameters count
34 check_parm_count() {
35         pCount=$1
36         pCountNeed=$2
37         if test ${pCount} -lt ${pCountNeed}; then
38                 usage
39                 echo "!!! insufficient number of arguments given"
40                 exit 1
41         if test ${pCount} -lt 2; then
42                 usage
43                 echo "!!! mimimum usage is the first two arguments"
44                 exit 1
45         else
46                 usage
47                 echo "!!! command expected ${pCountNeed} arguments"
48                 exit 1
49         fi
50                 usage
51                 exit 1
52         fi
53 }
54
55 # check arguments count for specific actions
56 case $2 in
57         list|listall)
58                 check_parm_count $# 2
59                 ;;
60         slots)
61                 check_parm_count $# 2
62                 ;;
63         transfer)
64                 check_parm_count $# 4
65                 ;;
66         *)
67                 check_parm_count $# 5
68                 ;;
69 esac
70
71
72 # get arguments
73 ctl=$1
74 cmd="$2"
75 slot=$3
76 device=$4
77 drive=$5
78
79 case ${cmd} in 
80         unload)
81                 ${CHIO} -f ${ctl} move drive ${drive} slot $((${slot} - 1))
82                 rtn=$?
83                 [ ${rtn} -eq 0 ] && sleep ${SLEEP}
84                 exit ${rtn}
85                 ;;
86         load)
87                 ${CHIO} -f ${ctl} move slot $((${slot} - 1)) drive ${drive}
88                 rtn=$?
89                 [ ${rtn} -eq 0 ] && sleep ${SLEEP}
90                 exit ${rtn}
91                 ;;
92         list)
93                 ${CHIO} -f ${ctl} status -v slot | \
94                         sed -ne 's/^slot *\([0-9]*:\).*FULL.*voltag.*<\(.*\):.*/\1\2/p' | \
95                         awk -F: '{ print $1 + 1 ":" $2 }'
96                 exit $?
97                 ;;
98         listall)
99                 # XXX only one drive is queried
100                 _list=$(${0} ${1} list)
101                 _loaded_s=$(${0} ${1} loaded ${slot} ${device} ${drive})
102                 _loaded_t=$(${CHIO} -f ${ctl} status -v | grep "drive ${drive}" | awk '{ print $NF }' | sed -e 's,<,,' -e 's,:.*,,')
103
104                 [ -n "${_list}" -a -n "${_loaded_s}" -a -n "${_loaded_t}" ] || exit 1
105
106                 (for i in ${_list}; do
107                         echo "S:${i}" | sed 's/\(.*\):/\1:F:/'
108                 done
109                 echo S:${_loaded_s}:E
110                 if [ "${_loaded_s}" -ne 0 ]; then
111                         echo D:${drive}:F:${_loaded_s}:${_loaded_t}
112                 else
113                         echo D:${drive}:E
114                 fi) | sort
115                 ;;
116         loaded)
117                 # XXX output the first empty slot if the drive is loaded
118                 _slot=$(${CHIO} -f ${ctl} status -v | egrep '^slot.*<ACCESS> voltag: <:[0-9]>$' | awk '{ print $2 }' | awk -F: '{ print $1 + 1 }')
119                 rtn=$?
120                 _loaded=$(${CHIO} -f ${ctl} status -v | egrep "^drive ${drive}: <ACCESS,FULL> voltag: <.*:[0-9]>")
121                 [ -z "${_slot}" -o -z "${_loaded}" ] && _slot=0
122                 echo ${_slot} | awk '{ print $1 }'
123                 exit ${rtn}
124                 ;;
125         slots)
126                 ${CHIO} -f ${ctl} params | awk "/slots/{print \$2}"
127                 exit $?
128                 ;;
129         transfer)
130                 slotdest=${device}
131                 ${CHIO} -f ${ctl} move slot $((${slot} - 1)) slot ${slotdest}
132                 exit $?
133                 ;;
134 esac