3 # Bacula interface to virtual autoloader using disk storage
5 # Written by Kern Sibbald
7 # Bacula® - The Network Backup Solution
9 # Copyright (C) 2000-2014 Free Software Foundation Europe e.V.
11 # The main author of Bacula is Kern Sibbald, with contributions from many
12 # others, a complete list can be found in the file AUTHORS.
14 # You may use this file and others of this release according to the
15 # license defined in the LICENSE file, which includes the Affero General
16 # Public License, v3.0 ("AGPLv3") and some additional permissions and
17 # terms pursuant to its AGPLv3 Section 7.
19 # Bacula® is a registered trademark of Kern Sibbald.
22 # If you set in your Device resource
24 # Changer Command = "path-to-this-script/disk-changer %c %o %S %a %d"
25 # you will have the following input to this script:
27 # So Bacula will always call with all the following arguments, even though
28 # in come cases, not all are used. Note, the Volume name is not always
31 # disk-changer "changer-device" "command" "slot" "archive-device" "drive-index" "volume"
34 # By default the autochanger has 10 Volumes and 1 Drive.
36 # Note: For this script to work, you *must" specify
38 # in each of the Devices associated with your AutoChanger resource.
40 # changer-device is the name of a file that overrides the default
41 # volumes and drives. It may have:
42 # maxslot=n where n is one based (default 10)
43 # maxdrive=m where m is zero based (default 1 -- i.e. 2 drives)
45 # This code can also simulate barcodes. You simply put
46 # a list of the slots and barcodes in the "base" directory/barcodes.
47 # See below for the base directory definition. Example of a
49 # /var/bacula/barcodes
54 # archive-device is the name of the base directory where you want the
55 # Volumes stored appended with /drive0 for the first drive; /drive1
56 # for the second drive, ... For example, you might use
57 # /var/bacula/drive0 Note: you must not have a trailing slash, and
58 # the string (e.g. /drive0) must be unique, and it must not match
59 # any other part of the directory name. These restrictions could be
60 # easily removed by any clever script jockey.
62 # Full example: disk-changer /var/bacula/conf load 1 /var/bacula/drive0 0 TestVol001
64 # The Volumes will be created with names slot1, slot2, slot3, ... maxslot in the
65 # base directory. In the above example the base directory is /var/bacula.
66 # However, as with tapes, their Bacula Volume names will be stored inside the
67 # Volume label. In addition to the Volumes (e.g. /var/bacula/slot1,
68 # /var/bacula/slot3, ...) this script will create a /var/bacula/loadedn
69 # file to keep track of what Slot is loaded. You should not change this file.
71 # Modified 8 June 2010 to accept Volume names from the calling program as arg 6.
72 # In this case, rather than storing the data in slotn, it is stored in the
73 # Volume name. Note: for this to work, Volume names may not include spaces.
81 # to turn on logging, uncomment the following line
82 #touch $wd/disk-changer.log
84 dbgfile="$wd/disk-changer.log"
86 if test -f $dbgfile; then
87 echo "`date +\"%Y%m%d-%H:%M:%S\"` $*" >> $dbgfile
93 # Create a temporary file
96 TMPFILE=`mktemp -t mtx.XXXXXXXXXX`
97 if test x${TMPFILE} = x; then
98 TMPFILE="$wd/disk-changer.$$"
99 if test -f ${TMPFILE}; then
100 echo "Temp file security problem on: ${TMPFILE}"
106 # check parameter count on commandline
111 if test $pCount -lt $pCountNeed; then
112 echo "usage: disk-changer ctl-device command [slot archive-device drive-index]"
113 echo " Insufficient number of arguments arguments given."
114 if test $pCount -lt 2; then
115 echo " Mimimum usage is first two arguments ..."
117 echo " Command expected $pCountNeed arguments"
124 # Strip off the final name in order to get the Directory ($dir)
125 # that we are dealing with.
128 bn=`basename $device`
129 dir=`echo "$device" | sed -e s%/$bn%%g`
130 if [ ! -d $dir ]; then
131 echo "ERROR: Autochanger directory \"$dir\" does not exist."
132 echo " You must create it."
138 # Get the Volume name from the call line, or directly from
139 # the volslotn information.
144 if test "x$volume" != x && test "x$volume" != "x*NONE*" ; then
145 debug "touching $dir/$volume"
147 echo "$volume" >$dir/volslot${slot}
149 elif [ -f $dir/volslot${slot} ]; then
150 volume=`cat $dir/volslot${slot}`
174 # Check for special cases where only 2 arguments are needed,
175 # all others are a minimum of 5
179 check_parm_count $# 2
182 check_parm_count $# 2
185 check_parm_count $# 4
186 if [ $slot -gt $maxslot ]; then
187 echo "Slot ($slot) out of range (1-$maxslot)"
188 debug "Error: Slot ($slot) out of range (1-$maxslot)"
193 check_parm_count $# 5
194 if [ $drive -gt $maxdrive ]; then
195 echo "Drive ($drive) out of range (0-$maxdrive)"
196 debug "Error: Drive ($drive) out of range (0-$maxdrive)"
199 if [ $slot -gt $maxslot ]; then
200 echo "Slot ($slot) out of range (1-$maxslot)"
201 debug "Error: Slot ($slot) out of range (1-$maxslot)"
208 debug "Parms: $ctl $cmd $slot $device $drive $volume $havevol"
212 debug "Doing disk -f $ctl unload $slot $device $drive $volume"
214 if [ -f $dir/loaded${drive} ]; then
215 ld=`cat $dir/loaded${drive}`
217 echo "Storage Element $slot is Already Full"
218 debug "Unload error: $dir/loaded${drive} is already unloaded"
221 if [ $slot -eq $ld ]; then
222 echo "0" >$dir/loaded${drive}
223 unlink $device 2>/dev/null >/dev/null
225 echo "Storage Element $slot is Already Full"
226 debug "Unload error: $dir/loaded${drive} slot=$ld is already unloaded"
232 debug "Doing disk $ctl load $slot $device $drive $volume"
235 # Check if slot already in a drive
236 while [ $i -le $maxdrive ]; do
237 if [ -f $dir/loaded${i} ]; then
238 ld=`cat $dir/loaded${i}`
242 if [ $ld -eq $slot ]; then
243 echo "Drive ${i} Full (Storage element ${ld} loaded)"
244 debug "Load error: Cannot load Slot=${ld} in drive=$drive. Already in drive=${i}"
249 # Check if we have a Volume name
251 if [ $havevol -eq 0 ]; then
252 # check if slot exists
253 if [ ! -f $dir/slot${slot} ] ; then
254 echo "source Element Address $slot is Empty"
255 debug "Load error: source Element Address $slot is Empty"
259 if [ -f $dir/loaded${drive} ]; then
260 ld=`cat $dir/loaded${drive}`
264 if [ $ld -ne 0 ]; then
265 echo "Drive ${drive} Full (Storage element ${ld} loaded)"
266 echo "Load error: Drive ${drive} Full (Storage element ${ld} loaded)"
269 echo "0" >$dir/loaded${drive}
270 unlink $device 2>/dev/null >/dev/null
271 if [ $havevol -ne 0 ]; then
272 ln -s $dir/$volume $device
275 ln -s $dir/slot${slot} $device
278 if [ $rtn -eq 0 ]; then
279 echo $slot >$dir/loaded${drive}
285 debug "Doing disk -f $ctl -- to list volumes"
287 if [ -f $dir/barcodes ]; then
291 while [ $i -le $maxslot ]; do
295 if [ $havevol -eq 0 ]; then
307 # ***FIXME*** must add new Volume stuff
309 debug "Doing disk -f $ctl -- to list volumes"
311 if [ ! -f $dir/barcodes ]; then
315 # we print drive content seen by autochanger
316 # and we also remove loaded media from the barcode list
318 while [ $i -le $maxdrive ]; do
319 if [ -f $dir/loaded${i} ]; then
320 ld=`cat $dir/loaded${i}`
321 v=`awk -F: "/^$ld:/"' { print $2 }' $dir/barcodes`
323 echo "^$ld:" >> $TMPFILE
328 # Empty slots are not in barcodes file
329 # When we detect a gap, we print missing rows as empty
330 # At the end, we fill the gap between the last entry and maxslot
331 grep -v -f $TMPFILE $dir/barcodes | sort -n | \
332 perl -ne 'BEGIN { $cur=1 }
344 END { while ($cur < '"$maxslot"') { print "S:$cur:E\n"; $cur++; } } '
350 # ***FIXME*** must add new Volume stuff
354 if [ -f $dir/slot{$slotdest} ]; then
355 echo "destination Element Address $slot is Full"
358 if [ ! -f $dir/slot${slot} ] ; then
359 echo "source Element Address $slot is Empty"
363 echo "Transfering $slot to $slotdest"
364 mv $dir/slot${slot} $dir/slot{$slotdest}
366 if [ -f $dir/barcodes ]; then
367 sed "s/^$slot:/$slotdest:/" > $TMPFILE
368 sort -n $TMPFILE > $dir/barcodes
373 debug "Doing disk -f $ctl $drive -- to find what is loaded"
375 if [ -f $dir/loaded${drive} ]; then
376 a=`cat $dir/loaded${drive}`
380 debug "Loaded: drive=$drive is $a"
386 debug "Doing disk -f $ctl -- to get count of slots"