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