3 # Bacula interface to virtual autoloader using disk storage
5 # Written by Kern Sibbald
7 # Copyright (C) 2000-2010 Free Software Foundation Europe e.V.
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 three of the GNU Affero General Public
13 # License as published by the Free Software Foundation, which is
14 # listed in the file LICENSE.
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.
21 # You should have received a copy of the GNU Affero 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
26 # Bacula® is a registered trademark of Kern Sibbald.
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.
32 # If you set in your Device resource
34 # Changer Command = "path-to-this-script/disk-changer %c %o %S %a %d"
35 # you will have the following input to this script:
37 # So Bacula will always call with all the following arguments, even though
38 # in come cases, not all are used. Note, the Volume name is not always
41 # disk-changer "changer-device" "command" "slot" "archive-device" "drive-index" "volume"
44 # By default the autochanger has 10 Volumes and 1 Drive.
46 # Note: For this script to work, you *must" specify
48 # in each of the Devices associated with your AutoChanger resource.
50 # changer-device is the name of a file that overrides the default
51 # volumes and drives. It may have:
52 # maxslot=n where n is one based (default 10)
53 # maxdrive=m where m is zero based (default 1 -- i.e. 2 drives)
55 # This code can also simulate barcodes. You simply put
56 # a list of the slots and barcodes in the "base" directory/barcodes.
57 # See below for the base directory definition. Example of a
59 # /var/bacula/barcodes
64 # archive-device is the name of the base directory where you want the
65 # Volumes stored appended with /drive0 for the first drive; /drive1
66 # for the second drive, ... For example, you might use
67 # /var/bacula/drive0 Note: you must not have a trailing slash, and
68 # the string (e.g. /drive0) must be unique, and it must not match
69 # any other part of the directory name. These restrictions could be
70 # easily removed by any clever script jockey.
72 # Full example: disk-changer /var/bacula/conf load 1 /var/bacula/drive0 0 TestVol001
74 # The Volumes will be created with names slot1, slot2, slot3, ... maxslot in the
75 # base directory. In the above example the base directory is /var/bacula.
76 # However, as with tapes, their Bacula Volume names will be stored inside the
77 # Volume label. In addition to the Volumes (e.g. /var/bacula/slot1,
78 # /var/bacula/slot3, ...) this script will create a /var/bacula/loadedn
79 # file to keep track of what Slot is loaded. You should not change this file.
81 # Modified 8 June 2010 to accept Volume names from the calling program as arg 6.
82 # In this case, rather than storing the data in slotn, it is stored in the
83 # Volume name. Note: for this to work, Volume names may not include spaces.
91 # to turn on logging, uncomment the following line
92 #touch $wd/disk-changer.log
94 dbgfile="$wd/disk-changer.log"
96 if test -f $dbgfile; then
97 echo "`date +\"%Y%m%d-%H:%M:%S\"` $*" >> $dbgfile
103 # Create a temporary file
106 TMPFILE=`mktemp -t mtx.XXXXXXXXXX`
107 if test x${TMPFILE} = x; then
108 TMPFILE="$wd/disk-changer.$$"
109 if test -f ${TMPFILE}; then
110 echo "Temp file security problem on: ${TMPFILE}"
116 # check parameter count on commandline
121 if test $pCount -lt $pCountNeed; then
122 echo "usage: disk-changer ctl-device command [slot archive-device drive-index]"
123 echo " Insufficient number of arguments arguments given."
124 if test $pCount -lt 2; then
125 echo " Mimimum usage is first two arguments ..."
127 echo " Command expected $pCountNeed arguments"
134 # Strip off the final name in order to get the Directory ($dir)
135 # that we are dealing with.
138 bn=`basename $device`
139 dir=`echo "$device" | sed -e s%/$bn%%g`
140 if [ ! -d $dir ]; then
141 echo "ERROR: Autochanger directory \"$dir\" does not exist."
142 echo " You must create it."
148 # Get the Volume name from the call line, or directly from
149 # the volslotn information.
154 if test "x$volume" != x && test "x$volume" != "x*NONE*" ; then
155 debug "touching $dir/$volume"
157 echo "$volume" >$dir/volslot${slot}
159 elif [ -f $dir/volslot${slot} ]; then
160 volume=`cat $dir/volslot${slot}`
184 # Check for special cases where only 2 arguments are needed,
185 # all others are a minimum of 5
189 check_parm_count $# 2
192 check_parm_count $# 2
195 check_parm_count $# 4
196 if [ $slot -gt $maxslot ]; then
197 echo "Slot ($slot) out of range (1-$maxslot)"
202 check_parm_count $# 5
203 if [ $drive -gt $maxdrive ]; then
204 echo "Drive ($drive) out of range (0-$maxdrive)"
207 if [ $slot -gt $maxslot ]; then
208 echo "Slot ($slot) out of range (1-$maxslot)"
215 debug "Parms: $ctl $cmd $slot $device $drive $volume $havevol"
219 debug "Doing disk -f $ctl unload $slot $device $drive $volume"
221 if [ -f $dir/loaded${drive} ]; then
222 ld=`cat $dir/loaded${drive}`
224 echo "Storage Element $slot is Already Full"
227 if [ $slot -eq $ld ]; then
228 echo "0" >$dir/loaded${drive}
229 unlink $device 2>/dev/null >/dev/null
232 echo "Storage Element $slot is Already Full"
238 debug "Doing disk $ctl load $slot $device $drive $volume"
241 while [ $i -le $maxdrive ]; do
242 if [ -f $dir/loaded${i} ]; then
243 ld=`cat $dir/loaded${i}`
247 if [ $ld -eq $slot ]; then
248 echo "Drive ${i} Full (Storage element ${ld} loaded)"
253 # Check if we have a Volume name
255 if [ $havevol -eq 0 ]; then
256 # check if slot exists
257 if [ ! -f $dir/slot${slot} ] ; then
258 echo "source Element Address $slot is Empty"
262 if [ -f $dir/loaded${drive} ]; then
263 ld=`cat $dir/loaded${drive}`
267 if [ $ld -ne 0 ]; then
268 echo "Drive ${drive} Full (Storage element ${ld} loaded)"
271 echo "0" >$dir/loaded${drive}
272 unlink $device 2>/dev/null >/dev/null
274 if [ $havevol -ne 0 ]; then
275 ln -s $dir/$volume $device
278 ln -s $dir/slot${slot} $device
281 if [ $rtn -eq 0 ]; then
282 echo $slot >$dir/loaded${drive}
288 debug "Doing disk -f $ctl -- to list volumes"
290 if [ -f $dir/barcodes ]; then
294 while [ $i -le $maxslot ]; do
298 if [ $havevol -eq 0 ]; then
310 # ***FIXME*** must add new Volume stuff
312 debug "Doing disk -f $ctl -- to list volumes"
314 if [ ! -f $dir/barcodes ]; then
318 # we print drive content seen by autochanger
319 # and we also remove loaded media from the barcode list
321 while [ $i -le $maxdrive ]; do
322 if [ -f $dir/loaded${i} ]; then
323 ld=`cat $dir/loaded${i}`
324 v=`awk -F: "/^$ld:/"' { print $2 }' $dir/barcodes`
326 echo "^$ld:" >> $TMPFILE
331 # Empty slots are not in barcodes file
332 # When we detect a gap, we print missing rows as empty
333 # At the end, we fill the gap between the last entry and maxslot
334 grep -v -f $TMPFILE $dir/barcodes | sort -n | \
335 perl -ne 'BEGIN { $cur=1 }
347 END { while ($cur < '"$maxslot"') { print "S:$cur:E\n"; $cur++; } } '
353 # ***FIXME*** must add new Volume stuff
357 if [ -f $dir/slot{$slotdest} ]; then
358 echo "destination Element Address $slot is Full"
361 if [ ! -f $dir/slot${slot} ] ; then
362 echo "source Element Address $slot is Empty"
366 echo "Transfering $slot to $slotdest"
367 mv $dir/slot${slot} $dir/slot{$slotdest}
369 if [ -f $dir/barcodes ]; then
370 sed "s/^$slot:/$slotdest:/" > $TMPFILE
371 sort -n $TMPFILE > $dir/barcodes
376 debug "Doing disk -f $ctl $drive -- to find what is loaded"
378 if [ -f $dir/loaded${drive} ]; then
379 cat $dir/loaded${drive}
387 debug "Doing disk -f $ctl -- to get count of slots"