]> git.sur5r.net Git - bacula/bacula/blob - bacula/scripts/disk-changer.in
Correct improperly formated list command output reported by Dan.
[bacula/bacula] / bacula / scripts / disk-changer.in
1 #!/bin/sh
2 #
3 # Bacula interface to virtual autoloader using disk storage
4 #
5 #  $Id$
6 #
7 #  If you set in your Device resource
8 #
9 #  Changer Command = "path-to-this-script/disk-changer %c %o %S %a %d"
10 #    you will have the following input to this script:
11 #
12 #  So Bacula will always call with all the following arguments, even though
13 #    in come cases, not all are used.
14 #
15 #  disk-changer "changer-device" "command" "slot" "archive-device" "drive-index"
16 #                   $1              $2       $3        $4               $5
17 #
18 # By default the autochanger has 10 Volumes and 1 Drive.
19 #
20 # Note: For this script to work, you *must" specify
21 #    Device Type = File 
22 # in each of the Devices associated with your AutoChanger resource.
23 #
24 # changer-device is the name of a file that overrides the default
25 #   volumes and drives.  It may have:
26 #        maxslot=n   where n is one based (default 10)
27 #        maxdrive=m  where m is zero based (default 1 -- i.e. 2 drives)
28 #  
29 #   This code can also simulate barcodes. You simply put
30 #   a list of the slots and barcodes in the "base" directory/barcodes.
31 #   See below for the base directory definition.  Example of a 
32 #   barcodes file:
33 #      /var/bacula/barcodes
34 #      1:Vol001
35 #      2:Vol002
36 #      ...
37
38 # archive-device is the name of the base directory where you want the
39 #  Volumes stored appended with /drive0 for the first drive; /drive1
40 #  for the second drive, ... For example, you might use
41 #  /var/bacula/drive0  Note: you must not have a trailing slash, and
42 #  the string (e.g. /drive0) must be unique, and it must not match
43 #  any other part of the directory name. These restrictions could be
44 #  easily removed by any clever script jockey.
45 #
46 #  Full example: disk-changer /var/bacula/conf load 1 /var/bacula/drive0 0
47 #
48 # The Volumes will be created with names slot1, slot2, slot3, ... maxslot in the
49 #  base directory. In the above example the base directory is /var/bacula.
50 #  However, as with tapes, their Bacula Volume names will be stored inside the
51 #  Volume label. In addition to the Volumes (e.g. /var/bacula/slot1, 
52 #  /var/bacula/slot3, ...) this script will create a /var/bacula/loadedn
53 #  file to keep track of what Slot is loaded. You should not change this file.
54 #
55 #
56
57 wd=@working_dir@
58
59 #
60 # log whats done
61 #
62 # to turn on logging, uncomment the following line
63 #touch $wd/disk-changer.log
64 #
65 dbgfile="$wd/disk-changer.log"
66 debug() {
67     if test -e $dbgfile; then
68         echo "`date +\"%Y%m%d-%H:%M:%S\"` $*" >> $dbgfile
69     fi
70 }
71
72
73 #
74 # Create a temporary file
75 #
76 make_temp_file() {
77   TMPFILE=`mktemp -t mtx.XXXXXXXXXX`
78   if test x${TMPFILE} = x; then
79      TMPFILE="$wd/disk-changer.$$"
80      if test -f ${TMPFILE}; then
81         echo "Temp file security problem on: ${TMPFILE}"
82         exit 1
83      fi
84   fi
85 }
86
87 # check parameter count on commandline
88 #
89 check_parm_count() {
90     pCount=$1
91     pCountNeed=$2
92     if test $pCount -lt $pCountNeed; then
93         echo "usage: disk-changer ctl-device command [slot archive-device drive-index]"
94         echo "  Insufficient number of arguments arguments given."
95         if test $pCount -lt 2; then
96             echo "  Mimimum usage is first two arguments ..."
97         else
98             echo "  Command expected $pCountNeed arguments"
99         fi
100         exit 1
101     fi
102 }
103
104 #
105 # Strip off the final name in order to get the Directory ($dir)
106 #  that we are dealing with.
107 #
108 get_dir() {
109    bn=`basename $device`
110    dir=`echo "$device" | sed -e s%/$bn%%g`
111    if [ ! -d $dir ]; then
112       echo "ERROR: Autochanger directory \"$dir\" does not exist.\n"
113       echo "       You must create it.\n"
114       exit 1
115    fi
116 }
117
118
119 # Setup arguments
120 ctl=$1
121 cmd="$2"
122 slot=$3
123 device=$4
124 drive=$5
125
126 # set defaults
127 maxdrive=1
128 maxslot=10
129
130 # Pull in conf file
131 if [ -f $ctl ]; then 
132    . $ctl
133 fi
134
135
136 # Check for special cases where only 2 arguments are needed, 
137 #  all others are a minimum of 5
138 #
139 case $2 in
140     list)
141         check_parm_count $# 2
142         ;;
143     slots)
144         check_parm_count $# 2
145         ;;
146     *)
147         check_parm_count $# 5
148         if [ $drive -gt $maxdrive ]; then
149            echo "Drive ($drive) out of range (0-$maxdrive)"
150            exit 1
151         fi
152         if [ $slot -gt $maxslot ]; then
153            echo "Slot ($slot) out of range (1-$maxslot)"
154            exit 1
155         fi
156         ;;
157 esac
158
159
160
161 debug "Parms: $ctl $cmd $slot $device $drive"
162
163 case $cmd in 
164    unload)
165       debug "Doing disk -f $ctl unload $slot $device $drive"
166       get_dir
167       echo "0" >$dir/loaded${drive}
168       unlink $device 2>/dev/null >/dev/null
169       rm -f $device
170       ;;
171
172    load)
173       debug "Doing disk $ctl load $slot $device $drive"
174       get_dir
175       echo "0" >$dir/loaded${drive}
176       unlink $device 2>/dev/null >/dev/null
177       rm -f $device
178       ln -s $dir/slot${slot} $device
179       rtn=$?
180       if [ $rtn -eq 0 ]; then
181          echo $slot >$dir/loaded${drive}
182       fi
183       exit $rtn
184       ;;
185
186    list) 
187       debug "Doing disk -f $ctl -- to list volumes"
188       get_dir 
189       if [ -f $dir/barcodes ]; then
190          cat $dir/barcodes
191       else
192          i=1
193          while [ $i -le $maxslot ]; do
194             echo "$i:"
195             i=`expr $i + 1`
196          done
197       fi
198       exit 0
199       ;;
200
201    loaded)
202       debug "Doing disk -f $ctl $drive -- to find what is loaded"
203       get_dir
204       if [ -f $dir/loaded${drive} ]; then
205          cat $dir/loaded${drive}
206       else
207          echo "0"
208       fi
209       exit
210       ;;
211
212    slots)
213       debug "Doing disk -f $ctl -- to get count of slots"
214       echo $maxslot
215       ;;
216 esac