]> git.sur5r.net Git - bacula/bacula/blob - bacula/scripts/disk-changer.in
Minor tweaks
[bacula/bacula] / bacula / scripts / disk-changer.in
1 #!/bin/sh
2 #
3 # Bacula interface to virtual autoloader using disk storage
4 #
5 #  Written by Kern Sibbald
6 #
7 #  Copyright (C) 2000-2007 Free Software Foundation Europe e.V.
8 #
9 #  The main author of Bacula is Kern Sibbald, with contributions from
10 #  many others, a complete list can be found in the file AUTHORS.
11 #  This program is Free Software; you can redistribute it and/or
12 #  modify it under the terms of version two of the GNU General Public
13 #  License as published by the Free Software Foundation, which is 
14 #  listed in the file LICENSE.
15 3
16 #  This program is distributed in the hope that it will be useful, but
17 #  WITHOUT ANY WARRANTY; without even the implied warranty of
18 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 #  General Public License for more details.
20 3
21 #  You should have received a copy of the GNU General Public License
22 #  along with this program; if not, write to the Free Software
23 #  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 #  02110-1301, USA.
25 #
26 #  Bacula® is a registered trademark of John Walker.
27 #  The licensor of Bacula is the Free Software Foundation Europe
28 #  (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
29 #  Switzerland, email:ftf@fsfeurope.org.
30 #
31 #
32 #  $Id$
33 #
34 #  If you set in your Device resource
35 #
36 #  Changer Command = "path-to-this-script/disk-changer %c %o %S %a %d"
37 #    you will have the following input to this script:
38 #
39 #  So Bacula will always call with all the following arguments, even though
40 #    in come cases, not all are used.
41 #
42 #  disk-changer "changer-device" "command" "slot" "archive-device" "drive-index"
43 #                   $1              $2       $3        $4               $5
44 #
45 # By default the autochanger has 10 Volumes and 1 Drive.
46 #
47 # Note: For this script to work, you *must" specify
48 #    Device Type = File 
49 # in each of the Devices associated with your AutoChanger resource.
50 #
51 # changer-device is the name of a file that overrides the default
52 #   volumes and drives.  It may have:
53 #        maxslot=n   where n is one based (default 10)
54 #        maxdrive=m  where m is zero based (default 1 -- i.e. 2 drives)
55 #  
56 #   This code can also simulate barcodes. You simply put
57 #   a list of the slots and barcodes in the "base" directory/barcodes.
58 #   See below for the base directory definition.  Example of a 
59 #   barcodes file:
60 #      /var/bacula/barcodes
61 #      1:Vol001
62 #      2:Vol002
63 #      ...
64
65 # archive-device is the name of the base directory where you want the
66 #  Volumes stored appended with /drive0 for the first drive; /drive1
67 #  for the second drive, ... For example, you might use
68 #  /var/bacula/drive0  Note: you must not have a trailing slash, and
69 #  the string (e.g. /drive0) must be unique, and it must not match
70 #  any other part of the directory name. These restrictions could be
71 #  easily removed by any clever script jockey.
72 #
73 #  Full example: disk-changer /var/bacula/conf load 1 /var/bacula/drive0 0
74 #
75 # The Volumes will be created with names slot1, slot2, slot3, ... maxslot in the
76 #  base directory. In the above example the base directory is /var/bacula.
77 #  However, as with tapes, their Bacula Volume names will be stored inside the
78 #  Volume label. In addition to the Volumes (e.g. /var/bacula/slot1, 
79 #  /var/bacula/slot3, ...) this script will create a /var/bacula/loadedn
80 #  file to keep track of what Slot is loaded. You should not change this file.
81 #
82 #
83
84 wd=@working_dir@
85
86 #
87 # log whats done
88 #
89 # to turn on logging, uncomment the following line
90 #touch $wd/disk-changer.log
91 #
92 dbgfile="$wd/disk-changer.log"
93 debug() {
94     if test -f $dbgfile; then
95         echo "`date +\"%Y%m%d-%H:%M:%S\"` $*" >> $dbgfile
96     fi
97 }
98
99
100 #
101 # Create a temporary file
102 #
103 make_temp_file() {
104   TMPFILE=`mktemp -t mtx.XXXXXXXXXX`
105   if test x${TMPFILE} = x; then
106      TMPFILE="$wd/disk-changer.$$"
107      if test -f ${TMPFILE}; then
108         echo "Temp file security problem on: ${TMPFILE}"
109         exit 1
110      fi
111   fi
112 }
113
114 # check parameter count on commandline
115 #
116 check_parm_count() {
117     pCount=$1
118     pCountNeed=$2
119     if test $pCount -lt $pCountNeed; then
120         echo "usage: disk-changer ctl-device command [slot archive-device drive-index]"
121         echo "  Insufficient number of arguments arguments given."
122         if test $pCount -lt 2; then
123             echo "  Mimimum usage is first two arguments ..."
124         else
125             echo "  Command expected $pCountNeed arguments"
126         fi
127         exit 1
128     fi
129 }
130
131 #
132 # Strip off the final name in order to get the Directory ($dir)
133 #  that we are dealing with.
134 #
135 get_dir() {
136    bn=`basename $device`
137    dir=`echo "$device" | sed -e s%/$bn%%g`
138    if [ ! -d $dir ]; then
139       echo "ERROR: Autochanger directory \"$dir\" does not exist.\n"
140       echo "       You must create it.\n"
141       exit 1
142    fi
143 }
144
145
146 # Setup arguments
147 ctl=$1
148 cmd="$2"
149 slot=$3
150 device=$4
151 drive=$5
152
153 # set defaults
154 maxdrive=1
155 maxslot=10
156
157 # Pull in conf file
158 if [ -f $ctl ]; then 
159    . $ctl
160 fi
161
162
163 # Check for special cases where only 2 arguments are needed, 
164 #  all others are a minimum of 5
165 #
166 case $2 in
167     list)
168         check_parm_count $# 2
169         ;;
170     slots)
171         check_parm_count $# 2
172         ;;
173     *)
174         check_parm_count $# 5
175         if [ $drive -gt $maxdrive ]; then
176            echo "Drive ($drive) out of range (0-$maxdrive)"
177            exit 1
178         fi
179         if [ $slot -gt $maxslot ]; then
180            echo "Slot ($slot) out of range (1-$maxslot)"
181            exit 1
182         fi
183         ;;
184 esac
185
186
187
188 debug "Parms: $ctl $cmd $slot $device $drive"
189
190 case $cmd in 
191    unload)
192       debug "Doing disk -f $ctl unload $slot $device $drive"
193       get_dir
194       ld=`cat $dir/loaded${drive}`
195       if [ $slot -eq $ld ]; then
196          echo "0" >$dir/loaded${drive}
197          unlink $device 2>/dev/null >/dev/null
198          rm -f $device
199       else
200          echo "Storage Element $slot is Already Full"
201          exit 1
202       fi
203       ;;
204
205    load)
206       debug "Doing disk $ctl load $slot $device $drive"
207       get_dir
208       if [ -f $dir/loaded${drive} ]; then
209          ld=`cat $dir/loaded${drive}`
210       else
211          ld=0
212       fi
213       if [ $ld -eq 0 ]; then
214          echo "0" >$dir/loaded${drive}
215          unlink $device 2>/dev/null >/dev/null
216          rm -f $device
217          ln -s $dir/slot${slot} $device
218          rtn=$?
219          if [ $rtn -eq 0 ]; then
220             echo $slot >$dir/loaded${drive}
221          fi
222          exit $rtn
223       else
224          echo "Drive ${drive} Full (Storage element ${ld} loaded)"
225          exit 1
226       fi
227       ;;
228
229    list) 
230       debug "Doing disk -f $ctl -- to list volumes"
231       get_dir 
232       if [ -f $dir/barcodes ]; then
233          cat $dir/barcodes
234       else
235          i=1
236          while [ $i -le $maxslot ]; do
237             echo "$i:"
238             i=`expr $i + 1`
239          done
240       fi
241       exit 0
242       ;;
243
244    loaded)
245       debug "Doing disk -f $ctl $drive -- to find what is loaded"
246       get_dir
247       if [ -f $dir/loaded${drive} ]; then
248          cat $dir/loaded${drive}
249       else
250          echo "0"
251       fi
252       exit
253       ;;
254
255    slots)
256       debug "Doing disk -f $ctl -- to get count of slots"
257       echo $maxslot
258       ;;
259 esac